redis集群和哨兵模式

2021/7/11 19:08:29

本文主要是介绍redis集群和哨兵模式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

redis集群和哨兵模式

redis集群

在这里插入图片描述

六个Redis实例的配置文件:

redis.conf

port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

/opt/redis
		/7000/redis.conf
		/7001/redis.conf
		/7002/redis.conf
		/7003/redis.conf
		/7004/redis.conf
		/7005/redis.conf

mkdir /opt/redis
cd /opt/redis
mkdir 7000 7001 7002 7003 7004 7005

cat <<EOF >7000/redis.conf
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

cat <<EOF >7001/redis.conf
port 7001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

cat <<EOF >7002/redis.conf
port 7002
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

cat <<EOF >7003/redis.conf
port 7003
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

cat <<EOF >7004/redis.conf
port 7004
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

cat <<EOF >7005/redis.conf
port 7005
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

先启动6个redis实例

docker run -d --name redis7000 \
-v /opt/redis/7000/redis.conf:/redis.conf \
--net=host \
--restart=always \
redis \
redis-server /redis.conf

docker run -d --name redis7001 \
-v /opt/redis/7001/redis.conf:/redis.conf \
--net=host \
--restart=always \
redis \
redis-server /redis.conf

docker run -d --name redis7002 \
-v /opt/redis/7002/redis.conf:/redis.conf \
--net=host \
--restart=always \
redis \
redis-server /redis.conf

docker run -d --name redis7003 \
-v /opt/redis/7003/redis.conf:/redis.conf \
--net=host \
--restart=always \
redis \
redis-server /redis.conf

docker run -d --name redis7004 \
-v /opt/redis/7004/redis.conf:/redis.conf \
--net=host \
--restart=always \
redis \
redis-server /redis.conf

docker run -d --name redis7005 \
-v /opt/redis/7005/redis.conf:/redis.conf \
--net=host \
--restart=always \
redis \
redis-server /redis.conf

执行命令配置集群

# 进入容器执行集群配置命令

docker exec -it redis7000 \
redis-cli --cluster create \
192.168.64.150:7000 192.168.64.150:7001 \
192.168.64.150:7002 192.168.64.150:7003 \
192.168.64.150:7004 192.168.64.150:7005 \
--cluster-replicas 1

查看集群信息

docker exec -it redis7000 redis-cli -c -p 7000

cluster info

cluster nodes

存数据

docker exec -it redis7000 redis-cli -c -p 7000
# 7002
set a aaaaaaaaaaaaa
# 7000
set b bbbbbbbbbbb
# 7001
set c cccccccccccccccc

redis哨兵

redis主从复制结构中,当主服务器宕机,哨兵可以监控到服务宕机,从从服务器中选举产生一个新的主服务器

1.搭建主从服务器 – 一主两从

docker rm -f $(docker ps -aq)

# 启动主服务器,正常启动,没有特殊设置
docker run -d --name redis6379 --net=host --restart=always redis 

docker exec -it redis6379 redis-cli
> info replication

启动两个从服务器

docker run -d --name redis6380 --net=host --restart=always redis \
redis-server --port 6380 --slaveof 192.168.64.150 6379


docker run -d --name redis6381 --net=host --restart=always redis \
redis-server --port 6381 --slaveof 192.168.64.150 6379

docker exec -it redis6379 redis-cli
> info replication

docker exec -it redis6380 redis-cli -p 6380
> info replication

docker exec -it redis6381 redis-cli -p 6381
> info replication

2.添加哨兵配置文件

mkdir /opt/sentinel/
cd /opt/sentinel/

# sentinel monitor mymaster 192.168.64.150 6379 2
# 末尾的 2 表示两台从服务器投票确认主服务器宕机,哨兵才会认为主服务器宕机
cat <<EOF >5000.conf
port 5000
sentinel monitor mymaster 192.168.64.150 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
EOF

cat <<EOF >5001.conf
port 5001
sentinel monitor mymaster 192.168.64.150 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
EOF

cat <<EOF >5002.conf
port 5002
sentinel monitor mymaster 192.168.64.150 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
EOF

3.启动三个哨兵

docker run -d --name sentinel5000 \
-v /opt/sentinel/5000.conf:/sentinel.conf \
--net=host \
redis redis-sentinel /sentinel.conf

docker run -d --name sentinel5001 \
-v /opt/sentinel/5001.conf:/sentinel.conf \
--net=host \
redis redis-sentinel /sentinel.conf

docker run -d --name sentinel5002 \
-v /opt/sentinel/5002.conf:/sentinel.conf \
--net=host \
redis redis-sentinel /sentinel.conf

# 进入一个哨兵容器,查看它监控的主从服务器和其他哨兵
docker exec -it sentinel5000 redis-cli -p 5000
> sentinel master mymaster
> sentinel slaves mymaster
> sentinel sentinels mymaster

4.停止主服务器,测试主服务器重新选举

# 停止主服务器
docker stop redis6379
# 在哨兵日志中查看服务器切换日志: +switch-master mymaster 192.168.64.150 6379 192.168.64.150 6381
docker logs sentinel5000

# 查看 6380 和 6381 服务器的角色变化
docker exec -it redis6380 redis-cli -p 6380
> info replication

docker exec -it redis6381 redis-cli -p 6381
> info replication

重新启动6379,不会把6379切换成主服务器,而是作为从服务器

docker start redis6379

docker exec -it redis6379 redis-cli
> info replication

5.客户端api连接哨兵存取数据



这篇关于redis集群和哨兵模式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程