Redis Cluster基于Docker的集群搭建
摘要:Docker
搭建 Redis Cluster
,解决单机挂掉导致服务不可用的情况,并通过 docker-compose
进行部署容器停止可以自动重启;本文在单机上模拟了6个 Redis
服务,实际情况部署则需要至少6台机器才能保证服务高可用。
Redis Cluster
介绍
Redis Cluster
要求至少需要3个master才能组成一个集群,同时每个master至少需要有一个slave节点。各个节点之间保持TCP通信。当master发生了宕机, Redis Cluster自动会将对应的slave节点提拔为master,来重新对外提供服务。Redis Cluster
功能 : 负载均衡,故障切换,主从复制 先说下槽,集群中每个redis实例都负责接管一部分槽,总槽数为:16384(2^14),如果有3台master,那么每台负责5461个槽(16384/3) 当redis客户端设置值时,会拿key进行CRC16算法,然后 跟16384取模,得到的就是落在哪个槽位,根据上面表格就得出在哪台节点上。槽公式如下: slot = CRC16(key) & 16383
环境准备
docker
安装参考该文章 juejin.cn/post/696827…
搭建
本机的
ip 192.168.137.180
,后面都用该IP
编写配置文件redis.conf
redis.conf
port 6371
requirepass 1234
masterauth 1234
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 192.168.137.180
cluster-announce-port 6371
cluster-announce-bus-port 16371
由于我是在本机配置了6个服务,所以这里的配置文件要复制6份出来,每一份的端口对应修改一下
- 或者采用批量创建模版
redis-cluster.tmpl
port ${PORT}
requirepass 1234
masterauth 1234
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 192.168.137.180
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
- 执行下面脚本
for port in `seq 6371 6376`; do \
mkdir -p ${port}/conf \
&& PORT=${port} envsubst < redis-cluster.tmpl > ${port}/conf/redis.conf \
&& mkdir -p ${port}/data;\
done
- 配置说明
port:节点端口;
requirepass:添加访问认证;
masterauth:如果主节点开启了访问认证,从节点访问主节点需要认证;
protected-mode:保护模式,默认值 yes,即开启。开启保护模式以后,需配置 bind ip 或者设置访问密码;关闭保护模式,外部网络可以直接访问;
daemonize:是否以守护线程的方式启动(后台启动),默认 no;
appendonly:是否开启 AOF 持久化模式,默认 no;
cluster-enabled:是否开启集群模式,默认 no;
cluster-config-file:集群节点信息文件;
cluster-node-timeout:集群节点连接超时时间;
cluster-announce-ip:集群节点 IP,填写宿主机的 IP;
cluster-announce-port:集群节点映射端口;
cluster-announce-bus-port:集群节点总线端口。
每个 Redis 集群节点都需要打开两个 TCP 连接。一个用于为客户端提供服务的正常 Redis TCP 端口,例如 6379。还有一个基于 6379 端口加 10000 的端口,比如 16379。 第二个端口用于集群总线,这是一个使用二进制协议的节点到节点通信通道。节点使用集群总线进行故障检测、配置更新、故障转移授权等等。客户端永远不要尝试与集群总线端口通信,与正常的 Redis 命令端口通信即可,但是请确保防火墙中的这两个端口都已经打开,否则 Redis 集群节点将无法通信。
docker-compose.yml
编写
我本机采用的是
bridge
网络,并且提前创建了,所以最好你也提前创建一个network
version: '3'
services:
redis-cluster-01:
image: redis:7.0.0
container_name: redis-cluster-01
environment:
TZ: Asia/Shanghai
networks:
- default
ports:
- 6371:6371
- 16371:16371
command: ["redis-server","/etc/redis/redis.conf"]
volumes:
- ./6371/conf:/etc/redis/
- ./6371/data:/data
redis-cluster-02:
image: redis:7.0.0
container_name: redis-cluster-02
environment:
TZ: Asia/Shanghai
networks:
- default
ports:
- 6372:6372
- 16372:16372
command: ["redis-server","/etc/redis/redis.conf"]
volumes:
- ./6372/conf:/etc/redis/
- ./6372/data:/data
redis-cluster-03:
image: redis:7.0.0
container_name: redis-cluster-03
environment:
TZ: Asia/Shanghai
networks:
- default
ports:
- 6373:6373
- 16373:16373
command: ["redis-server","/etc/redis/redis.conf"]
volumes:
- ./6373/conf:/etc/redis/
- ./6373/data:/data
redis-cluster-04:
image: redis:7.0.0
container_name: redis-cluster-04
environment:
TZ: Asia/Shanghai
networks:
- default
ports:
- 6374:6374
- 16374:16374
command: ["redis-server","/etc/redis/redis.conf"]
volumes:
- ./6374/conf:/etc/redis/
- ./6374/data:/data
redis-cluster-05:
image: redis:7.0.0
container_name: redis-cluster-05
environment:
TZ: Asia/Shanghai
networks:
- default
ports:
- 6375:6375
- 16375:16375
command: ["redis-server","/etc/redis/redis.conf"]
volumes:
- ./6375/conf:/etc/redis/
- ./6375/data:/data
redis-cluster-06:
image: redis:7.0.0
container_name: redis-cluster-06
environment:
TZ: Asia/Shanghai
networks:
- default
ports:
- 6376:6376
- 16376:16376
command: ["redis-server","/etc/redis/redis.conf"]
volumes:
- ./6376/conf:/etc/redis/
- ./6376/data:/data
networks:
default:
external:
name: huzhihui
-
我的文件目录如下
-
运行
docker-compose up -d
- 创建集群
# 进入容器
docker exec -it redis-cluster-01 /bin/sh
# 进入bin目录
cd /usr/local/bin
# 查看目录下可执行的文件
ls
docker-entrypoint.sh gosu redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server
输入下面命令创建集群(由于我们只有6个节点,所以3主3从,每个主节点只有一个副本)
redis-cli -a 1234 --cluster create 192.168.137.180:6371 192.168.137.180:6372 192.168.137.180:6373 192.168.137.180:6374 192.168.137.180:6375 192.168.137.180:6376 --cluster-replicas 1
显示如下信息则表示创建集群成功
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Could not connect to Redis at 192.168.10.11:6374: Connection refused
# redis-cli -a 1234 --cluster create 192.168.137.180:6371 192.168.137.180:6372 192.168.137.180:6373 192.168.137.180:6374 192.168.137.180:6375 192.168.137.180:6376 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.137.180:6375 to 192.168.137.180:6371
Adding replica 192.168.137.180:6376 to 192.168.137.180:6372
Adding replica 192.168.137.180:6374 to 192.168.137.180:6373
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 3a5d90221ad36073bd529afe5f45935a0dde133b 192.168.137.180:6371
slots:[0-5460] (5461 slots) master
M: 9cc41e739c71e93dfd6a72330045b2307f6582bb 192.168.137.180:6372
slots:[5461-10922] (5462 slots) master
M: 08798e7ec09a19fb201bf8b8915d75824e15511a 192.168.137.180:6373
slots:[10923-16383] (5461 slots) master
S: 804b4960a3e55fbda8bffff672102a0ac017414e 192.168.137.180:6374
replicates 9cc41e739c71e93dfd6a72330045b2307f6582bb
S: d1d876b6e2bf02b5dbc752f18d461bac1330c352 192.168.137.180:6375
replicates 08798e7ec09a19fb201bf8b8915d75824e15511a
S: bcf9616f4aac0b431940cfdac0c62d0fcf4c81d3 192.168.137.180:6376
replicates 3a5d90221ad36073bd529afe5f45935a0dde133b
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 192.168.137.180:6371)
M: 3a5d90221ad36073bd529afe5f45935a0dde133b 192.168.137.180:6371
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 9cc41e739c71e93dfd6a72330045b2307f6582bb 192.168.137.180:6372
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: d1d876b6e2bf02b5dbc752f18d461bac1330c352 192.168.137.180:6375
slots: (0 slots) slave
replicates 08798e7ec09a19fb201bf8b8915d75824e15511a
S: 804b4960a3e55fbda8bffff672102a0ac017414e 192.168.137.180:6374
slots: (0 slots) slave
replicates 9cc41e739c71e93dfd6a72330045b2307f6582bb
M: 08798e7ec09a19fb201bf8b8915d75824e15511a 192.168.137.180:6373
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: bcf9616f4aac0b431940cfdac0c62d0fcf4c81d3 192.168.137.180:6376
slots: (0 slots) slave
replicates 3a5d90221ad36073bd529afe5f45935a0dde133b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
- 检查集群
redis-cli -a 1234 --cluster check 192.168.137.180:6371
- 登陆到集群
redis-cli -c -a 1234 -h 192.168.137.180 -p 6376
- 查看集群状态
集群状态:
cluster info
;集群节点查询:cluster node
192.168.137.180:6376> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:107
cluster_stats_messages_pong_sent:111
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:219
cluster_stats_messages_ping_received:111
cluster_stats_messages_pong_received:108
cluster_stats_messages_received:219
total_cluster_links_buffer_limit_exceeded:0
192.168.137.180:6376> cluster node
(error) ERR unknown subcommand 'node'. Try CLUSTER HELP.
192.168.137.180:6376> cluster nodes
d1d876b6e2bf02b5dbc752f18d461bac1330c352 192.168.137.180:6375@16375 slave 08798e7ec09a19fb201bf8b8915d75824e15511a 0 1652065682000 3 connected
9cc41e739c71e93dfd6a72330045b2307f6582bb 192.168.137.180:6372@16372 master - 0 1652065684048 2 connected 5461-10922
bcf9616f4aac0b431940cfdac0c62d0fcf4c81d3 192.168.137.180:6376@16376 myself,slave 3a5d90221ad36073bd529afe5f45935a0dde133b 0 1652065684000 1 connected
804b4960a3e55fbda8bffff672102a0ac017414e 192.168.137.180:6374@16374 slave 9cc41e739c71e93dfd6a72330045b2307f6582bb 0 1652065683000 2 connected
08798e7ec09a19fb201bf8b8915d75824e15511a 192.168.137.180:6373@16373 master - 0 1652065685062 3 connected 10923-16383
3a5d90221ad36073bd529afe5f45935a0dde133b 192.168.137.180:6371@16371 master - 0 1652065684000 1 connected 0-5460
192.168.137.180:6376> get wx:user:u001
-> Redirected to slot [7808] located at 192.168.137.180:6372
使用客户端连接集群
转载自:https://juejin.cn/post/7095675331696132127