实战 Redis集群

Posted pigeast

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实战 Redis集群相关的知识,希望对你有一定的参考价值。

实战Redis集群

# 创建一个自定义的网络名词叫redis
docker network create --subnet 172.18.0.0/16 --gateway 172.18.0.1 redis

# 宿主机挂载文件
for port in $(seq 1 6);
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 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.18.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done


docker run -p 6371:6379 --name redis-1 -v /mydata/redis/node-1/data:/data -v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf --net redis --ip 172.18.0.11 -d redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

docker run -p 6372:6379 --name redis-2 -v /mydata/redis/node-2/data:/data -v /mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf --net redis --ip 172.18.0.12 -d redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

docker run -p 6373:6379 --name redis-3 -v /mydata/redis/node-3/data:/data -v /mydata/redis/node-3/conf/redis.conf:/etc/redis/redis.conf --net redis --ip 172.18.0.13 -d redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

docker run -p 6374:6379 --name redis-4 -v /mydata/redis/node-4/data:/data -v /mydata/redis/node-4/conf/redis.conf:/etc/redis/redis.conf --net redis --ip 172.18.0.14 -d redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf


docker run -p 6375:6379 --name redis-5 -v /mydata/redis/node-5/data:/data -v /mydata/redis/node-5/conf/redis.conf:/etc/redis/redis.conf --net redis --ip 172.18.0.15 -d redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf


docker run -p 6376:6379 --name redis-6 -v /mydata/redis/node-6/data:/data -v /mydata/redis/node-6/conf/redis.conf:/etc/redis/redis.conf --net redis --ip 172.18.0.16 -d redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

# 进入一个容器,创建容器
docker exec -it redis-1 sh
redis-cli --cluster create 172.18.0.11:6379 172.18.0.12:6379 172.18.0.13:6379 172.18.0.14:6379 172.18.0.15:6379 172.18.0.16:6379 --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 172.18.0.15:6379 to 172.18.0.11:6379
Adding replica 172.18.0.16:6379 to 172.18.0.12:6379
Adding replica 172.18.0.14:6379 to 172.18.0.13:6379
M: 4eab0f1816f1f24f9bb4d4371080369e4e0fb123 172.18.0.11:6379
   slots:[0-5460] (5461 slots) master
M: f3e506918e48d1d27389701e83f72b3547fecccc 172.18.0.12:6379
   slots:[5461-10922] (5462 slots) master
M: a3b9bcf1fb06e81eabd325f8536d9a728c1d87ba 172.18.0.13:6379
   slots:[10923-16383] (5461 slots) master
S: f3f8898e5f02064d249c83e2596122a3af996859 172.18.0.14:6379
   replicates a3b9bcf1fb06e81eabd325f8536d9a728c1d87ba
S: 02ffb3aad77e45b4cc721c81b37524c6b54ccb7d 172.18.0.15:6379
   replicates 4eab0f1816f1f24f9bb4d4371080369e4e0fb123
S: c82bb6fb90080a061e88d9cfd7e7c9133e623aa5 172.18.0.16:6379
   replicates f3e506918e48d1d27389701e83f72b3547fecccc
Can I set the above configuration? (type ‘yes‘ to accept): 

Waiting for the cluster to join
.....
>>> Performing Cluster Check (using node 172.18.0.11:6379)
M: 4eab0f1816f1f24f9bb4d4371080369e4e0fb123 172.18.0.11:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: f3f8898e5f02064d249c83e2596122a3af996859 172.18.0.14:6379
   slots: (0 slots) slave
   replicates a3b9bcf1fb06e81eabd325f8536d9a728c1d87ba
M: a3b9bcf1fb06e81eabd325f8536d9a728c1d87ba 172.18.0.13:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: c82bb6fb90080a061e88d9cfd7e7c9133e623aa5 172.18.0.16:6379
   slots: (0 slots) slave
   replicates f3e506918e48d1d27389701e83f72b3547fecccc
S: 02ffb3aad77e45b4cc721c81b37524c6b54ccb7d 172.18.0.15:6379
   slots: (0 slots) slave
   replicates 4eab0f1816f1f24f9bb4d4371080369e4e0fb123
M: f3e506918e48d1d27389701e83f72b3547fecccc 172.18.0.12:6379
   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.

# 查看集群
/data # redis-cli -c
127.0.0.1:6379> 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:331
cluster_stats_messages_pong_sent:334
cluster_stats_messages_sent:665
cluster_stats_messages_ping_received:329
cluster_stats_messages_pong_received:331
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:665

# 查看nodes
127.0.0.1:6379> cluster nodes
f3f8898e5f02064d249c83e2596122a3af996859 172.18.0.14:6379@16379 slave a3b9bcf1fb06e81eabd325f8536d9a728c1d87ba 0 1603122027848 4 connected
a3b9bcf1fb06e81eabd325f8536d9a728c1d87ba 172.18.0.13:6379@16379 master - 0 1603122027645 3 connected 10923-16383
c82bb6fb90080a061e88d9cfd7e7c9133e623aa5 172.18.0.16:6379@16379 slave f3e506918e48d1d27389701e83f72b3547fecccc 0 1603122026839 6 connected
4eab0f1816f1f24f9bb4d4371080369e4e0fb123 172.18.0.11:6379@16379 myself,master - 0 1603122027000 1 connected 0-5460
02ffb3aad77e45b4cc721c81b37524c6b54ccb7d 172.18.0.15:6379@16379 slave 4eab0f1816f1f24f9bb4d4371080369e4e0fb123 0 1603122027545 5 connected
f3e506918e48d1d27389701e83f72b3547fecccc 172.18.0.12:6379@16379 master - 0 1603122027000 2 connected 5461-10922

# 测试当我们停掉一个主节点,会看到slave马上起来,自己成为一个master
172.18.0.12:6379> get a
-> Redirected to slot [15495] located at 172.18.0.14:6379
"b"
172.18.0.14:6379> cluster nodes
a3b9bcf1fb06e81eabd325f8536d9a728c1d87ba 172.18.0.13:6379@16379 **master,fail **- 1603123412437 1603123410319 3 connected
c82bb6fb90080a061e88d9cfd7e7c9133e623aa5 172.18.0.16:6379@16379 slave f3e506918e48d1d27389701e83f72b3547fecccc 0 1603123455562 6 connected
02ffb3aad77e45b4cc721c81b37524c6b54ccb7d 172.18.0.15:6379@16379 slave 4eab0f1816f1f24f9bb4d4371080369e4e0fb123 0 1603123455763 5 connected
f3f8898e5f02064d249c83e2596122a3af996859 **172.18.0.14:6379@16379 myself,master** - 0 1603123454000 7 connected 10923-16383
4eab0f1816f1f24f9bb4d4371080369e4e0fb123 172.18.0.11:6379@16379 master - 0 1603123455000 1 connected 0-5460
f3e506918e48d1d27389701e83f72b3547fecccc 172.18.0.12:6379@16379 master - 0 1603123454756 2 connected 5461-10922

# 我们使用docker 之后,集群就简单好玩多了

以上是关于实战 Redis集群的主要内容,如果未能解决你的问题,请参考以下文章

如何利用redis来进行分布式集群系统的限流设计

实战 Redis集群

docker部署redis集群(实战)

Redis实战-Redis集群环境搭建(自动搭建)

Redis实战-Redis集群环境搭建(自动搭建)

Redis 3.2.4集群实战