docker redis 集群搭建及主从容错切换演练
第一步,先拉取镜像docker pull redis
然后直接把容器都跑起来,这里用了redis-01 到redis-08
docker run -d --name redis-01 --net host --privileged=true -v /data/redis/redis_01:/data redis:latest --cluster-enabled
yes --appendonly yes --port 6381
docker run -d --name redis-02 --net host --privileged=true -v /data/redis/redis_02:/data redis:latest --cluster-enabled
yes --appendonly yes --port 6382
docker run -d --name redis-03 --net host --privileged=true -v /data/redis/redis_03:/data redis:latest --cluster-enabled
yes --appendonly yes --port 6383
docker run -d --name redis-04 --net host --privileged=true -v /data/redis/redis_04:/data redis:latest --cluster-enabled
yes --appendonly yes --port 6384
docker run -d --name redis-05 --net host --privileged=true -v /data/redis/redis_05:/data redis:latest --cluster-enabled
yes --appendonly yes --port 6385
docker run -d --name redis-06 --net host --privileged=true -v /data/redis/redis_06:/data redis:latest --cluster-enabled
yes --appendonly yes --port 6386
docker run -d --name redis-07 --net host --privileged=true -v /data/redis/redis_07:/data redis:latest --cluster-enabled
yes --appendonly yes --port 6387
docker run -d --name redis-08 --net host --privileged=true -v /data/redis/redis_08:/data redis:latest --cluster-enabled
yes --appendonly yes --port 6388
docker run 创建并运行容器
–name 指定容器名字
–net 使用宿主机的IP和端口,默认
–privileged 获取宿主机root用户权限
-v 容器卷, 宿主机文件夹: 容器内部文件
redis:latest 镜像和版本号(latest是最新版本号)
–cluster-enabled 开启redis集群
–appendonly开启持久化
–port redis端口
运行docker容器的时候,他会自动把容器用到的宿主机文件夹建起来,如下面:
看一宿主机的IP地址,我们等会要用
[root@WIND scripts]# ifconfig | grep net
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
登录其中一个redis ,用redis-clis命令创建redis集群,命令格式:
redis-cli --cluster create IP:port --cluster-replicas 1
[root@WIND ~]# docker exec -it redis-01 /bin/bash
root@WIND:/data#redis-cli --cluster create 172.17.0.1:6381 172.17.0.1:6382 172.17.0.1:6383 172.17.0.1:6384 172.17.0.1:6385 172.17.0.1:6386 --cluster-replicas 1
这里的IP,就是我们刚才用ifconfig命令查看到的宿主机IP,注意不是容器内部的IP
root@WIND:/data# redis-cli --cluster create 172.17.0.1:6381 172.17.0.1:6382 172.17.0.1:6383 172.17.0.1:6384 172.17.0.1:6
385 172.17.0.1:6386 --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.17.0.1:6385 to 172.17.0.1:6381
Adding replica 172.17.0.1:6386 to 172.17.0.1:6382
Adding replica 172.17.0.1:6384 to 172.17.0.1:6383
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 583c95d6be5823b381ae9a83d785b10a877c1fc4 172.17.0.1:6381
slots:[0-5460] (5461 slots) master
M: 6d096c81307598ed804f61ed1a2b6d04ec226adb 172.17.0.1:6382
slots:[5461-10922] (5462 slots) master
M: f0c8010c67b60bdbecfe8022c6670f2c75be213f 172.17.0.1:6383
slots:[10923-16383]