Bootstrapping a consul cluster with cloud-init-buddy

Bootstrapping a consul cluster is a non-trivial operation mostly because it requires sequencing the startup of the cluster nodes with the starting of the consul agents. You can’t form a cluster until you know the IP addresses of the nodes to pass to the consul agent for registration and cluster formation. So you first need to start the nodes and then distribute the IP addresses to all the nodes that need to be in the cluster. This is where cloud-init-buddy comes in. Cloud-init-buddy provides the glue for the cluster nodes to discover each other and not worry about sequencing startup and setup. Everything happens in the cloud-init script with all the sequencing and discovery automatically handled by the cluster nodes themselves. First thing we need to do is create a cloud-init-buddy node. I’m going to use digital ocean VMs but you can use whatever cloud provider you feel most comfortable with. The only thing you need is for the VMs to support cloud-init. Here’s the cloud-init configuration for setting up the cloud-init-buddy node

Wait for the VM to come up. Once it is up, log in to find out the password for the admin user because we need the password for the consul nodes to talk to cloud-init-buddy to coordinate the cluster formation. You can get the admin password by running cat /cloud-init-buddy/password <(echo). At this point we have all the pieces and can write the cloud-init scripts for the consul cluster. I’m going to use the values that I got and you will need to substitute your own values for the IP address and admin password

It looks a little ugly but it gets the job done. Cloud-init-buddy also provides an endpoint for storing and retrieving tar packages but I didn’t use that feature here. It comes in handy when you want to include other assets on the consul nodes or if you want to use something other than bash for bootstrapping the node. The package could include a ruby or python script and the cloud-init script would just be reduced to downloading the package, unpacking it, and executing the necessary script from the package.