一、示意图
二、创建Redis网络
执行命令
docker network ls docker network create redis --subnet 192.168.100.0/24 docker network inspect redis
执行命令结果
[root@docker ~]# docker network ls NETWORK ID NAME DRIVER SCOPE d000c2d78106 bridge bridge local 9fd62dbfb07f host host local 27700772b8f7 none null local [root@docker ~]# docker network create redis --subnet 192.168.100.0/24 75c42ccdfaff7291081a73cf6f28eeb629c80da12b1ec55153a8171d2bc98155 [root@docker ~]# docker network ls NETWORK ID NAME DRIVER SCOPE d000c2d78106 bridge bridge local 9fd62dbfb07f host host local 27700772b8f7 none null local 75c42ccdfaff redis bridge local [root@docker ~]# docker network inspect redis [ { "Name": "redis", "Id": "75c42ccdfaff7291081a73cf6f28eeb629c80da12b1ec55153a8171d2bc98155", "Created": "2022-05-11T00:09:44.013536884+08:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "192.168.100.0/24", "Gateway": "192.168.100.1" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": {}, "Options": {}, "Labels": {} } ] [root@docker ~]#
三、创建Redis配置文件
执行脚本创建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 192.168.100.1${port} cluster-announce-port 6379 cluster-announce-bus-port 16379 appendonly yes EOF done
执行命令结果
[root@docker ~]# 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.36.0.1${port} > cluster-announce-port 6379 > cluster-announce-bus-port 16379 > appendonly yes > EOF > done [root@docker ~]# ll /mydata/redis/ total 0 drwxr-xr-x 3 root root 18 May 11 23:07 node-1 drwxr-xr-x 3 root root 18 May 11 23:07 node-2 drwxr-xr-x 3 root root 18 May 11 23:07 node-3 drwxr-xr-x 3 root root 18 May 11 23:07 node-4 drwxr-xr-x 3 root root 18 May 11 23:07 node-5 drwxr-xr-x 3 root root 18 May 11 23:07 node-6 [root@docker ~]# cat /mydata/redis/node-1/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.36.0.11 cluster-announce-port 6379 cluster-announce-bus-port 16379 appendonly yes [root@docker ~]#
四、启动Redis容器
执行六条命令,分别创建六个redis容器
# 第一个节点配置命令 docker run -p 6371:6379 -p 16371:16379 --name redis-01 \ -v /mydata/redis/node-1/data:/data \ -v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 192.168.100.11 redis:6-alpine3.15 redis-server /etc/redis/redis.conf # 命令参数解释: # -p 6371:6379 对外端口 # -p 16371:16379 Redis内部通信端口 # -v /mydata/redis/node-1/data:/data 绑定挂载卷(容器内外挂载路径) # -d 在后台运行容器并打印容器ID # --net redis 使用自定义redis网络 # --ip 指定IP地址 # redis:6-alpine3.15 指定Redis版本号 # 第二个节点配置命令 docker run -p 6372:6379 -p 16372:16379 --name redis-02 \ -v /mydata/redis/node-2/data:/data \ -v /mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 192.168.100.12 redis:6-alpine3.15 redis-server /etc/redis/redis.conf # 第三个节点配置命令 docker run -p 6373:6379 -p 16373:16379 --name redis-03 \ -v /mydata/redis/node-3/data:/data \ -v /mydata/redis/node-3/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 192.168.100.13 redis:6-alpine3.15 redis-server /etc/redis/redis.conf # 第四个节点配置命令 docker run -p 6374:6379 -p 16374:16379 --name redis-04 \ -v /mydata/redis/node-4/data:/data \ -v /mydata/redis/node-4/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 192.168.100.14 redis:6-alpine3.15 redis-server /etc/redis/redis.conf # 第五个节点配置命令 docker run -p 6375:6379 -p 16375:16379 --name redis-05 \ -v /mydata/redis/node-5/data:/data \ -v /mydata/redis/node-5/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 192.168.100.15 redis:6-alpine3.15 redis-server /etc/redis/redis.conf # 第六个节点配置命令 docker run -p 6376:6379 -p 16376:16379 --name redis-06 \ -v /mydata/redis/node-6/data:/data \ -v /mydata/redis/node-6/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 192.168.100.16 redis:6-alpine3.15 redis-server /etc/redis/redis.conf
查看部署的Redis容器
[root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 34864cf361ab redis:6-alpine3.15 "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:6376->6379/tcp, :::6376->6379/tcp, 0.0.0.0:16376->16379/tcp, :::16376->16379/tcp redis-06 cb3667596bd8 redis:6-alpine3.15 "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:6375->6379/tcp, :::6375->6379/tcp, 0.0.0.0:16375->16379/tcp, :::16375->16379/tcp redis-05 190ca9226db4 redis:6-alpine3.15 "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:6374->6379/tcp, :::6374->6379/tcp, 0.0.0.0:16374->16379/tcp, :::16374->16379/tcp redis-04 cd9fae883a1f redis:6-alpine3.15 "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:6373->6379/tcp, :::6373->6379/tcp, 0.0.0.0:16373->16379/tcp, :::16373->16379/tcp redis-03 6784b9a3fa94 redis:6-alpine3.15 "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 0.0.0.0:6372->6379/tcp, :::6372->6379/tcp, 0.0.0.0:16372->16379/tcp, :::16372->16379/tcp redis-02 884dd21979fb redis:6-alpine3.15 "docker-entrypoint.s…" 11 minutes ago Up 11 minutes 0.0.0.0:6371->6379/tcp, :::6371->6379/tcp, 0.0.0.0:16371->16379/tcp, :::16371->16379/tcp redis-01
五、创建Redis集群
执行命令
docker exec -it redis-01 /bin/sh redis-cli --cluster create 192.168.100.11:6379 192.168.100.12:6379 192.168.100.13:6379 192.168.100.14:6379 192.168.100.15:6379 192.168.100.16:6379 --cluster-replicas 1
执行命令结果
六、查看Redis集群
执行命令
# 进入集群 redis-cli -c # 查看集群内节点 cluster nodes
执行命令结果
七、测试Redis集群高可用
模拟redis集群中某台节点丢失,例如,将Redis-03节点停止服务。
执行命令
# 查看节点 docker ps # 停止redis-03节点 docker stop redis-03
执行命令结果
查看Redis集群中,节点变化情况,是否实现集群高可用
重新启动redis-03节点
docker start redis-03 docker exec -it redis-01 /bin/sh
九、学习链接
Redis - Official Image | Docker Hub