Skip to content

Instantly share code, notes, and snippets.

@rackerbenoit
Last active August 29, 2015 14:16
Show Gist options
  • Save rackerbenoit/9e907b9b5ff0aa00fdc4 to your computer and use it in GitHub Desktop.
Save rackerbenoit/9e907b9b5ff0aa00fdc4 to your computer and use it in GitHub Desktop.
Consul Events
Consul must first be installed and configured using the Getting Started guide on consul.io
There are also other checks that I had to do on my own including health checks and registering services before
this was up and running properly. I initially started to use Atlas, a nice UI interface, to test and show stats
but the team decided against that method for now.
All these represent AWS EC2 node instances
Run on the server node:
[root@ip-10-168-16-98 pearson]# consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -node=server-boss -bind=10.168.16.98
You should see this message from the server node:
2015/03/05 17:05:49 [INFO] consul: cluster leadership acquired
2015/03/05 17:05:49 [INFO] consul: New leader elected: server-boss
Run on first agent:
[root@ip-10-168-18-181 pearson]# consul agent -data-dir /tmp/consul -node=agent-two -bind=10.168.18.181
Run again on another server if you would like to connect another agent:
[root@ip-10-168-15-13 pearson]# consul agent -data-dir /tmp/consul -node=agent-three -bind=10.168.15.13
Connect the Servers using the server node. Do this until all agents are connected
[pearson@ip-10-168-16-98 ~]$ consul join 10.168.15.13
AND
[pearson@ip-10-168-16-98 ~]$ consul join 10.168.18.181
Successfully joined cluster by contacting 1 nodes.
You should see a message similar to this on the agent side once the server begins to join the agents:
2015/03/05 17:15:09 [INFO] serf: EventMemberJoin: agent-two 10.168.18.181
2015/03/05 17:15:09 [INFO] serf: EventMemberJoin: server-boss 10.168.16.98
2015/03/05 17:15:09 [INFO] serf: EventMemberJoin: agent-four 10.168.18.239
2015/03/05 17:15:09 [INFO] serf: EventMemberJoin: agent-three 10.168.15.13
2015/03/05 17:15:09 [INFO] consul: adding server server-boss (Addr: 10.168.16.98:8300) (DC: dc1)
2015/03/05 17:15:09 [INFO] consul: New leader elected: server-boss
Notice that each server, whether server or agent, joins the group and 'gossips.'
Type the command 'consul members' and you should see all the connections:
[pearson@ip-10-168-16-98 ~]$ consul members
Node Address Status Type Build Protocol
server-boss 10.168.16.98:8301 alive server 0.5.0 2
agent-three 10.168.15.13:8301 alive client 0.5.0 2
agent-two 10.168.18.181:8301 alive client 0.5.0 2
agent-four 10.168.18.239:8301 alive client 0.5.0 2
agent-five 10.168.19.71:8301 alive client 0.5.0 2
This command can be used on any connected agent or server.
I placed a shell script in /usr/bin/ that would execute if a particular event happened:
[root@ip-10-168-16-98 pearson]# consul watch -type event -name web-deploy /usr/bin/my-deploy-handler.sh &
[1] 16798 -> pid
Notice I am running this in the background so I can use the same terminal. I don't care about the output
This 'watch' is being run on the main server in this example. The shell script spits out a .txt file in the server's
home directory. The name of the 'event' being 'watched' for is 'web-deploy'
On an agent, I issue this command:
[pearson@ip-10-168-15-13 ~]$ consul event -name web-deploy
Event ID: d0d4aeb8-772b-4c95-b0c6-131608351c0d
The agent 'watching' for the event now has a consul.txt file in the home directory. In this case it is in the home diectory
of the main server.
Now I want to make another agent watch for an event:
[root@ip-10-168-15-13 pearson]# consul watch -type event -name web-deploy-agent /usr/bin/deploy-on-new-agent.sh &
[1] 3734
[root@ip-10-168-15-13 pearson]# ps -ef | grep consul
root 3734 28263 0 19:01 pts/1 00:00:00 consul watch -type event -name web-deploy-agent /usr/bin/deploy-on-new-agent.sh
This new event is called 'web-deploy-agent'
I now put a new 'event' command inside the bash script for 'web-deploy,' which was the initial event. The new 'event'
added will be 'web-deploy-agent'. Now issue this command from the original server:
[root@ip-10-168-16-98 pearson]# consul event -name web-deploy
This triggers the 'watch' for 'web-deploy' and 'web-deploy-agent' which creates a new file in the home directory of the
original server and agent. Once a 'watch' is issued on one of the agents or servers, an 'event' with a particular attribute
will trigger the 'watch' and execute a specified command.
Now, I am adding a new 'watch' to the main server to append to the consul.txt file:
[root@ip-10-168-16-98 pearson]# ps -ef | grep consul
root 16680 16650 0 17:05 pts/2 00:00:30 consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -node=server-boss -bind=10.168.16.98
root 16798 16781 0 17:36 pts/3 00:00:00 consul watch -type event -name web-deploy /usr/bin/my-deploy-handler.sh
root 17123 16781 0 20:16 pts/3 00:00:00 consul watch -type event -name add-to-consul /usr/bin/add-to-consul.sh
root 17130 16781 0 20:16 pts/3 00:00:00 grep consul
Once the 'add-to-consul' 'event' happens, a line will be appended to the file:
[root@ip-10-168-16-98 pearson]# consul event -name add-to-consul
This is what is contained in consul.txt after running the event twice:
This is some Consul testing
This is appending to consul.txt in a seaprate event
This is appending to consul.txt in a separate event
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment