如何在服务器用docker搭建Redis集群

用docker部署Redis集群

这里用的是分片+高可用+负载均衡,三主三从

如何在服务器用docker搭建Redis集群_第1张图片

第一步创建网卡

# 创建网卡
docker network create redis --subnet 172.38.0.0/16

第二步用shell脚本配置(一个一个或者用shell脚本创建都行,建议shell脚本!)

# 通过脚本创建六个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.38.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done

# 用shell脚本创建六个节点 
docker run -p637${port}:6379 -p 1637${port}:16379 --name redis-${port} \
-v /mydata/redis/node-${port}/data:/data \
-v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf; \

# 创建结点1
docker run -p 6371:6379 -p 16371:16379 --name redis-1 \
-v /mydata/redis/node-1/data:/data \
-v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
 
#创建结点2
docker run -p 6372:6379 -p 16372:16379 --name redis-2 \
-v /mydata/redis/node-2/data:/data \
-v /mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.12 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

#创建结点3
docker run -p 6373:6379 -p 16373:16379 --name redis-3 \
-v /mydata/redis/node-3/data:/data \
-v /mydata/redis/node-3/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.13 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

#创建结点4
docker run -p 6374:6379 -p 16374:16379 --name redis-4 \
-v /mydata/redis/node-4/data:/data \
-v /mydata/redis/node-4/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.14 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

#创建结点5
docker run -p 6375:6379 -p 16375:16379 --name redis-5 \
-v /mydata/redis/node-5/data:/data \
-v /mydata/redis/node-5/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.15 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

#创建结点6
docker run -p 6376:6379 -p 16376:16379 --name redis -6 \
-v /mydata/redis/node-6/data:/data \
-v /mydata/redis/node-6/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.16 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

第三步我们创建Redis集群

# 创建集群
/data # redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16:6379 --cluster-rep
licas 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.38.0.15:6379 to 172.38.0.11:6379
Adding replica 172.38.0.16:6379 to 172.38.0.12:6379
Adding replica 172.38.0.14:6379 to 172.38.0.13:6379
M: 3fe0bd7d921ec3271f852908e2da13ee2f21b2f8 172.38.0.11:6379
   slots:[0-5460] (5461 slots) master
M: b32a9427e25a63375b9f9ea911e9af8a6ee2f54d 172.38.0.12:6379
   slots:[5461-10922] (5462 slots) master
M: 83e1c6e862c76eab9eccc6baf6dba46140d1f397 172.38.0.13:6379
   slots:[10923-16383] (5461 slots) master
S: c80aa76cc498770e242128ecf29155c6c77d1557 172.38.0.14:6379
   replicates 83e1c6e862c76eab9eccc6baf6dba46140d1f397
S: d188af2f802146515d0bff92729729448e8ad59e 172.38.0.15:6379
   replicates 3fe0bd7d921ec3271f852908e2da13ee2f21b2f8
S: d54b94a4d0e1fbd21375c2995cbfd4d2f3c3a4c7 172.38.0.16:6379
   replicates b32a9427e25a63375b9f9ea911e9af8a6ee2f54d
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 172.38.0.11:6379)
M: 3fe0bd7d921ec3271f852908e2da13ee2f21b2f8 172.38.0.11:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: d54b94a4d0e1fbd21375c2995cbfd4d2f3c3a4c7 172.38.0.16:6379
   slots: (0 slots) slave
   replicates b32a9427e25a63375b9f9ea911e9af8a6ee2f54d
S: d188af2f802146515d0bff92729729448e8ad59e 172.38.0.15:6379
   slots: (0 slots) slave
   replicates 3fe0bd7d921ec3271f852908e2da13ee2f21b2f8
M: 83e1c6e862c76eab9eccc6baf6dba46140d1f397 172.38.0.13:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: b32a9427e25a63375b9f9ea911e9af8a6ee2f54d 172.38.0.12:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: c80aa76cc498770e242128ecf29155c6c77d1557 172.38.0.14:6379
   slots: (0 slots) slave
   replicates 83e1c6e862c76eab9eccc6baf6dba46140d1f397
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

下面我们来测试一下

# 连接redis集群客户端
/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:93
cluster_stats_messages_pong_sent:99
cluster_stats_messages_sent:192
cluster_stats_messages_ping_received:94
cluster_stats_messages_pong_received:93
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:192

# 查看一下节点
127.0.0.1:6379> cluster nodes
d54b94a4d0e1fbd21375c2995cbfd4d2f3c3a4c7 172.38.0.16:6379@16379 slave b32a9427e25a63375b9f9ea911e9af8a6ee2f54d 0 1650767977591 6 connected
3fe0bd7d921ec3271f852908e2da13ee2f21b2f8 172.38.0.11:6379@16379 myself,master - 0 1650767977000 1 connected 0-5460
d188af2f802146515d0bff92729729448e8ad59e 172.38.0.15:6379@16379 slave 3fe0bd7d921ec3271f852908e2da13ee2f21b2f8 0 1650767978293 5 connected
83e1c6e862c76eab9eccc6baf6dba46140d1f397 172.38.0.13:6379@16379 master - 0 1650767976287 3 connected 10923-16383
b32a9427e25a63375b9f9ea911e9af8a6ee2f54d 172.38.0.12:6379@16379 master - 0 1650767976789 2 connected 5461-10922
c80aa76cc498770e242128ecf29155c6c77d1557 172.38.0.14:6379@16379 slave 83e1c6e862c76eab9eccc6baf6dba46140d1f397 0 1650767977290 4 connected
# 现在我们set 一个值进去
127.0.0.1:6379> set a b
-> Redirected to slot [15495] located at 172.38.0.13:6379
OK

可以看见放进了13这个里面,那我们知道主机挂了从机会替代上去,那我们把这个redis-3停了试试!

[root@QCSJdeLinux ~]# docker stop redis-3
redis-3

我们在get a试试,可以看见从14这个里面得到了这个值

127.0.0.1:6379> get a
-> Redirected to slot [15495] located at 172.38.0.14:6379
"b"

我们在看一下集群节点,这个就是成功了!

请添加图片描述

总结:这个就是高可用集群的搭建,一般你自己本机开6个redis不一定开的起来,用docker搭建是很快的,我们使用了docker之后,所有的技术都会简单起来~

你可能感兴趣的:(docker,redis,后端,golang)