redis集群整体的安装效果
备注:本机docker容器的宿主机ip为192.168.0.200,下面的配置全部基于当前IP进行配置;
1 docker镜像下载使用的国内地址
vi /etc/docker/daemon.json
{
"registry-mirrors": [
"https://ustc-edu-cn.mirror.aliyuncs.com/",
"https://ccr.ccs.tencentyun.com/",
"https://docker.m.daocloud.io/"
]
}
2 下载镜像
当前教程使用的是7.2.4,如果需要其他版本redis,可以自行搜索:点击跳转
docker pull redis:7.2.4
3 创建并且启动镜像
- 创建6台除端口号不一致,其他全部一致的redis容器
docker run -d --name redis-node-1 --net host --privileged=true -v /opt/redis/cluster/redis-node-1:/opt/redis redis:7.2.4 --cluster-enabled yes --appendonly yes --port 6381
docker run -d --name redis-node-2 --net host --privileged=true -v /opt/redis/cluster/redis-node-2:/opt/redis redis:7.2.4 --cluster-enabled yes --appendonly yes --port 6382
docker run -d --name redis-node-3 --net host --privileged=true -v /opt/redis/cluster/redis-node-3:/opt/redis redis:7.2.4 --cluster-enabled yes --appendonly yes --port 6383
docker run -d --name redis-node-4 --net host --privileged=true -v /opt/redis/cluster/redis-node-4:/opt/redis redis:7.2.4 --cluster-enabled yes --appendonly yes --port 6384
docker run -d --name redis-node-5 --net host --privileged=true -v /opt/redis/cluster/redis-node-5:/opt/redis redis:7.2.4 --cluster-enabled yes --appendonly yes --port 6385
docker run -d --name redis-node-6 --net host --privileged=true -v /opt/redis/cluster/redis-node-6:/opt/redis redis:7.2.4 --cluster-enabled yes --appendonly yes --port 6386
4 6个容器构建集群关系
- 随便进入一台redis容器,为6个容器构建redis集群关系
进入容器
docker exec -it 容器id bash
构建redis集群关系
redis-cli --cluster create 192.168.0.200:6381 192.168.0.200:6382 192.168.0.200:6383 192.168.0.200:6384 192.168.0.200:6385 192.168.0.200:6386 --cluster-replicas 1
注:
- --cluster-replicas 1 : 表示为每个master创建一个slave节点 集群操作
5 查看集群与节点
进入容器
docker exec -it 容器id bash
连接redis实例
redis-cli -p 6381
5.1 查看集群信息
cluster info
127.0.0.1:6381> 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:1286
cluster_stats_messages_pong_sent:990
cluster_stats_messages_sent:2276
cluster_stats_messages_ping_received:985
cluster_stats_messages_pong_received:1286
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:2276
total_cluster_links_buffer_limit_exceeded:0
5.2 查看集群节点
cluster nodes
127.0.0.1:6381> cluster nodes
77f9016dcb3ca6eb87a639e8547426e9f136ba7a 192.168.0.200:6382@16382 master - 0 1722680065749 2 connected 5461-10922
ca475b5a6ecab558e26d9c9a208b72d646024584 192.168.0.200:6385@16385 slave 77f9016dcb3ca6eb87a639e8547426e9f136ba7a 0 1722680065000 2 connected
6ff5eb220e3154217448c7c3a7e5cb1455bcf7d4 192.168.0.200:6381@16381 myself,master - 0 1722765605000 1 connected 0-5460
fa37a1a0ff2c5346a9fc3b01345148fd2169eaff 192.168.0.200:6386@16386 slave fe1fb4226b1abdd140ced297b5724e96ebb5990f 0 1722680063000 3 connected
fe1fb4226b1abdd140ced297b5724e96ebb5990f 192.168.0.200:6383@16383 master - 0 1722680065000 3 connected 10923-16383
8373031b5a1177071f12e57f2cf09b0bb2189590 192.168.0.200:6384@16384 slave 6ff5eb220e3154217448c7c3a7e5cb1455bcf7d4 0 1722765608514 1 connected
127.0.0.1:6381>
6 重新分配集群槽位
待补充