Docker redis集群配置。我們會在一臺centos的虛擬機(jī)上,配置redis集群
前期規(guī)劃:
redis版本 redis:5.0.14
網(wǎng)絡(luò)配置,我們會創(chuàng)建名稱為redis的網(wǎng)絡(luò),子網(wǎng)掩碼為 172.38.0.0/16的網(wǎng)絡(luò)
我們會啟動六個redis。對應(yīng)的容器名分別為:redis-1,redis-2,redis-3,redis-4,redis-5,redis-6;對應(yīng)的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;映射到宿主機(jī)的端口分別為:6371,6372,6373,6374,6375,6376;配置文件和數(shù)據(jù)文件分別掛載在宿主機(jī):/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
配置過程:
- 創(chuàng)建網(wǎng)絡(luò)
# 創(chuàng)建一個名為redis,子網(wǎng)掩碼為172.38.0.0/16的網(wǎng)絡(luò)
[root@localhost ~]# docker network create redis --subnet 172.38.0.0/16
# 查看網(wǎng)絡(luò)是否創(chuàng)建成功,看到NAME為redis的則表示創(chuàng)建成功
[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的詳細(xì)信息
[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
我們會在宿主機(jī)上的/usr/local/docker/redis文件夾下,分別創(chuàng)建node-1,node-2,node-3,node-4,node-5,node-6。寫入對應(yīng)的redis.conf文件的內(nèi)容。注意cluster-announce-ip對應(yīng)的ip。
# 1. 創(chuàng)建6個redis對應(yīng)的配置,下面的命令可以直接復(fù)制到xshell里面執(zhí)行。會生成對應(yīng)的文件和寫入相應(yīng)的配置 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. 查看相應(yīng)的文件是否成功,配置文件是否正常寫入 [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數(shù)據(jù)文件和配置文件掛載到宿主機(jī)。下面的內(nèi)容可以直接復(fù)制到xshell里面執(zhí)行 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 -
- 創(chuàng)建集群
創(chuàng)建集群,我們需要進(jìn)入容器里面輸入命令: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. 創(chuàng)建集群,這里我們有6個redis,我們隨便進(jìn)入一個redis里面配置,比如這里我們進(jìn)入redis-1里面配置 docker exec -it redis-1 /bin/sh 進(jìn)入容器 [root@localhost redis]# docker exec -it redis-1 /bin/sh # 進(jìn)入容器之后,輸入命令 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 # 執(zhí)行過程中,提示輸入的時候,輸入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. 查看集群是否創(chuàng)建成功,進(jìn)入容器,連接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>