自动化运维之三Saltstack之部署Redis主从

1.Saltstack部署Redis主从

1.1安装redis软件包

[root@linux-node1 ~]# yum install -y redis
......省略部分........
Installed:
 redis.x86_64 0:3.2.12-1.el7 

Dependency Installed:
 jemalloc.x86_64 0:3.6.0-1.el7 

Complete!

1.2设计redis目录架构

[root@linux-node1 ~]# cd /srv/salt/prod/
[root@linux-node1 prod]# mkdir redis
[root@linux-node1 prod]# cd redis/
[root@linux-node1 redis]# mkdir files
[root@linux-node1 prod]# tree
.
├── apache
│   ├── files
│   │   └── httpd.conf
│   ├── init_require_in.sls
│   ├── init_require.sls
│   └── init.sls
├── lamp.sls
├── mysql
│   ├── files
│   │   └── my.cnf
│   └── init.sls
├── php
│   ├── files
│   │   └── php.ini
│   └── init.sls
└── redis
 ├── files
 └── init.sls
8 directories, 10 files

1.3编辑redis状态文件

[root@linux-node1 redis]# pwd
/srv/salt/prod/redis
[root@linux-node1 redis]# cat 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

1.4修改redis配置文件并拷贝到Salt的files目录里进行配置管理

[root@linux-node1 redis]# egrep -v "#|^$" /etc/redis.conf 
bind {{ IPADDR }}
protected-mode yes
port {{ PORT }}
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
##拷贝到salt配置管理文件里
[root@linux-node1 redis]# cd files/
[root@linux-node1 files]# cp /etc/redis.conf .
[root@linux-node1 files]# pwd
/srv/salt/prod/redis/files

1.5手动进行运行测试

[root@linux-node1 ~]# salt '*' state.sls redis.init saltenv=prod
.......省略部分.......
Summary
------------
Succeeded: 3 (changed=3)
Failed: 0
------------
Total states run: 3
[root@linux-node1 ~]# netstat -lntup|grep 6379
tcp     0       0     118.190.201.11:6379      0.0.0.0:*     LISTEN     62196/redis-server
[root@linux-node2 ~]# to learn how to use Xshell prompt^C
[root@linux-node2 ~]# netstat -lntup|grep 6379
tcp     0       0     118.190.201.12:6379      0.0.0.0:*     LISTEN     8966/redis-server 1

2.部署配置redis主从

2.1编写主从状态文件

#编写主配置状态文件
[root@linux-node1 redis]# vim master.sls
include:
  - redis.init
#编写从配置状态文件
思路:执行命令获取redis的slave的信息
[root@linux-node2 ~]# redis-cli -h 118.190.201.12 info
连接主库
[root@linux-node2 ~]# redis-cli -h 118.190.201.12 slaveof 118.190.201.11 6379
OK
[root@linux-node2 ~]# redis-cli -h 118.190.201.12 info  #转换为slave
[root@linux-node1 redis]# vim slave.sls
include:
  - redis.init

slave_confing:
  cmd.run:
    - name: redis-cli -h 118.190.201.12 slaveof 118.190.201.11 6379
    - require:
      - service: redis-service
#top_file文件进行指定
[root@linux-node1 redis]# cat /srv/salt/base/top.sls
#base:
#  'os:CentOS':
#    - match: grain
#    - web.apache

prod:
  'linux-node1.example.com':
    - lamp
    - redis.master
  'linux-node2.example.com':
    - lamp
    - redis.slave
#执行状态
[root@linux-node1 ~]# salt '*' state.highstate
......省略部分.......
Summary
-------------
Succeeded: 13
Failed: 0
-------------
Total states run: 13

2.2配置状态之间的判断unless模块

[root@linux-node2 ~]# redis-cli -h 118.190.201.12 info |grep role:slave
role:slave
[root@linux-node2 ~]# echo $?
0
[root@linux-node1 redis]# vim slave.sls
include:
  - redis.init

slave_confing:
  cmd.run:
    - name: redis-cli -h 118.190.201.12 slaveof 118.190.201.11 6379
    - unless: redis-cli -h 118.190.201.12 info |grep role:slave
    - require:
      - service: redis-service

####注意事项
生产环境需要test=True先测试
salt 'linux-node*' state.highstate test=True   ###一台测试好后,在使用*进行全部test=True进行测试,之后再发布

Everything will be ok in the end, if it’s not ok, it’s not the end.  所有的事情到最后都会好起来的,如果不够好,说明还没到最后。

0
如无特殊说明,文章均为本站原创,转载请注明出处

该文章由 发布

这货来去如风,什么鬼都没留下!!!
发表我的评论

Hi,请填写昵称和邮箱!

取消评论
代码 贴图 加粗 链接 删除线 签到