HashiCorp (HC) offers a suite of tools that are sometimes collectively called HashiStack. While HC tools have great tutorials, instructions to set them up together in a local cluster are not easy to come by.
This project contains tooling to set up a local HashiStack cluster using the following tools:
- Vagrant (used to manage local Virtual Machines using VirtualBox)
- Ansible (install and configure necessary software in the VMs)
Ansible is one of the many provisioners supported by Vagrant. I've used Ansible because:
- I've used Ansible before
- I like the idea of idempotent scripts
- This project assumes that you are running Ubuntu OS locally
- This project has only been tested on Ubuntu 20.04
- Local Virtual Machines (VM) are configured to use Ubuntu 20.04
- You can change this by tweaking the Vagrantfile.
-
Install software necessary for bootstrapping the cluster. This will install Vagrant, VirtualBox, Python 3.9, and Ansible. You can skip this step these are already available in your machine.
- Please go through
prerequisites.sh
and verify that it is not doing anything unsafe - Installation requires your
sudo
password - please keep it ready
hashilab$ ./prerequisites.sh
- Please go through
-
Install the
vagrant-docker-compose
plugin, used to install Docker in all VMs. This plugin will be installed locally inside the project repository.hashilab$ vagrant plugin install vagrant-docker-compose # Installing the 'vagrant-docker-compose' plugin. This can take a few minutes...
-
Bring up the VMs. Vagrant will invoke Ansible to set up and configure the necessary software the first time this is done. This will take a few minutes (~15 in my experience) depending on your machine and network speed.
hashilab$ vagrant up # Bringing machine 'server' up with 'virtualbox' provider... # Bringing machine 'client1' up with 'virtualbox' provider... # Bringing machine 'client2' up with 'virtualbox' provider... # ...
If everything goes well you should be able to see the following:
- Three VMs running in VirtualBox. These will be named:
server
client1
andclient2
respectively
- Consul up and running at http://localhost:8500/ui/
- Nomad up and running at http://localhost:4646/ui/
You can SSH into the machines as follows:
hashilab$ vagrant ssh <machine-name>
For e.g. to access server
VM:
hashilab$ vagrant ssh server
Both Consul and Nomad are set up to run as services, using systemd
.
Check their status and logs to see if the services started properly:
hashilab$ vagrant ssh server
# After SSH into one of the VMs
vagrant@server:~$ systemctl status consul
● consul.service - Consul Startup process
Loaded: loaded (/etc/systemd/system/consul.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2022-03-08 22:35:03 IST; 19min ago
Main PID: 17486 (consul)
Tasks: 8 (limit: 2314)
CGroup: /system.slice/consul.service
vagrant@server:~$ systemctl status nomad
● nomad.service - Nomad Startup process
Loaded: loaded (/etc/systemd/system/nomad.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2022-03-08 22:36:22 IST; 18min ago
Main PID: 18413 (nomad)
Tasks: 9 (limit: 2314)
CGroup: /system.slice/nomad.service
└─18413 /usr/bin/nomad agent -config /etc/nomad.d/
vagrant@server:~$ journalctl -u consul
-- Logs begin at Tue 2022-03-08 22:29:26 IST, end at Tue 2022-03-08 22:54:51 IST. --
Mar 08 22:35:03 server systemd[1]: Started Consul Startup process.
Mar 08 22:35:03 server consul[17486]: 2022-03-08T22:35:03.813+0530 [WARN] agent: skipping file /etc/consul.d/consul.env, extension must be .hcl or .jso
Mar 08 22:35:03 server consul[17486]: 2022-03-08T22:35:03.815+0530 [WARN] agent: skipping file /etc/consul.d/consul.hcl.17325.2022-03-08@22:35:01~, ext
Mar 08 22:35:03 server consul[17486]: 2022-03-08T22:35:03.815+0530 [WARN] agent: The 'ui' field is deprecated. Use the 'ui_config.enabled' field instea
Mar 08 22:35:03 server consul[17486]: 2022-03-08T22:35:03.815+0530 [WARN] agent: BootstrapExpect is set to 1; this is the same as Bootstrap mode.
Mar 08 22:35:03 server consul[17486]: 2022-03-08T22:35:03.816+0530 [WARN] agent: bootstrap = true: do not enable unless necessary
Mar 08 22:35:03 server consul[17486]: 2022-03-08T22:35:03.816+0530 [WARN] agent: using
vagrant@server:~$ journalctl -u nomad
-- Logs begin at Tue 2022-03-08 22:29:26 IST, end at Tue 2022-03-08 22:55:13 IST. --
Mar 08 22:36:22 server systemd[1]: Started Nomad Startup process.
Mar 08 22:36:22 server bash[18413]: ==> WARNING: Bootstrap mode enabled! Potentially unsafe operation.
Mar 08 22:36:22 server bash[18413]: ==> Loaded configuration from /etc/nomad.d/nomad.hcl
Mar 08 22:36:22 server bash[18413]: ==> Starting Nomad agent...
Consul, and Nomad server configurations generated after provisioning are given below.
This file is located at /etc/consul.d/consul.hcl
in the server
VM.
# NOTE: `ansible_eth1` because `eth1` is the name of the interface created via Vagrant
bind_addr = "172.20.20.10"
data_dir = "/var/consul"
datacenter = "dc-local"
enable_script_checks = true
enable_syslog = true
leave_on_terminate = true
log_level = "DEBUG"
node_name = "server"
# // @formatter:off
retry_join = ["172.20.20.10", "172.20.20.21", "172.20.20.22"]
# // @formatter:on
# // @formatter:off
bootstrap_expect = 1
client_addr = "0.0.0.0"
server = true
ui = true
# // @formatter:on
This file is located at /etc/consul.d/nomad.hcl
in the server
VM.
# Ref: https://learn.hashicorp.com/tutorials/nomad/clustering
# NOTE: Bind to `0.0.0.0` to make UI accessible from host
bind_addr = "0.0.0.0"
data_dir = "/var/nomad"
datacenter = "dc-local"
advertise {
http = "172.20.20.10"
rpc = "172.20.20.10"
serf = "172.20.20.10"
}
server {
enabled = true
bootstrap_expect = 1
}
ui {
enabled = true
}
Refer to the examples directory to see some sample jobs. You can run them using Nomad as described below.
-
SSH into the
server
VMhashilab$ vagrant ssh server
-
Vagrant shares the directory where
Vagrantfile
is located with all VMs as/vagrant
inside the VMs. Theexamples
directory can therefore be found at/vagrant/examples
insideserver
. To run a specific example navigate to the appropriate child directory insideexamples
:vagrant@server:~$ cd /vagrant/examples/01_load_balancing_with_fabio/
-
Submit the
fabio.nomad
job:vagrant@server:/vagrant/examples/01_..._fabio$ nomad job run fabio.nomad
-
Submit the
webserver.nomad
job:vagrant@server:/vagrant/examples/01_..._fabio$ nomad job run webserver.nomad
-
If everything goes well you should be able to see the jobs running in the Nomad UI as shown in the screenshot below.
This project has tooling to set up two HashiStack tools: Consul, and Nomad. I'll add more as I go along. Take a look at the issues, and pull requests to see the process.
The following files will help you understand how the cluster is set up.
- Review the Vagrantfile. This file describes/controls the following:
- How the VMs are created, network is configured etc.
- How to install Docker using
vagrant-docker-compose
- How to use Ansible to install other HC software in the VMs after they start
- Review playbooks/hashilab.yml, and playbooks/roles/hashilab/tasks/main.yml
- These describe what software should be installed, and how they should be configured