saltstack 实现redis主从
centos7.4
172.16.80.5 redis 主
172.16.80.6 redis 从
目录结构如下
file_roots: base: - /srv/salt/base dev: - /srv/salt/dev test: - /srv/salt/test prod: - /srv/salt/prod
[root@zabbix redis]# vim /srv/salt/base/top.sls
[root@zabbix redis]# cat redis.sls redis-install: pkg.installed: - name: redis redis-config: file.managed: - name: /etc/redis.conf - source: salt://redis/files/redis.conf - user: root - group: root
- mode: 644 - template: jinja - defaults: IPADDR: {{ grains['fqdn_ip4'][0] }} PORT: 6379 redis-service: service.running: - name: redis - enable: True - reload: True [root@zabbix redis]# cat master.sls include: - redis.redis [root@zabbix redis]# cat slave.sls include: - redis.redis slave_config: cmd.run: - name: redis-cli -h 172.16.80.6 slaveof 172.16.80.5 6379 - unless: redis-cli -h 172.16.80.6 info|grep role:slave - require: - service: redis-service
Vim files/redis.conf 修改如下配置文件
bind {{ IPADDR }}
port {{ PORT }}
daemonize yes
[root@zabbix redis]# salt '*' state.highstate zabbix: ---------- ID: redis-install Function: pkg.installed Name: redis Result: True Comment: The following packages were installed/updated: redis Started: 10:36:42.414507 Duration: 20238.065 ms Changes: ---------- redis: ---------- new: 3.2.10-2.el7 old: ---------- ID: redis-config Function: file.managed Name: /etc/redis.conf Result: True Comment: File /etc/redis.conf updated Started: 10:37:02.743576 Duration: 421.028 ms Changes: ---------- diff: --- +++ @@ -45,7 +45,7 @@ # Examples: # # bind 192.168.1.100 10.0.0.1 -# bind 127.0.0.1 ::1 +bind 172.16.80.5 # # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the # internet, binding to all the interfaces is dangerous and will expose the @@ -58,7 +58,6 @@ # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES # JUST COMMENT THE FOLLOWING LINE. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -bind 127.0.0.1 # Protected mode is a layer of security protection, in order to avoid that # Redis instances left open on the internet are accessed and exploited. @@ -125,7 +124,7 @@ # By default Redis does not run as a daemon. Use 'yes' if you need it. # Note that Redis will write a pid file in /var/run/redis.pid when daemonized. -daemonize no +daemonize yes # If you run Redis from upstart or systemd, Redis can interact with your # supervision tree. Options: mode: 0644 user: root ---------- ID: redis-service Function: service.running Name: redis Result: True Comment: Service redis has been enabled, and is running Started: 10:37:04.982412 Duration: 174.844 ms Changes: ---------- redis: True Summary for zabbix ------------ Succeeded: 3 (changed=3) Failed: 0 ------------ Total states run: 3 Total run time: 20.834 s k8s-node1: ---------- ID: redis-install Function: pkg.installed Name: redis Result: True Comment: The following packages were installed/updated: redis Started: 10:36:41.350432 Duration: 47371.887 ms Changes: ---------- redis: ---------- new: 3.2.10-2.el7 old: ---------- ID: redis-config Function: file.managed Name: /etc/redis.conf Result: True Comment: File /etc/redis.conf updated Started: 10:37:28.751141 Duration: 179.74 ms Changes: ---------- diff: --- +++ @@ -45,7 +45,7 @@ # Examples: # # bind 192.168.1.100 10.0.0.1 -# bind 127.0.0.1 ::1 +bind 172.16.80.6 # # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the # internet, binding to all the interfaces is dangerous and will expose the @@ -58,7 +58,6 @@ # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES # JUST COMMENT THE FOLLOWING LINE. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -bind 127.0.0.1 # Protected mode is a layer of security protection, in order to avoid that # Redis instances left open on the internet are accessed and exploited. @@ -125,7 +124,7 @@ # By default Redis does not run as a daemon. Use 'yes' if you need it. # Note that Redis will write a pid file in /var/run/redis.pid when daemonized. -daemonize no +daemonize yes # If you run Redis from upstart or systemd, Redis can interact with your # supervision tree. Options: mode: 0644 user: root ---------- ID: redis-service Function: service.running Name: redis Result: True Comment: Service redis has been enabled, and is running Started: 10:37:29.930887 Duration: 147.004 ms Changes: ---------- redis: True ---------- ID: slave_config Function: cmd.run Name: redis-cli -h 172.16.80.6 slaveof 172.16.80.5 6379 Result: True Comment: Command "redis-cli -h 172.16.80.6 slaveof 172.16.80.5 6379" run Started: 10:37:30.079706 Duration: 33.352 ms Changes: ---------- pid: 5141 retcode: 0 stderr: stdout: OK Summary for k8s-node1 ------------ Succeeded: 4 (changed=4) Failed: 0 ------------ Total states run: 4 Total run time: 47.732 s
验证结果