[redis] linux下集群篇(4) docker部署

一.环境说明

服务端:

    192.168.0.108 CentOS Linux release 7.5.1804 (Core)

    采用docker部署,选择redis5.0版本,该版本起redis-cli命令自带了创建集群功能,之前版本使用redis-trib.rb。

客户端:

    192.168.0.95

二.服务端部署

1.安装docker(略)

2.准备模板文件

masterauth和requirepass按需设置

[root@XM-v108 ~]# cat /data/conf/redis-cluster.tmpl 
port ${PORT}
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
cluster-announce-ip 192.168.0.108
masterauth pass
requirepass pass

3.生成各个节点的配置文件

[root@XM-v108 /data/conf]# cd /data/conf
[root@XM-v108 /data/conf]# mkdir redis-cluster
[root@XM-v108 /data/conf]# for port in `seq 6381 6386`; do PORT=$port envsubst < redis-cluster.tmpl > redis-cluster/redis_$port.conf; done
[root@XM-v108 /data/conf]# ll redis-cluster
total 24
-rw-r--r-- 1 root root 177 Aug 28 09:53 redis_6381.conf
-rw-r--r-- 1 root root 177 Aug 28 09:53 redis_6382.conf
-rw-r--r-- 1 root root 177 Aug 28 09:53 redis_6383.conf
-rw-r--r-- 1 root root 177 Aug 28 09:53 redis_6384.conf
-rw-r--r-- 1 root root 177 Aug 28 09:53 redis_6385.conf
-rw-r--r-- 1 root root 177 Aug 28 09:53 redis_6386.conf

4.启动实例,创建集群

[root@XM-v108 /data/conf]# docker network create redis-cluster
[root@XM-v108 /data/conf]# docker pull redis:5.0
[root@XM-v108 /data/conf]# for port in `seq 6381 6386`; do 
docker run -d -ti -p ${port}:${port} -p 1${port}:1${port} \
-v /data/conf/redis-cluster/redis_$port.conf:/etc/redis.conf \
--restart always --name redis_${port} --net redis-cluster redis:5.0 redis-server /etc/redis.conf; \
done
[root@XM-v108 /data/conf]# docker exec -ti redis_6381 bash
root@58f9142e6f0c:/data# redis-cli --cluster create 192.168.0.108:6381 192.168.0.108:6382 192.168.0.108:6383 192.168.0.108:6384 192.168.0.108:6385 192.168.0.108:6386 --cluster-replicas 1 -a pass
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.0.108:6385 to 192.168.0.108:6381
Adding replica 192.168.0.108:6386 to 192.168.0.108:6382
Adding replica 192.168.0.108:6384 to 192.168.0.108:6383
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 17a0a2b71365497ba0a0ed1adef842c684d9f97b 192.168.0.108:6381
   slots:[0-5460] (5461 slots) master
M: 18091272ef718023a6261cfd5c926c2d2273e548 192.168.0.108:6382
   slots:[5461-10922] (5462 slots) master
M: 14e3c882337a1aa71b08e8d0f4b4832bef8e077b 192.168.0.108:6383
   slots:[10923-16383] (5461 slots) master
S: 2f9a6a6d1f01289421325a5c2179fee0d4cb1d04 192.168.0.108:6384
   replicates 14e3c882337a1aa71b08e8d0f4b4832bef8e077b
S: b0061c450c434acbb72a38112196d510b1b2c0fc 192.168.0.108:6385
   replicates 17a0a2b71365497ba0a0ed1adef842c684d9f97b
S: b6b2d75a589b2c526712c97ab350968747716ff2 192.168.0.108:6386
   replicates 18091272ef718023a6261cfd5c926c2d2273e548
Can I set the above configuration? (type 'yes' to accept): yes(输入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.0.108:6381)
M: 17a0a2b71365497ba0a0ed1adef842c684d9f97b 192.168.0.108:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 2f9a6a6d1f01289421325a5c2179fee0d4cb1d04 192.168.0.108:6384
   slots: (0 slots) slave
   replicates 14e3c882337a1aa71b08e8d0f4b4832bef8e077b
S: b0061c450c434acbb72a38112196d510b1b2c0fc 192.168.0.108:6385
   slots: (0 slots) slave
   replicates 17a0a2b71365497ba0a0ed1adef842c684d9f97b
S: b6b2d75a589b2c526712c97ab350968747716ff2 192.168.0.108:6386
   slots: (0 slots) slave
   replicates 18091272ef718023a6261cfd5c926c2d2273e548
M: 14e3c882337a1aa71b08e8d0f4b4832bef8e077b 192.168.0.108:6383
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 18091272ef718023a6261cfd5c926c2d2273e548 192.168.0.108:6382
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

5.客户端连接

# redis-cli -c -h 192.168.0.108 -p 6381 -a pass

redis-cli --cluster create命令的前三个节点会被设置为主节点,客户端连接任一主节点即可(连接副节点貌似也可以),不需要在连接地址中填写全部的节点地址,也不需要指定全部的主节点,通信过程中会自动重定向主节点。

posted @ 2020-08-28 10:38  金枪语  阅读(339)  评论(0编辑  收藏  举报