redis Cluster模式集群 多机器 docker 部署
参考
https://www.jianshu.com/p/339549138e07
mkdir -p /data/redis-cluster && cd /data/redis-cluster
编辑 master结点配置文件 redis-master.conf
port 6379 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes daemonize no protected-mode no # requirepass 123456 # masterauth 123456 pidfile /var/run/redis.pid cluster-announce-ip 192.168.3.201
cluster-config-file: 集群配置文件(自动创建)
cluster-node-timeout: 集群超时时间(毫秒)
cluster-announce-ip: 集群节点 IP,如果想要集群可以供外网访问,这里直接填宿主机的IP
port 6380 cluster-enabled no appendonly yes daemonize no protected-mode no pidfile /var/run/redis.pid slaveof 192.168.3.202 6379
slaveof 192.168.3.203 7001: 直接指定成为对应master的从节点
docker run -d --privileged=true --restart always -p 6379:6379 -p 16379:16379 \ -v /data/redis-cluster/master-1/:/etc/redis/ \ -v /data/redis-cluster/master-1/:/data \ --name redis-master-1 redis:7.0 redis-server /etc/redis/redis-master.conf --appendonly yes
docker run -d --privileged=true --restart always -p 6380:6380 \ -v /data/redis-cluster/slave-2/:/etc/redis/ \ -v /data/redis-cluster/slave-2/:/data \ --name redis-slave-2 redis:7.0 redis-server /etc/redis/redis-slave.conf --appendonly yes
docker run -d --privileged=true --restart always -p 6379:6379 -p 16379:16379 \ -v /data/redis-cluster/master-2/:/etc/redis/ \ -v /data/redis-cluster/master-2/:/data \ --name redis-master-2 redis:7.0 redis-server /etc/redis/redis-master.conf --appendonly yes docker run -d --privileged=true --restart always -p 6380:6380 \ -v /data/redis-cluster/slave-3/:/etc/redis/ \ -v /data/redis-cluster/slave-3/:/data \ --name redis-slave-3 redis:7.0 redis-server /etc/redis/redis-slave.conf --appendonly yes
docker run -d --privileged=true --restart always -p 6379:6379 -p 16379:16379 \ -v /data/redis-cluster/master-3/:/etc/redis/ \ -v /data/redis-cluster/master-3/:/data \ --name redis-master-3 redis:7.0 redis-server /etc/redis/redis-master.conf --appendonly yes docker run -d --privileged=true --restart always -p 6380:6380 \ -v /data/redis-cluster/slave-1/:/etc/redis/ \ -v /data/redis-cluster/slave-1/:/data \ --name redis-slave-1 redis:7.0 redis-server /etc/redis/redis-slave.conf --appendonly yes
注意3个主结点的容器,需要同时映射6379端口和16379端口。每个Redis Cluster节点都需要打开两个TCP连接。用于服务客户端的普通Redis TCP端口(如6379),以及数据端口加上10000得到的端口(例如16379)。第二个高端口用于集群总线,是使用二进制协议的节点到节点通信通道。节点使用集群总线进行故障检测、配置更新、故障转移授权等。
此时仅仅创建了三对主从节点,但是三个主节点(7001, 7002, 7003)是孤立的,并且还未分配slots。
docker exec -it redis-master-1 /bin/bash
redis-cli --cluster create 192.168.3.201:6379 192.168.3.202:6379 192.168.3.203:6379 --cluster-replicas 0 redis-cli -a 123456 --cluster create 192.168.2.201:6379 192.168.2.202:6379 192.168.2.203:6379 --cluster-replicas 0
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.100.101:7001) M: f936070976e483985e9fa0ac904268e952e4bae3 192.168.100.101:7001 slots:[0-5460] (5461 slots) master M: 9e6435cc51704ef543e60c186fec4e38277c3a88 192.168.2.240:7003 slots:[10923-16383] (5461 slots) master M: e4f2be7cb9321058e84bc482db0c59b9d1213faa 192.168.2.240:7002 slots:[5461-10922] (5462 slots) master [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
redis-cli -h 192.168.100.101 -p 7001 -c
有密码加 -a 密码
192.168.100.101:7001> set k1 v1
如果计算出来这个 key的 slot不在当前结点,则会提示:
-> Redirected to slot [12706] located at 192.168.242.93:7003 OK
表示通过计算 slot分配到 192.168.242.93:7003结点去了。
我们通过普通模式连接到主结点 192.168.242.93:7003和从结点 192.168.100.106:6379,都能查到这个 k1