1、创建网络 代码语言: javascript
复制 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker network create redis --subnet 172.38.0.0/16
4323026b1d7145e167f22ecc0e908dfe0d75765380840e29e6b7010f1b3b902c
[root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
f92c1c2c4bf7 bridge bridge local
e6983a928f90 host host local
e99e400ffce3 mynet bridge local
21e45a3c002b none null local
4323026b1d71 redis bridge local
[root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker network inspect redis
[
{
"Name": "redis",
"Id": "4323026b1d7145e167f22ecc0e908dfe0d75765380840e29e6b7010f1b3b902c",
"Created": "2020-05-05T17:22:17.497116433 08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.38.0.0/16"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
2、编写配置文件 代码语言: javascript
复制 forport 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.38.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done
3、创建容器 代码语言: javascript
复制 # 安装redis-cluster;3主3从方式,从为了同步备份,主进行slot数据分片
[root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker run -p 6371:6379 -p 16371:16379 --name redis-1
> -v/mydata/redis/node-1/data:/data
> -v/mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf
> -d--netredis --ip172.38.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf;
[root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker run -p 6372:6379 -p 16372:16379 --name redis-2
-v/mydata/redis/node-2/data:/data
-v/mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf
-d--netredis --ip172.38.0.12 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf;
[root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker run -p 6373:6379 -p 16373:16379 --name redis-3
-v/mydata/redis/node-3/data:/data
-v/mydata/redis/node-3/conf/redis.conf:/etc/redis/redis.conf
-d--netredis --ip172.38.0.13 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf;
[root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker run -p 6374:6379 -p 16374:16379 --name redis-4
-v/mydata/redis/node-4/data:/data
-v/mydata/redis/node-4/conf/redis.conf:/etc/redis/redis.conf
-d--netredis --ip172.38.0.14 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf;
[root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker run -p 6375:6379 -p 16375:16379 --name redis-5
-v/mydata/redis/node-5/data:/data
-v/mydata/redis/node-5/conf/redis.conf:/etc/redis/redis.conf
-d--netredis --ip172.38.0.15 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf;
[root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker run -p 6376:6379 -p 16376:16379 --name redis-6
-v/mydata/redis/node-6/data:/data
-v/mydata/redis/node-6/conf/redis.conf:/etc/redis/redis.conf
-d--netredis --ip172.38.0.16 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf;
4、查看镜像 代码语言: javascript
复制 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2341a8496789 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 7seconds ago Up 7seconds 0.0.0.0:6376->6379/tcp, 0.0.0.0:16376->16379/tcp redis-6
195f467bd6c9 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 14seconds ago Up 13seconds 0.0.0.0:6375->6379/tcp, 0.0.0.0:16375->16379/tcp redis-5
798f165cd2b6 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 21seconds ago Up 21seconds 0.0.0.0:6374->6379/tcp, 0.0.0.0:16374->16379/tcp redis-4
511f9a7d2dab redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 29seconds ago Up 28seconds 0.0.0.0:6373->6379/tcp, 0.0.0.0:16373->16379/tcp redis-3
820cfdd85652 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 3minutes ago Up 3minutes 0.0.0.0:6372->6379/tcp, 0.0.0.0:16372->16379/tcp redis-2
d5798ca01b02 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 8minutes ago Up 8minutes 0.0.0.0:6371->6379/tcp, 0.0.0.0:16371->16379/tcp redis-1
5、建立集群 代码语言: javascript
复制 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker exec -it redis-1 /bin/sh
/data # 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-replicas1
>>> Performing hash slots allocation on 6nodes...
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: a3d11679dd434e61a8c301b02f34ed2e37a6ce0d 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
M: 19293d61791795cae773eef640d55e0ac0cc090c 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
M: 13134855b3ad0d731688a0714543a7131520088d 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
S: 455e94fb32c097c63bd2e43dff4c7fac6875acea 172.38.0.14:6379
replicates 13134855b3ad0d731688a0714543a7131520088d
S: 165f0a056153eaa49f0d70b062ada0e1a44ebe33 172.38.0.15:6379
replicates a3d11679dd434e61a8c301b02f34ed2e37a6ce0d
S: 3195a7ceab0c2312fe15487a5a4bc9a16a7cf4de 172.38.0.16:6379
replicates 19293d61791795cae773eef640d55e0ac0cc090c
Can I setthe 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 forthe cluster to join
...
>>> Performing Cluster Check (using node172.38.0.11:6379)
M: a3d11679dd434e61a8c301b02f34ed2e37a6ce0d 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
1additional replica(s)
S: 455e94fb32c097c63bd2e43dff4c7fac6875acea 172.38.0.14:6379
slots: (0 slots) slave
replicates 13134855b3ad0d731688a0714543a7131520088d
S: 165f0a056153eaa49f0d70b062ada0e1a44ebe33 172.38.0.15:6379
slots: (0 slots) slave
replicates a3d11679dd434e61a8c301b02f34ed2e37a6ce0d
M: 19293d61791795cae773eef640d55e0ac0cc090c 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
1additional replica(s)
M: 13134855b3ad0d731688a0714543a7131520088d 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
1additional replica(s)
S: 3195a7ceab0c2312fe15487a5a4bc9a16a7cf4de 172.38.0.16:6379
slots: (0 slots) slave
replicates 19293d61791795cae773eef640d55e0ac0cc090c
[OK] All nodes agree about slots configuration.
>>> Check foropen slots...
>>> Check slots coverage...
[OK] All 16384slots covered.
6、存储数据 代码语言: javascript
复制 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker exec -it redis-1 /bin/sh
/data # redis-cli -c
127.0.0.1:6379> cluster nodes
455e94fb32c097c63bd2e43dff4c7fac6875acea 172.38.0.14:6379@16379 slave 13134855b3ad0d731688a0714543a7131520088d 015886730278134connected
165f0a056153eaa49f0d70b062ada0e1a44ebe33 172.38.0.15:6379@16379 slave a3d11679dd434e61a8c301b02f34ed2e37a6ce0d 015886730288165connected
a3d11679dd434e61a8c301b02f34ed2e37a6ce0d 172.38.0.11:6379@16379 myself,master -015886730270001connected 0-5460
19293d61791795cae773eef640d55e0ac0cc090c 172.38.0.12:6379@16379 master -015886730280002connected 5461-10922
13134855b3ad0d731688a0714543a7131520088d 172.38.0.13:6379@16379 master -015886730280003connected 10923-16383
3195a7ceab0c2312fe15487a5a4bc9a16a7cf4de 172.38.0.16:6379@16379 slave 19293d61791795cae773eef640d55e0ac0cc090c 015886730280006connected
127.0.0.1:6379> setywm 123456
-> Redirected to slot [10844] located at 172.38.0.12:6379
OK
172.38.0.12:6379> getywm
"123456"
7、测试高可用 代码语言: javascript
复制 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker stop redis-2
redis-2
[root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker exec -it redis-1 /bin/sh
/data # redis-cli -c
127.0.0.1:6379> getywm
-> Redirected to slot [10844] located at 172.38.0.16:6379
"123456"