SaltStack導入Redis主従実装
SaltStack導入Redis主従実装
必要:
192.168.56.11はメイン、192.168.56.12は
redisは0.0.0.0ではなく、自分のipアドレスをリスニングします。
分析:linux-node 1インストール構成起動linux-node 2インストール構成起動設定主従
[root@linux-node1 ~]# yum install redis -y
[root@linux-node1 prod]# mkdir redis/files -p
[root@linux-node1 redis]# cp /etc/redis.conf /srv/salt/prod/redis/files/
[root@linux-node1 redis]# tree
.
├── files
│ └── redis.conf
└── init.sls
1 directory, 2 files
[root@linux-node1 redis]# vim init.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:
PORT: 6379
IPADDR: {{ grains['fqdn_ip4'][0] }}
redis-service:
service.running:
- name: redis
- enable: True
- reload: True
[root@linux-node1 redis]# salt '*' state.sls redis.init saltenv=prod # sls
[root@linux-node1 redis]# netstat -tulnp|grep redis-server
tcp 0 0 192.168.56.11:6379 0.0.0.0:* LISTEN 10186/redis-server
[root@linux-node2 ~]# netstat -tulnp |grep redis-server
tcp 0 0 192.168.56.12:6379 0.0.0.0:* LISTEN 17973/redis-server
:
[root@linux-node1 redis]# vim master.sls
include:
- redis.init
[root@linux-node1 redis]# vim slave.sls
include:
- redis.init
slave_config:
cmd.run:
- name: redis-cli -h 192.168.56.12 slaveof 192.168.56.11 6379--->
- unless: redis-cli -h 192.168.56.12 info |grep role:slave--> node2 ,
- require:
- service: redis-service
[root@linux-node1 redis]# vim /srv/salt/base/top.sls # top file
prod:
'linux-node1.example.com':
- lamp
- redis.master
'linux-node2.example.com':
- lamp
- redis.slave
[root@linux-node1 redis]# salt '*' state.highstate
......
----------
ID: slave_config
Function: cmd.run
Name: redis-cli -h 192.168.56.12 slaveof 192.168.56.11 6379
Result: True
Comment: Command "redis-cli -h 192.168.56.12 slaveof 192.168.56.11 6379" run
Started: 12:08:46.428924
Duration: 31.328 ms
Changes:
----------
pid:
18132
retcode:
0
stderr:
stdout:
OK
Summary for linux-node2.example.com
-------------
Succeeded: 14 (changed=1)
Failed: 0
-------------
Total states run: 14
Total run time: 1.527 s
......
[root@linux-node1 redis]# tree
.
├── files
│ └── redis.conf
├── init.sls
├── master.sls
└── slave.sls
1 directory, 4 files
[root@linux-node1 redis]# cat slave.sls
include:
- redis.init
slave_config:
cmd.run:
- name: redis-cli -h 192.168.56.12 slaveof 192.168.56.11 6379
- unless: redis-cli -h 192.168.56.12 info |grep role:slave
- require:
- service: redis-service
TIPS:生産環境では、必ずtest=Trueを使用してテストを行い、目標はノードを選択して行い、エラーを回避し、業務の運行に影響を与える.
[root@linux-node1 ~]# yum install redis -y
[root@linux-node1 prod]# mkdir redis/files -p
[root@linux-node1 redis]# cp /etc/redis.conf /srv/salt/prod/redis/files/
[root@linux-node1 redis]# tree
.
├── files
│ └── redis.conf
└── init.sls
1 directory, 2 files
[root@linux-node1 redis]# vim init.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:
PORT: 6379
IPADDR: {{ grains['fqdn_ip4'][0] }}
redis-service:
service.running:
- name: redis
- enable: True
- reload: True
[root@linux-node1 redis]# salt '*' state.sls redis.init saltenv=prod # sls
[root@linux-node1 redis]# netstat -tulnp|grep redis-server
tcp 0 0 192.168.56.11:6379 0.0.0.0:* LISTEN 10186/redis-server
[root@linux-node2 ~]# netstat -tulnp |grep redis-server
tcp 0 0 192.168.56.12:6379 0.0.0.0:* LISTEN 17973/redis-server
:
[root@linux-node1 redis]# vim master.sls
include:
- redis.init
[root@linux-node1 redis]# vim slave.sls
include:
- redis.init
slave_config:
cmd.run:
- name: redis-cli -h 192.168.56.12 slaveof 192.168.56.11 6379--->
- unless: redis-cli -h 192.168.56.12 info |grep role:slave--> node2 ,
- require:
- service: redis-service
[root@linux-node1 redis]# vim /srv/salt/base/top.sls # top file
prod:
'linux-node1.example.com':
- lamp
- redis.master
'linux-node2.example.com':
- lamp
- redis.slave
[root@linux-node1 redis]# salt '*' state.highstate
......
----------
ID: slave_config
Function: cmd.run
Name: redis-cli -h 192.168.56.12 slaveof 192.168.56.11 6379
Result: True
Comment: Command "redis-cli -h 192.168.56.12 slaveof 192.168.56.11 6379" run
Started: 12:08:46.428924
Duration: 31.328 ms
Changes:
----------
pid:
18132
retcode:
0
stderr:
stdout:
OK
Summary for linux-node2.example.com
-------------
Succeeded: 14 (changed=1)
Failed: 0
-------------
Total states run: 14
Total run time: 1.527 s
......
[root@linux-node1 redis]# tree
.
├── files
│ └── redis.conf
├── init.sls
├── master.sls
└── slave.sls
1 directory, 4 files
[root@linux-node1 redis]# cat slave.sls
include:
- redis.init
slave_config:
cmd.run:
- name: redis-cli -h 192.168.56.12 slaveof 192.168.56.11 6379
- unless: redis-cli -h 192.168.56.12 info |grep role:slave
- require:
- service: redis-service