1哨兵的作用
哨兵是redis集群架构中非常重要的一个组件,主要功能如下:
1. 集群监控:负责监控redis master和slave进程是否正常工作
2. 消息通知:如果某个redis实例有故障,那么哨兵负责发送消息作为报警通知给管理员
3. 故障转移:如果master node挂掉了,会自动转移到slave node上
4. 配置中心:如果故障转移发生了,通知client客户端新的master地址
原文链接:https://blog.csdn.net/wang258533488/article/details/79352378
23台服务器部署及规划
主节点服务器:10.0.0.101
从节点服务器1:10.0.0.102
从节点服务器2:10.0.0.103
节点规划:
哨兵节点:26379
redis节点:6379
10.0.0.101:
cat /opt/redis_6379/conf/redis_6379.conf
daemonize yes
bind 127.0.0.1 10.0.0.101
port 6379
pidfile /opt/redis_6379/pid/redis_6379.pid
logfile /opt/redis_6379/logs/redis_6379.log
save 900 1
save 300 10
save 60 10000
dbfilename redis.rdb
dir /data/redis_6379/
appendonly yes
appendfilename "redis.aof"
appendfsync everysec
10.0.0.102:
ssh-keygen
ssh-copy-id 10.0.0.101
pkill redis
rm -rf /opt/redis*
rsync -avz 10.0.0.101:/usr/local/bin/redis-* /usr/local/bin
rsync -avz 10.0.0.101:/usr/lib/systemd/system/redis.service /usr/lib/systemd/system/
mkdir /opt/redis_6379/{conf,logs,pid} -p
mkdir /data/redis_6379 -p
cat >/opt/redis_6379/conf/redis_6379.conf <<EOF
daemonize yes
bind 127.0.0.1 $(ifconfig eth0|awk ‘NR==2{print $2}‘)
port 6379
pidfile "/opt/redis_6379/pid/redis_6379.pid"
logfile "/opt/redis_6379/logs/redis_6379.log"
dbfilename "redis.rdb"
dir "/data/redis_6379"
appendonly yes
appendfilename "redis.aof"
appendfsync everysec
EOF
useradd redis -M -s /sbin/nologin
chown -R redis:redis /opt/redis*
chown -R redis:redis /data/redis*
systemctl daemon-reload
systemctl start redis
redis-cli
10.0.0.103:
ssh-keygen
ssh-copy-id 10.0.0.101
pkill redis
rm -rf /opt/redis*
rsync -avz 10.0.0.101:/usr/local/bin/redis-* /usr/local/bin
rsync -avz 10.0.0.101:/usr/lib/systemd/system/redis.service /usr/lib/systemd/system/
mkdir /opt/redis_6379/{conf,logs,pid} -p
mkdir /data/redis_6379 -p
cat >/opt/redis_6379/conf/redis_6379.conf <<EOF
daemonize yes
bind 127.0.0.1 $(ifconfig eth0|awk ‘NR==2{print $2}‘)
port 6379
pidfile "/opt/redis_6379/pid/redis_6379.pid"
logfile "/opt/redis_6379/logs/redis_6379.log"
dbfilename "redis.rdb"
dir "/data/redis_6379"
appendonly yes
appendfilename "redis.aof"
appendfsync everysec
EOF
useradd redis -M -s /sbin/nologin
chown -R redis:redis /opt/redis*
chown -R redis:redis /data/redis*
systemctl daemon-reload
systemctl start redis
redis-cli
3配置主从复制(命令在任何一个节点服务器上操作都可以)
redis-cli -h 10.0.0.102 slaveof 10.0.0.101 6379
redis-cli -h 10.0.0.103 slaveof 10.0.0.101 6379
redis-cli -h 10.0.0.101 info replication
4哨兵部署(3台服务器同样配置)
mkdir -p /data/redis_26379
mkdir -p /opt/redis_26379/{conf,pid,logs}
cat >/opt/redis_26379/conf/redis_26379.conf << EOF
bind $(ifconfig eth0|awk ‘NR==2{print $2}‘)
port 26379
daemonize yes
logfile /opt/redis_26379/logs/redis_26379.log
dir /data/redis_26379
sentinel monitor myredis 10.0.0.101 6379 2
sentinel down-after-milliseconds myredis 3000
sentinel parallel-syncs myredis 1
sentinel failover-timeout myredis 18000
EOF
chown -R redis:redis /data/redis*
chown -R redis:redis /opt/redis*
cat >/usr/lib/systemd/system/redis-sentinel.service<<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/bin/redis-sentinel /opt/redis_26379/conf/redis_26379.conf --supervised systemd
ExecStop=/usr/local/bin/redis-cli -h $(ifconfig eth0|awk ‘NR==2{print $2}‘) -p 26379 shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start redis-sentinel
关键配置解释:
sentinel monitor myredis 10.0.0.101 6379 2
# myredis主节点别名 主节点IP 端口 需要2个哨兵节点同意
sentinel down-after-milliseconds myredis 3000
# 认定服务器已经断线所需要的毫秒数
sentinel parallel-syncs myredis 1
# 向主节点发送复制操作的从节点个数,1表示轮训发起复制
sentinel failover-timeout myredis 18000
# 故障转移超时时间
参考文档:https://www.cnblogs.com/heroinss/p/10340925.html
5注意事项
1.哨兵发起故障转移的条件是master节点失去联系,从节点挂掉不会发起故障转移
2.哨兵会自己维护配置文件,不需要手动修改
3.如果主从的结构发生变化,哨兵之间会自动同步最新的消息并且自动更新配置文件
4.哨兵启动完成之后,以后不要再自己去设置主从关系
6验证主机点
[root@oldboyedu ~]# redis-cli -h 10.0.0.101 -p 26379 sentinel get-master-addr-by-name myredis
1) "10.0.0.101"
2) "6379"
7哨兵常用命令
#获取当前master地址
redis-cli -h 10.0.0.101 -p 26379 SENTINEL get-master-addr-by-name myredis
#查看master状态
redis-cli -h 10.0.0.101 -p 26379 SENTINEL master myredis
redis-cli -h 10.0.0.101 -p 26379 SENTINEL slaves myredis
redis-cli -h 10.0.0.101 -p 26379 SENTINEL ckquorum myredis