docker安装Redis集群

规划

安装redis-cluster;3主3从方式,从为了同步备份,主进行slot数据分片。

配置

编辑运行多个redis容器脚本文件
# vim redis-cluster.sh
# cat redis-cluster.sh
for port in $(seq 8001 8006); \
do \
mkdir -p /mydata/redis/node-${port}/conf
touch /mydata/redis/node-${port}/conf/redis.conf
cat << EOF >/mydata/redis/node-${port}/conf/redis.conf
port ${port}
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 192.168.33.12
cluster-announce-port ${port}
cluster-announce-bus-port 1${port}
appendonly yes
EOF
docker run -p ${port}:${port} -p 1${port}:1${port} --name redis-${port} \
-v /mydata/redis/node-${port}/data:/data \
-v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d redis:5.0.7 redis-server /etc/redis/redis.conf; \
done

安装

# 启动创建
sh redis-cluster.sh
# 查看
docker ps
# 登录redis容器
docker exec -it redis-18001 bash
# 创建redis-cluster
redis-cli --cluster create 192.168.33                                                                         .11:8001 192.168.33.11:8002 192.168.33.11:8003 192.168.33.11:8                                                                         004 192.168.33.11:8005 192.168.33.11:8006 --cluster-replicas 1 

>>> 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.33.11:8005 to 192.168.33.11:8001
Adding replica 192.168.33.11:8006 to 192.168.33.11:8002
Adding replica 192.168.33.11:8004 to 192.168.33.11:8003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 7e3c1f4daa1750c37935d354b3ce9076d26d94c5 192.168.33.11:8001
   slots:[0-5460] (5461 slots) master
M: b972f261b0a09a7f43f3e527d7a5242a94a80af0 192.168.33.11:8002
   slots:[5461-10922] (5462 slots) master
M: 5a3e0a9d57e57c8b7f663b76109d347b5c4a5a40 192.168.33.11:8003
   slots:[10923-16383] (5461 slots) master
S: 88477a4f561fb158763e01d03d9f429886cc432b 192.168.33.11:8004
   replicates 5a3e0a9d57e57c8b7f663b76109d347b5c4a5a40
S: 22ab4bb185e8ab807f8ffbae27a933c3ae14788a 192.168.33.11:8005
   replicates 7e3c1f4daa1750c37935d354b3ce9076d26d94c5
S: efe10d6de7a2f851457999b3c8b6a112c8e4fba6 192.168.33.11:8006
   replicates b972f261b0a09a7f43f3e527d7a5242a94a80af0
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.33.11:8001)
M: 7e3c1f4daa1750c37935d354b3ce9076d26d94c5 192.168.33.11:8001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 22ab4bb185e8ab807f8ffbae27a933c3ae14788a 192.168.33.11:8005
   slots: (0 slots) slave
   replicates 7e3c1f4daa1750c37935d354b3ce9076d26d94c5
S: 88477a4f561fb158763e01d03d9f429886cc432b 192.168.33.11:8004
   slots: (0 slots) slave
   replicates 5a3e0a9d57e57c8b7f663b76109d347b5c4a5a40
M: b972f261b0a09a7f43f3e527d7a5242a94a80af0 192.168.33.11:8002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: efe10d6de7a2f851457999b3c8b6a112c8e4fba6 192.168.33.11:8006
   slots: (0 slots) slave
   replicates b972f261b0a09a7f43f3e527d7a5242a94a80af0
M: 5a3e0a9d57e57c8b7f663b76109d347b5c4a5a40 192.168.33.11:8003
   slots:[10923-16383] (5461 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.

使用


[root@localhost ~]# redis-cli -h 192.168.33.11 -p 8001 -c
192.168.33.11:8001> set k1 v1
-> Redirected to slot [12706] located at 192.168.33.11:8003
OK
192.168.33.11:8003> get k1
"v1"
192.168.33.11:8003> exit
[root@localhost ~]# redis-cli -h 192.168.33.11 -p 8002 -c
192.168.33.11:8002> get k1
-> Redirected to slot [12706] located at 192.168.33.11:8003
"v1"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值