Redis一主一从Docker方式部署通过keepalived和 sentinel哨兵模式实现高可用

有两台服务器一台是主,master : 172.24.69.180
另外一台是从, slave :172.24.69.181
vip 地址: 172.24.69.185

1、关闭防火墙

两台服务器都关闭防火墙

systemctl disable --now firewalld
firewall-cmd --state

关闭SELinux

setenforce 0
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
sestatus

首先关闭防火墙,有一次我没有关闭,vip一直无法漂移,找了好久

2、安装docker

两台服务器都安装docker
用二进制安装:
1、获取docker离线二进制包

docker二进制包获取地址:https://download.docker.com/linux/static/stable/x86_64/

2、上传服务器,解压缩

tar xzf docker-20.10.8.tgz
#解压缩完成后将docker目录中的所有文件复制至/usr/bin/目录下
cp docker/* /usr/bin

3、配置docker.service文件
vim /usr/lib/systemd/system/docker.service
在文件输入下面内容

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target

[Service]
Type=notify
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target

4、创建挂载目录

创建目录
mkdir /data/docker

5、确认daemon.json

mkdir /etc/docker

vim /etc/docker/daemon.json

在daemon.json中输入下面内容:

{
    "graph": "/data/docker",
    "storage-driver": "overlay2",
    "registry-mirrors": ["https://q2gr04ke.mirror.aliyuncs.com"],
    "bip": "172.7.21.1/24",
    "log-driver":"json-file",
    "log-opts":{ "max-size" :"100m","max-file":"3"},
    "exec-opts": ["native.cgroupdriver=systemd"]
}

6、启动docker 服务

systemctl daemon-reload
systemctl enable --now docker

3、安装redis

两台服务器都安装redis ,只是主从的配置稍有不一样
1、拉取镜像

 docker pull redis:6.2.6

2、 创建挂载目录

mkdir -p /data/redis/logs
mkdir -p /data/redis/data
chmod 777 /data/redis/data
chmod 777 /data/redis/logs

3、 主节点的配置, 在172.24.69.180上

cat > /data/redis/redis.conf << EOF
bind 0.0.0.0
daemonize no
port 6379
tcp-backlog 511
timeout 300
tcp-keepalive 60
databases 16
 
save 900 1
save 300 10
save 60 10000
 
dbfilename dump.rdb
 
loglevel notice
logfile "/var/log/redis-server.log"
dir "/data"
appendonly yes
appendfilename appendonly.aof
maxmemory-policy volatile-lru
maxmemory 6442450944
appendfsync everysec
 
requirepass pass123
masterauth pass123
EOF

配置文件中的 pass123 是设置的redis的密码

从节点的配置,在172.24.69.181 上

cat > /data/redis/redis.conf << EOF
bind 0.0.0.0
daemonize no
port 6379
tcp-backlog 511
timeout 300
tcp-keepalive 60
databases 16
 
save 900 1
save 300 10
save 60 10000
 
dbfilename dump.rdb
 
loglevel notice
logfile "/var/log/redis-server.log"
dir "/data"
appendonly yes
appendfilename appendonly.aof
maxmemory-policy volatile-lru
maxmemory 6442450944
appendfsync everysec
 
requirepass pass123
masterauth  pass123
 
slaveof 172.24.69.180 6379        
EOF

slaveof 172.24.69.180 这个是主节点的ip

4、Docker启动redis
主节点

docker run -dit --name mianredis --restart=always -v /data/redis/redis.conf:/etc/redis.conf -v /data/redis/data/:/data/ -v /data/redis/logs:/var/log -p 6379:6379 redis:6.2.6 redis-server /etc/redis.conf

从节点

docker run -dit --name slaveredis --restart=always -v /data/redis/redis.conf:/etc/redis.conf -v /data/redis/data/:/data/ -v /data/redis/logs:/var/log -p 6379:6379 redis:6.2.6 redis-server /etc/redis.conf

5、启动redis之后copy redis-cli 到宿主机的 /usr/local/bin/目录下
主节点

docker cp mianredis:/usr/local/bin/redis-cli /usr/local/bin/

从节点

docker cp slaveredis:/usr/local/bin/redis-cli /usr/local/bin/

4、Redis主从+sentinel(哨兵)+keepalived

两个节点都安装,只是配置不一样

实现的原理

在master和backup服务器分别安装哨兵和keepalived,master的优先级为100,backup的优先级为99,在salve服务器上配置vrrp_script检查脚本规则,检查slave当前的角色状态,一旦slave的redis角色状态为master,就把slave的优先级加2变为101,使其获得vip的权限;
当master的redis服务挂掉后,哨兵会将slave提升为新的master,slave检查角色状态为master时将优先级加2获得vip,当原来master的redis服务起来后哨兵将其作为slave加入到主从复制
当变为master的slave节点上redis服务挂掉后,哨兵会将redis的master设置为原来的master节点,vrrp_script检查自己的redis不是master时,将优先级减2变为99,原来的master优先级比slave优先级高,原来的master获得vip权限

1、sentinel.conf配置
主节点配置
路径在/data/redis/sentinel.conf, 在sentinel.conf输入下面的内容

port 26379
dir "/var/log/sentinel"
logfile "/var/log/sentinel/26379.log"
sentinel monitor mymaster 172.24.69.180 6379 1
sentinel down-after-milliseconds mymaster 5000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel auth-pass mymaster pass123
sentinel announce-ip "172.24.69.180"
sentinel announce-port 26379

从节点配置
sentinel.conf配置, 路径在/data/redis/sentinel.conf

port 26379
dir "/var/log/sentinel"
logfile "/var/log/sentinel/26379.log"
sentinel monitor mymaster 172.24.69.180 6379 1
sentinel down-after-milliseconds mymaster 5000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel auth-pass mymaster pass123
sentinel announce-ip "172.24.69.181"
sentinel announce-port 26379

172.24.69.180此处是主节点ip
172.24.69.181 是从节点ip

2、启动哨兵
主节点:

docker run -d --name main --restart=always -v /data/redis/sentinel.conf:/conf/sentinel.conf -v /data/logs/sentinel:/var/log/sentinel  -p 26379:26379 redis:6.2.6  redis-sentinel /conf/sentinel.conf

从节点:

docker run -d --name slave --restart=always -v /data/redis/sentinel.conf:/conf/sentinel.conf -v /data/logs/sentinel:/var/log/sentinel  -p 26379:26379 redis:6.2.6  redis-sentinel /conf/sentinel.conf

5、安装keepalived

两个服务器都安装,只是配置不一样
安装keepalived

yum -y install keepalived 

1、 修改配置文件
主节点的配置:
配置文件为/etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
   router_id redis-225 ##设备名称
}
vrrp_script chkmaster{
     script "/etc/keepalived/chkmaster.sh  127.0.0.1 6379 pass123" #检查当前redis是否为master
     interval 3 #每3秒检查一次
      weight -2 #当本机为master检测结果返回1,将优先级加2
}
vrrp_instance VI_redis {
    state MASTER ##当前设备为master
    interface enp4s1  ##vip绑定的网卡
    virtual_router_id 79 ##备份组id,同一备份组要一致
    priority 100 ##优先级,优先级高的成为master
    advert_int 3 ##每多少秒进行一次健康检查
    unicast_src_ip  172.24.69.180 ##(本地IP地址)
    unicast_peer {
                  172.24.69.181  ##(对端IP地址)此地址一定不能忘记
                       }
    authentication {
        auth_type PASS
        auth_pass redis
    }

    virtual_ipaddress {
        172.24.69.185/24
    }
   track_script
     { ##调用状态检查
          chkmaster
     }
}

从节点配置

! Configuration File for keepalived
global_defs
 {
     router_id redis-224
 }
vrrp_script chkmaster
 {
     script "/etc/keepalived/chkmaster.sh  127.0.0.1 6379 pass123" #检查当前redis是否为master
     interval 3 #每3秒检查一次
      weight -2 #当本机为master检测结果返回1,将优先级加2
  }
vrrp_instance VI_redis
{
   state BACKUP #本机的状态信息
   interface enp4s1   #vip绑定的网卡
    virtual_router_id 79 ##备份组id,同一备份组要一致
    priority 99 ##优先级,优先级高的成为master
    advert_int 3 ##每多少秒进行一次健康检查
    unicast_src_ip  172.24.69.181 ##(本地IP地址)
    unicast_peer {
                  172.24.69.180  ##(对端IP地址)此地址一定不能忘记
                       }
    authentication
    {
        auth_type PASS
        auth_pass redis
     }
     virtual_ipaddress
     {
         172.24.69.185/24
     }
     track_script
     { ##调用状态检查
          chkmaster
     }
}

2、主从的脚本都一样状态检查脚本内容:

#!/bin/bash
STATUS=`/usr/local/bin/redis-cli -h $1 -p $2 -a $3 info | grep role:master | wc -l`
echo $STATUS
if [ "$STATUS" -eq 0 ];then    
    exit 1
else    
    exit 0
fi

3、给脚本权限

chmod -R 755 /etc/keepalived/chkmaster.sh

4、重启keepalived

systemctl daemon-reload
systemctl enable --now keepalived
systemctl stop keepalived     #关闭keepalived
systemctl start keepalived     #启动keepalived

5、验证redis是否正常

#登录redis
/usr/local/bin/redis-cli -h 127.0.0.1 -p 6379 -a pass123
#查看redis信息
/usr/local/bin/redis-cli -h 127.0.0.1 -p 6379 -a pass123 info

6、验证vip 是否可以漂移
查看此时vip 在哪台服务器上,手动停止vip所在服务器上的redis 容器,看vip 是否能漂移到另外一台服务器,如果能漂移,把停掉的服务重新启动,再把vip所在的服务器的redis 容器停掉,看vip是否能漂移到另外一台,如果可以就成功了。

通过查看keepalived的状态,看到优先级的变化
Redis一主一从Docker方式部署通过keepalived和 sentinel哨兵模式实现高可用_第1张图片

你可能感兴趣的:(linux,运维,redis)