Docker redis集群配置。我们会在一台centos的虚拟机上,配置redis集群
前期规划:
redis版本 redis:5.0.14
网络配置,我们会创建名称为redis的网络,子网掩码为 172.38.0.0/16的网络
我们会启动六个redis。对应的容器名分别为:redis-1,redis-2,redis-3,redis-4,redis-5,redis-6;对应的IP为:172.38.0.11,172.38.0.12,172.38.0.13,172.38.0.14,172.38.0.15,172.38.0.16;映射到宿主机的端口分别为:6371,6372,6373,6374,6375,6376;配置文件和数据文件分别挂载在宿主机:/usr/local/docker/redis/node-1,/usr/local/docker/redis/node-2,/usr/local/docker/redis/node-3,/usr/local/docker/redis/node-4,/usr/local/docker/redis/node-5,/usr/local/docker/redis/node-6
配置过程:
-
- 创建网络
# 创建一个名为redis,子网掩码为172.38.0.0/16的网络
[root@localhost ~]# docker network create redis --subnet 172.38.0.0/16
# 查看网络是否创建成功,看到NAME为redis的则表示创建成功
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
910bb1c3ed53 bridge bridge local
0abda6b25a0e host host local
7c4403334c93 mynet bridge local
844e3f85e00e mysql bridge local
c2bbcb4013a7 none null local
295732ea0dac redis bridge local
# docker network inspect redis可以查看redis的详细信息
[root@localhost ~]# docker network inspect redis
[
{
"Name": "redis",
"Id": "295732ea0dacb5f64ba0c2748db3eeeb54271fe8e97f5d316df70b091279673b",
"Created": "2021-12-11T06:32:00.216608768-05:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.38.0.0/16",
"Gateway": "172.38.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
-
- 下载redis镜像
# 我们下载redis:5.0.14(docker pull redis:5.0.14) [root@localhost /]# docker pull redis:5.0.14 5.0.14: Pulling from library/redis e5ae68f74026: Pull complete 37c4354629da: Pull complete b065b1b1fa0f: Pull complete 99ab464ba8bb: Pull complete eb5bbe3179d2: Pull complete 2067794f93b6: Pull complete Digest: sha256:310f81701011175dc868e833d73f539282dd18510ca35d6f7b63c4d33ab4f54e Status: Downloaded newer image for redis:5.0.14 docker.io/library/redis:5.0.14 # docker images查看redis镜像是否下载成功 [root@localhost /]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE redis 5.0.14 071510791e92 2 weeks ago 110MB mysql 5.7 738e7101490b 2 weeks ago 448MB
-
- 启动六个redis
我们会在宿主机上的/usr/local/docker/redis文件夹下,分别创建node-1,node-2,node-3,node-4,node-5,node-6。写入对应的redis.conf文件的内容。注意cluster-announce-ip对应的ip。
# 1. 创建6个redis对应的配置,下面的命令可以直接复制到xshell里面执行。会生成对应的文件和写入相应的配置 for index in $(seq 1 6);\ do \ mkdir -p /usr/local/docker/redis/node-${index}/conf touch /usr/local/docker/redis/node-${index}/conf/redis.conf cat << EOF >> /usr/local/docker/redis/node-${index}/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${index} cluster-announce-port 6379 cluster-announce-bus-port 16379 appendonly yes EOF done # 2. 查看相应的文件是否成功,配置文件是否正常写入 [root@localhost redis]# pwd /usr/local/docker/redis [root@localhost redis]# ll 总用量 0 drwxr-xr-x. 3 root root 18 12月 18 00:59 node-1 drwxr-xr-x. 3 root root 18 12月 18 00:59 node-2 drwxr-xr-x. 3 root root 18 12月 18 00:59 node-3 drwxr-xr-x. 3 root root 18 12月 18 00:59 node-4 drwxr-xr-x. 3 root root 18 12月 18 00:59 node-5 drwxr-xr-x. 3 root root 18 12月 18 00:59 node-6 # 3. 启动6个redis。注意ip的配置。注意docker数据文件和配置文件挂载到宿主机。下面的内容可以直接复制到xshell里面执行 for index in $(seq 1 6);\ do \ docker run -p 637${index}:6379 -p 1637${index}:16379 --name redis-${index} \ -v /usr/local/docker/redis/node-${index}/data:/data \ -v /usr/local/docker/redis/node-${index}/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 172.38.0.1${index} redis:5.0.14 redis-server /etc/redis/redis.conf done # 4. 查看redis是否启动成功,没啥大问题的话,docker ps 我们可以看到我们启动了6个redis [root@localhost redis]# docker ps
-
- 创建集群
创建集群,我们需要进入容器里面输入命令: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
# 1. 创建集群,这里我们有6个redis,我们随便进入一个redis里面配置,比如这里我们进入redis-1里面配置 docker exec -it redis-1 /bin/sh 进入容器 [root@localhost redis]# docker exec -it redis-1 /bin/sh # 进入容器之后,输入命令 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 # 执行过程中,提示输入的时候,输入yes # 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 >>> 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: b5afda552e13a82efcbad213159a5f51b1ff5e9e 172.38.0.11:6379 slots:[0-5460] (5461 slots) master M: db1d24dd03af679c59543807b918496d185246ec 172.38.0.12:6379 slots:[5461-10922] (5462 slots) master M: b33635d04f16c013c85c69bfda59f810a2c63421 172.38.0.13:6379 slots:[10923-16383] (5461 slots) master S: e15110b81065f8a882115f068026785803a1e4e1 172.38.0.14:6379 replicates b33635d04f16c013c85c69bfda59f810a2c63421 S: 5ea19fe52c450aea5e18c74f8086b9d35ed7183e 172.38.0.15:6379 replicates b5afda552e13a82efcbad213159a5f51b1ff5e9e S: 79ae80b43bdbc7eb7acf0ba3965bd1077c9a6d79 172.38.0.16:6379 replicates db1d24dd03af679c59543807b918496d185246ec 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: b5afda552e13a82efcbad213159a5f51b1ff5e9e 172.38.0.11:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: b33635d04f16c013c85c69bfda59f810a2c63421 172.38.0.13:6379 slots:[10923-16383] (5461 slots) master 1 additional replica(s) M: db1d24dd03af679c59543807b918496d185246ec 172.38.0.12:6379 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: 79ae80b43bdbc7eb7acf0ba3965bd1077c9a6d79 172.38.0.16:6379 slots: (0 slots) slave replicates db1d24dd03af679c59543807b918496d185246ec S: 5ea19fe52c450aea5e18c74f8086b9d35ed7183e 172.38.0.15:6379 slots: (0 slots) slave replicates b5afda552e13a82efcbad213159a5f51b1ff5e9e S: e15110b81065f8a882115f068026785803a1e4e1 172.38.0.14:6379 slots: (0 slots) slave replicates b33635d04f16c013c85c69bfda59f810a2c63421 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] # 2. 查看集群是否创建成功,进入容器,连接redis,cluster nodes查看集群信息 [root@localhost redis]# docker exec -it redis-1 /bin/sh # redis-cli 127.0.0.1:6379> cluster nodes b33635d04f16c013c85c69bfda59f810a2c63421 172.38.0.13:6379@16379 master - 0 1639808016802 3 connected 10923-16383 db1d24dd03af679c59543807b918496d185246ec 172.38.0.12:6379@16379 master - 0 1639808017000 2 connected 5461-10922 79ae80b43bdbc7eb7acf0ba3965bd1077c9a6d79 172.38.0.16:6379@16379 slave db1d24dd03af679c59543807b918496d185246ec 0 1639808017805 6 connected 5ea19fe52c450aea5e18c74f8086b9d35ed7183e 172.38.0.15:6379@16379 slave b5afda552e13a82efcbad213159a5f51b1ff5e9e 0 1639808017000 5 connected b5afda552e13a82efcbad213159a5f51b1ff5e9e 172.38.0.11:6379@16379 myself,master - 0 1639808016000 1 connected 0-5460 e15110b81065f8a882115f068026785803a1e4e1 172.38.0.14:6379@16379 slave b33635d04f16c013c85c69bfda59f810a2c63421 0 1639808016600 4 connected 127.0.0.1:6379>