Getting Started with Consul

Simple Server / Simple Service

So if you’re here you already probably know a little about what Consul is and what it does.

But you might be saying “How do I get started?”, or “What things can I do with this?” or “Why is it spelled wrong?”

Well I don’t have answers to the last question, but let’s tackle the first two and do some cool things with it.

Let’s get started

Ok first we need to download a few tools. We will use these to setup a “Virtual Data Center” on your home computer. How cool is that?

Download and install the following tools. If you are installing on Windows do this as an admin user.

Software Location
Vagrant http://vagrantup.com
VirtualBox http://virtualbox.org
Git http://git-scm.com

Fantastic!

Launching your consul servers

Go ahead and start your git bash.

Great. Now let’s create a directory for our projects. Run the following commands to create a project directory called ‘projects’ under your home directory.

mkdir ~/projects
cd ~/projects

Now we need to pull down our Vagrant project from GitHub. Run the following command.

git clone https://github.com/autostructure/vagrant-consul.git
cd vagrant-consul

Finally let’s start up our Consul machine. This command will download a virtual machine from the cloud and start it up.

vagrant up consul0

Give it a few minutes, or a while. It depends on the speed of your computer and network. Once it finishes consul will be up and running and we can poke around.

Poking Around Consul Town

When Consul was installed into the VM we instructed it to also include a user interface. This will come in handy as we investigate what consul can do.

GO ahead and bring up a browser, any one will do. Insert the following URL:

Now you should see the Consul console. (I’ve been waiting the whole article to drop that one.)

The top of the console has 5 sections:

  • Services
  • Nodes
  • Key/Value
  • ACL
  • DC1 (Datacenter)

The titles are fairly self-explanatory. Don’t worry iif there is any confusion though. We will be touching on all of these subjects one at a time.

Our First Service

Ok, when we started this you probably remember Consul’s description; “Service Discovery Tool”. So let’s go discover a service.

Let’s start-up a Tomcat server that contains a sample app which will act as our service.

vagrant up tomcat0

When this has finished running our service will be running. Let’s take a look at it. Open up a new browser tab and go to:

http://10.20.1.10:8080

You should see the Tomcat splash screen.

Nice.

Go back to your Consul UI and click on Services. You will see that it is now tracking our new service. The service is marked in green meaning that that “Hey everything is OK.”

Before we go for today let’s see what happens when Tomcat is shutdown. Make the following edit to the file manifests/tomcat0.yaml

tomcat::service { 'default':
  catalina_home  => '/opt/tomcat',
  service_ensure => 'stopped',     # Change this from running to stopped
}

Let’s ask Vagrant to have Puppet re-run our code with the new service change.

vagrant provision tomcat0

The Tomcat server has now shutdown and the service is no more. This change will be reflected on the console UI which will color the service orange.