(This post is continuation of ENABLING BEACONS IN SALTSTACK.)
In this post we are going to create the reactor for event driven infrastructure saltstack, Salt’s Reactor system gives Salt the ability to trigger actions in response to an event. It is a simple interface to watching Salt’s event bus for event tags that match a given pattern and then running one or more commands in response.
Description: Ubuntu 16.04.2 LTS
Saltmaster: 192.168.232.70
host1: 192.168.232.71
host2: 192.168.232.72
Reactor config for Service(Nginx and Diskusage):
# cat /etc/salt/master.d/reactor.conf
reactor:
– ‘salt/beacon/*/service/’:
– /srv/reactor/nginx-mon.sls
Reactor config-handler:
# cat /srv/reactor/nginx-mon.sls
{% if data[‘id’] %}
web_state_run:
local.state.apply:
– tgt: {{ data[‘id’] }}
– arg:
– webserver.nginx_up
{% endif %}
# cat /srv/reactor/disk-cleanup.sls
{% if data[‘id’] %}
cleanup_tmp_dir:
local.state.apply:
– tgt: {{ data[‘id’] }}
– arg:
– disk-cleanup
{% endif %}
Reactor execution state file:
# cat /srv/salt/webserver/nginx_up.sls
make_nginx_online:
service.running:
– name: nginx
# cat /srv/salt/disk-cleanup.sls
clean_tmp_directory:
file.absent:
– name: /tmp/bigfile2
Let us restart the salt-master service.
on salt-master:
# salt-run state.event pretty=True
(or)
We can see even more information by starting the salt-master into debug mode, lets check the below command.
# salt-master -l debug
(use killall -9 salt-master if you unable to start the salt-master in debug-mode and with error like service already listening)
On the event bus:
Reactor check Nginx service:
Reactor check disk usage:
– (run the command: ‘dd if=/dev/zero of=/tmp/bigfile2 bs=1M count=2000’ on minion to increase the diskspace)
The disk space exceeds the limit now, lets see the reactor
Reactor check through debug-mode:
Nginx service:
We need to stop the salt-master service,
# systemctl stop salt-master
Start the salt-master in debug-mode.
# salt-master -l debug
We can see lot of debug and info messages on the screen,
Disk usage:
– (run the command: ‘dd if=/dev/zero of=/tmp/bigfile2 bs=1M count=2000’ on minion to increase the diskspace)
The disk space exceeds the limit now, lets see the reactor
– disk-cleanup.sls contents should be based on your circumstances, no need to use the same like /tmp/bigfile2.
As we already created the file bigfile2 under the /tmp, so that we have added to remove that file to make the disk usage within the limit, you can use files based on your environment or disk usage.
Video:
Thanks!!