SaltStack部署Keepalived+Redis的实现

一、环境:

CENTOS7

salt-master:m6  ip:192.168.99.86

redis-master:v73  ip:192.168.99.73

redis-slave:m5  ip:192.168.99.85

 

二、安装文件、配置文件和目录结构

[root@m6 keepalivedredis]# pwd
/srv/salt/keepalivedredis
[root@m6 keepalivedredis]# ls
install.sls  keepalived.conf  redis_backup.sh  redis_check.sh  redis_fault.sh  redis.j2  redis_master.sh  redis_stop.sh

安装部署文件只有一个,避免得多次引用,方便查看


[root@m6 keepalivedredis]# cat install.sls 
install_redis:
  pkg.installed:
    - name: redis

cpredis_conf:
  file.managed:
    - name: /etc/redis.conf
    - source: salt://keepalivedredis/redis.j2
    - user: root
    - group: root
    - template: jinja
    - mode: 644
    - require:
      - pkg: install_redis

mkredisdir:
  cmd.run:
    - name: mkdir -pv /data/redis && chown redis.redis /data/redis
    - unless: test -d /data/redis

redis_running:
  service.running:
    - name: redis
    - enable: True
    - require:
      - pkg: install_redis
    - watch:
      - file: cpredis_conf

install_keepalived:
  pkg.installed:
    - name: keepalived

scripts_dir:
  cmd.run:
    - name: mkdir -pv /etc/keepalived/scripts/
    - unless: test -d /etc/keepalived/scripts/

keepalived_conf:
  file.managed:
    - name: /etc/keepalived/keepalived.conf
    - source: salt://keepalivedredis/keepalived.conf
    - user: root
    - group: root
    - template: jinja
    - mode: 644
    - require:
      - pkg: install_keepalived
      - cmd: scripts_dir

{% for s in ["redis_backup.sh","redis_check.sh","redis_fault.sh","redis_master.sh","redis_stop.sh"] %}
keepalived_{{s}}:
  file.managed:
    - name: /etc/keepalived/scripts/{{s}}
    - source: salt://keepalivedredis/{{s}}
    - user: root
    - group: root
    - template: jinja
    - mode: 755
    - require:
      - pkg: install_keepalived
{% endfor %}

keepalived_running:
  service.running:
    - name: keepalived
    - enable: True
    - require:
      - pkg: install_keepalived
    - watch:
      - file: keepalived_conf

 

redis的配置文件:

[root@m6 keepalivedredis]# cat redis.j2 
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis/redis.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 /data/redis/
{% if grains['role'] == "slave" %}
slaveof {{grains['master_ip']}} 6379
{% endif %}
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

 

keepalived的配置文件:

[root@m6 keepalivedredis]# cat keepalived.conf 
! Configuration File for keepalived

vrrp_script chk_redis {
    script "/etc/keepalived/scripts/redis_check.sh"
    interval 2
}

vrrp_instance VI_1 {
{% if grains['role'] == 'master' %}
    state MASTER
{% else %}
    state BACKUP
{% endif %}
    interface ens37
    virtual_router_id 51
{% if grains['role'] == "master" %}
    priority 101
{% else %}
    priority 100
{% endif %}
#    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass redis
    }
    track_script {
        chk_redis
    }
    virtual_ipaddress {
        {{ grains['vip'] }}
    }
    notify_master /etc/keepalived/scripts/redis_master.sh
    notify_backup /etc/keepalived/scripts/redis_backup.sh
    notify_fault /etc/keepalived/scripts/redis_fault.sh
    notify_stop /etc/keepalived/scripts/redis_stop.sh
}

 

脚本的内容,网上有很多

[root@m6 keepalivedredis]# cat redis_master.sh 
#!/bin/bash
#
REDISCLI="/usr/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master..." >> $LOGFILE 2>&1
echo "run slaveof no one cmd..." >> $LOGFILE
$RESISCLI SLAVEOF {{ grains['master_ip']  }} 6479>> $LOGFILE 2>&1
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1


[root@m6 keepalivedredis]# cat redis_backup.sh 
#!/bin/bash
#
REDISCLI="/usr/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "being salve..." >> $LOGFILE 2>&1
#sleep 25
echo "run slaveof cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF {{ grains['master_ip'] }} 6379 $LOGFILE 2>&1


[root@m6 keepalivedredis]# cat redis_check.sh 
#!/bin/bash
#
ALIVE=`/usr/bin/redis-cli PING`
if [ "$ALIVE" == "PONG" ];then
    echo $ALIVE 
    exit 0
else
    echo $ALIVE
    exit 1
fi


[root@m6 keepalivedredis]# cat redis_stop.sh 
#!/bin/bash
#
LOGFILE=/var/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE


[root@m6 keepalivedredis]# cat redis_fault.sh 
#!/bin/bash
#
$LOGFILE=/var/log/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE

 

三、部署过程和测试:

[root@m6 keepalivedredis]# salt "v73" grains.setvals "{'role':'master','vip':'192.168.99.10','master_ip':'192.168.99.85'}"

[root@m6 keepalivedredis]# salt "m5" grains.setvals "{'role':'slave','vip':'192.168.99.10','master_ip':'192.168.99.73'}"

[root@m6 keepalivedredis]# salt -L 'v73,m5' state.sls keepalivedredis.install

SaltStack部署Keepalived+Redis的实现_第1张图片

 

SaltStack部署Keepalived+Redis的实现_第2张图片

 

测试redis主从切换

 

SaltStack部署Keepalived+Redis的实现_第3张图片

[root@v73 ~]# redis-cli -h 192.168.99.10 -p 6379
192.168.99.10:6379> set ktest "vtest"
OK
192.168.99.10:6379> exit
[root@v73 ~]# service redis stop
Redirecting to /bin/systemctl stop redis.service

此时IP发生切换在m5机器

SaltStack部署Keepalived+Redis的实现_第4张图片

 

用同样的IP仍然可以访问

[root@m5 network-scripts]# redis-cli -h 192.168.99.10 -p 6379
192.168.99.10:6379> get ktest
"vtest"
192.168.99.10:6379> 

 

 

你可能感兴趣的:(SaltStack部署Keepalived+Redis的实现)