Starting as a Boot-time Service
Till now we have downloaded and run Consul from the command line, but we want it to autostart whenever the machine reboots. The cluster should keep on functioning whenever this happens. We need to have the right mix of configuration and auto-start scripts so that no manual intervention becomes necessary once the cluster has been booted once.
Ubuntu
Upstart is an event-based replacement for the /sbin/init daemon which handles starting of tasks and services during boot, stopping them during shutdown and supervising them while the system is running.
The Upstart script will be identical on all the servers. The client script will be slightly different.
Create a file within the /etc/init
directory to hold Consul configuration on all servers and clients.
In Servers
root@server1:~#sudo vim /etc/init/consul.conf
description "Consul server process"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]
respawn
setuid consul
setgid consul
exec consul agent -config-dir /etc/consul.d/server
A brief description of what each command means follows:
* "start" - a job or emit an event.
* "local-filesystems" - A service that wishes to be running once local filesystems are mounted.
* "net-device-up IFACE=eth0" - A service that wishes to be running when device eth0 is running.
* "stop on runlevel [!12345]" - stop the process when halting or rebooting the server.
* "respawn" - restart the process if it ever dies unexpectedly.
* "setuid & setgid" - specifying the user and group that the process should run under.
* "exec" - providing the actual command that we want to run.
Before proceeding, make sure that
- The upstart package is installed.
- The user and group which are specified in the
init/consul.conf
are created. - The
/var/consul
directory belongs to the consul user.
Copy the contents of the file called /etc/init/consul.conf
to each of the servers and the client.
We need to change the configuration directory that is passed into the actual consul
command in the client's version of the file.
In Client
#vim /etc/init/consul.conf
description "Consul Client process"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]
respawn
setuid consul
setgid consul
exec consul agent -config-dir /etc/consul.d/client
Starting the cluster with upstart scripts
As root, start the Consul service in all the servers and the client.
root@server1:~#start consul
root@server1:~#su consul
consul@client:~$service consul status
consul start/running, process 5657
consul@client:~$consul members
Servername | IP Address | Role |
---|---|---|
server1.example.com | 192.168.1.11 | Consul Server |
server2.example.com | 192.168.1.12 | Consul Server |
server3.example.com | 192.168.1.13 | Consul Server |
client.example.com | 192.168.1.21 | Consul Client |
consul info
* Centos
TBD
* RHEL
TBD