redis 分片+ 高可用+ 负载均衡
6个redis(3主 + 3从)
1.创建redis自定义的网络
docker network create redis --subnet 172.38.0.0/16
查看创建的网络
[root@hc /]# docker network ls
NETWORK ID NAME DRIVER SCOPE
923c8521a545 bridge bridge local
c73d7061483e host host local
a9a351b54f2c mynet bridge local
c5527900ca76 none null local
0f561c32c71d redis bridge local
[root@hc /]# docker network inspect redis
[
{
"Name": "redis",
"Id": "0f561c32c71dc4a83af83ef8c2f847b6ecc08950317b415f83c8173c41d94220",
"Created": "2021-06-13T00:15:12.74917123+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.38.0.0/16"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
2.通过脚本创建6个redis配置
for port in $(seq 1 6);\
do \
mkdir -p /mydata/redis/node-${port}/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
脚本创建后检查
[root@hc /]# for port in $(seq 1 6);\
> do \
> mkdir -p /mydata/redis/node-${port}/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
[root@hc /]# cd /mydata/redis/
[root@hc redis]# ls
node-1 node-2 node-3 node-4 node-5 node-6
[root@hc redis]# cd node-1
[root@hc node-1]# ls
conf
3.启动6个docker redis服务
实例:
docker run -p 637${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; \
切换不同的端口,启动6次
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
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
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
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
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
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
验证:
[root@hc /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e14abdf06ae redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 6 seconds ago Up 5 seconds 0.0.0.0:6376->6379/tcp, :::6376->6379/tcp, 0.0.0.0:16376->16379/tcp, :::16376->16379/tcp redis-6
b1b58cec4dee redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 17 seconds ago Up 16 seconds 0.0.0.0:6375->6379/tcp, :::6375->6379/tcp, 0.0.0.0:16375->16379/tcp, :::16375->16379/tcp redis-5
553148c2862a redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 30 seconds ago Up 29 seconds 0.0.0.0:6374->6379/tcp, :::6374->6379/tcp, 0.0.0.0:16374->16379/tcp, :::16374->16379/tcp redis-4
92b68c297974 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 43 seconds ago Up 42 seconds 0.0.0.0:6373->6379/tcp, :::6373->6379/tcp, 0.0.0.0:16373->16379/tcp, :::16373->16379/tcp redis-3
3203719093f5 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:6372->6379/tcp, :::6372->6379/tcp, 0.0.0.0:16372->16379/tcp, :::16372->16379/tcp redis-2
5ec1632eaa02 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:6371->6379/tcp, :::6371->6379/tcp, 0.0.0.0:16371->16379/tcp, :::16371->16379/tcp redis-1
4.创建集群
随便进入一个redis里
docker exec -it redis-1 /bin/sh #进入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-replicas 1
实战:
/data # ls
appendonly.aof nodes.conf
/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 --clust
er-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.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: c4ce2b4ef0bf46f2c8d8146646c30be689a900ff 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
M: fbd74f02010b6690698d5466c1c3890609f60dec 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
M: d6bb03671b8cca0b90f25f9fe72d174342a76d9c 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
S: cba3938ee96d43929f19db73d62f8423349996f1 172.38.0.14:6379
replicates d6bb03671b8cca0b90f25f9fe72d174342a76d9c
S: 97c3512250e62ad27239288de8389b6e30f97cf1 172.38.0.15:6379
replicates c4ce2b4ef0bf46f2c8d8146646c30be689a900ff
S: 60c97f9f8e2d1c37129b1815f62f5b181e5bb3b4 172.38.0.16:6379
replicates fbd74f02010b6690698d5466c1c3890609f60dec
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: c4ce2b4ef0bf46f2c8d8146646c30be689a900ff 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 60c97f9f8e2d1c37129b1815f62f5b181e5bb3b4 172.38.0.16:6379
slots: (0 slots) slave
replicates fbd74f02010b6690698d5466c1c3890609f60dec
M: fbd74f02010b6690698d5466c1c3890609f60dec 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: cba3938ee96d43929f19db73d62f8423349996f1 172.38.0.14:6379
slots: (0 slots) slave
replicates d6bb03671b8cca0b90f25f9fe72d174342a76d9c
M: d6bb03671b8cca0b90f25f9fe72d174342a76d9c 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 97c3512250e62ad27239288de8389b6e30f97cf1 172.38.0.15:6379
slots: (0 slots) slave
replicates c4ce2b4ef0bf46f2c8d8146646c30be689a900ff
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
集群配置查看
redis-cli -c #进入redis集群
cluster info #查看集群信息
cluster nodes # 查看集群节点
/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:384
cluster_stats_messages_pong_sent:407
cluster_stats_messages_sent:791
cluster_stats_messages_ping_received:402
cluster_stats_messages_pong_received:384
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:791
127.0.0.1:6379> cluster nodes
c4ce2b4ef0bf46f2c8d8146646c30be689a900ff 172.38.0.11:6379@16379 myself,master - 0 1623518279000 1 connected 0-5460
60c97f9f8e2d1c37129b1815f62f5b181e5bb3b4 172.38.0.16:6379@16379 slave fbd74f02010b6690698d5466c1c3890609f60dec 0 1623518279000 6 connected
fbd74f02010b6690698d5466c1c3890609f60dec 172.38.0.12:6379@16379 master - 0 1623518279528 2 connected 5461-10922
cba3938ee96d43929f19db73d62f8423349996f1 172.38.0.14:6379@16379 slave d6bb03671b8cca0b90f25f9fe72d174342a76d9c 0 1623518280029 4 connected
d6bb03671b8cca0b90f25f9fe72d174342a76d9c 172.38.0.13:6379@16379 master - 0 1623518280530 3 connected 10923-16383
97c3512250e62ad27239288de8389b6e30f97cf1 172.38.0.15:6379@16379 slave c4ce2b4ef0bf46f2c8d8146646c30be689a900ff 0 1623518280000 5 connected