1.安装docker环境并配置加速
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum -y install docker-ce
sudo service docker start
docker --version
mkdir -p /etc/docker
tee /etc/docker/daemon.json << -'EOF'
{
"registry-mirrors" : [ "https://jk2v9c0j.mirror.aliyuncs.com" ]
}
EOF
systemctl daemon-reload
systemctl restart docker
2.拉取镜像
[ root@c7-41 ~]
[ root@c7-41 ~]
[ root@c7-41 redis]
REPOSITORY TAG IMAGE ID CREATED SIZE
publicisworldwide/redis-cluster latest 29e4f38e4475 18 months ago 94.9MB
inem0o/redis-trib latest 0f7b910114d5 2 years ago 32MB
3.上传docker-compose到/usr/local/bin并赋予执行权限
[ root@c7-41 ~]
[ root@c7-41 bin]
/usr/local/bin
[ root@c7-41 bin]
docker-compose
[ root@c7-41 bin]
[ root@c7-41 bin]
docker-compose version 1.24.0, build 0aa59064
4.编写docker-compose.yml
[ root@c7-41 ~]
[ root@c7-41 ~]
[ root@c7-41 redis-cluster]
version: "3"
services:
redis1:
image: publicisworldwide/redis-cluster
environment:
REDIS_PORT: 7001
ports:
- "7001:7001"
- "17001:17001"
redis2:
image: publicisworldwide/redis-cluster
environment:
REDIS_PORT: 7002
ports:
- "7002:7002"
- "17002:17002"
redis3:
image: publicisworldwide/redis-cluster
environment:
REDIS_PORT: 7003
ports:
- "7003:7003"
- "17003:17003"
redis4:
image: publicisworldwide/redis-cluster
environment:
REDIS_PORT: 7004
ports:
- "7004:7004"
- "17004:17004"
redis5:
image: publicisworldwide/redis-cluster
environment:
REDIS_PORT: 7005
ports:
- "7005:7005"
- "17005:17005"
redis6:
image: publicisworldwide/redis-cluster
environment:
REDIS_PORT: 7006
ports:
- "7006:7006"
- "17006:17006"
EOF
5.启动所有容器
[ root@c7-41 redis-cluster]
Creating network "redis_default" with the default driver
Creating redis_redis4_1 .. . done
Creating redis_redis2_1 .. . done
Creating redis_redis1_1 .. . done
Creating redis_redis5_1 .. . done
Creating redis_redis3_1 .. . done
Creating redis_redis6_1 .. . done
6.创建集群
[ root@c7-41 redis-cluster]
>> > Creating cluster
>> > Performing hash slots allocation on 6 nodes.. .
Using 3 masters:
10.0.0.41:7001
10.0.0.41:7002
10.0.0.41:7003
Adding replica 10.0.0.41:7004 to 10.0.0.41:7001
Adding replica 10.0.0.41:7005 to 10.0.0.41:7002
Adding replica 10.0.0.41:7006 to 10.0.0.41:7003
M: 594081c27f135a9e426318c6c6f80a2a04ff0893 10.0.0.41:7001
slots:0-5460 ( 5461 slots) master
M: bf7a2bc7a74dfaa64060a78fd3466731b3c9a8a4 10.0.0.41:7002
slots:5461-10922 ( 5462 slots) master
M: b13310aec6adc0802a64e5d84814a8bc05e660a8 10.0.0.41:7003
slots:10923-16383 ( 5461 slots) master
S: 192b603929f7cc91498664890982a89914abf49d 10.0.0.41:7004
replicates 594081c27f135a9e426318c6c6f80a2a04ff0893
S: cf604dccff29d238de67fa2054727b5c0a9db741 10.0.0.41:7005
replicates bf7a2bc7a74dfaa64060a78fd3466731b3c9a8a4
S: 2a3d4c6a7a919a0ae8a17cc046c38afff2808c80 10.0.0.41:7006
replicates b13310aec6adc0802a64e5d84814a8bc05e660a8
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 10.0.0.41:7001)
M: 594081c27f135a9e426318c6c6f80a2a04ff0893 10.0.0.41:7001
slots:0-5460 ( 5461 slots) master
1 additional replica( s)
S: 2a3d4c6a7a919a0ae8a17cc046c38afff2808c80 172.18.0.1:7006@17006
slots: ( 0 slots) slave
replicates b13310aec6adc0802a64e5d84814a8bc05e660a8
M: b13310aec6adc0802a64e5d84814a8bc05e660a8 172.18.0.1:7003@17003
slots:10923-16383 ( 5461 slots) master
1 additional replica( s)
M: bf7a2bc7a74dfaa64060a78fd3466731b3c9a8a4 172.18.0.1:7002@17002
slots:5461-10922 ( 5462 slots) master
1 additional replica( s)
S: 192b603929f7cc91498664890982a89914abf49d 172.18.0.1:7004@17004
slots: ( 0 slots) slave
replicates 594081c27f135a9e426318c6c6f80a2a04ff0893
S: cf604dccff29d238de67fa2054727b5c0a9db741 172.18.0.1:7005@17005
slots: ( 0 slots) slave
replicates bf7a2bc7a74dfaa64060a78fd3466731b3c9a8a4
[ OK] All nodes agree about slots configuration.
>> > Check for open slots.. .
>> > Check slots coverage.. .
[ OK] All 16384 slots covered.
7.查看集群
[ root@c7-41 redis-cluster]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
31a6a96fe1b4 publicisworldwide/redis-cluster "/usr/local/bin/entr…" 57 seconds ago Up 54 seconds 6379/tcp, 0.0.0.0:7003-> 7003/tcp, 7000/tcp, 0.0.0.0:17003-> 17003/tcp redis-cluster_redis3_1
7eb419c98601 publicisworldwide/redis-cluster "/usr/local/bin/entr…" 57 seconds ago Up 54 seconds 6379/tcp, 0.0.0.0:7004-> 7004/tcp, 7000/tcp, 0.0.0.0:17004-> 17004/tcp redis-cluster_redis4_1
7c6ca48507f2 publicisworldwide/redis-cluster "/usr/local/bin/entr…" 57 seconds ago Up 54 seconds 6379/tcp, 0.0.0.0:7001-> 7001/tcp, 7000/tcp, 0.0.0.0:17001-> 17001/tcp redis-cluster_redis1_1
d5823dd426da publicisworldwide/redis-cluster "/usr/local/bin/entr…" 57 seconds ago Up 54 seconds 6379/tcp, 0.0.0.0:7002-> 7002/tcp, 7000/tcp, 0.0.0.0:17002-> 17002/tcp redis-cluster_redis2_1
5e304c7629fc publicisworldwide/redis-cluster "/usr/local/bin/entr…" 57 seconds ago Up 54 seconds 6379/tcp, 0.0.0.0:7006-> 7006/tcp, 7000/tcp, 0.0.0.0:17006-> 17006/tcp redis-cluster_redis6_1
a0908c891ccf publicisworldwide/redis-cluster "/usr/local/bin/entr…" 57 seconds ago Up 54 seconds 6379/tcp, 0.0.0.0:7005-> 7005/tcp, 7000/tcp, 0.0.0.0:17005-> 17005/tcp redis-cluster_redis5_1
8.登陆并查看集群节点信息
[ root@c7-41 redis-cluster]
root@7c6ca48507f2:/data
10.0.0.41:7001> cluster nodes
4044374764cc4303058de2289593b596cc76855b 172.18.0.1:7002@17002 master - 0 1589360230000 2 connected 5461-10922
827e0e1698953e920de1b9fb000ed890f2d1a525 172.18.0.1:7006@17006 slave 896a6d82a6d0c5a2e30a673ee0d9761d3d24ec36 0 1589360230804 6 connected
9acec4810b9e6ff5500605101801992e8a992265 172.18.0.1:7005@17005 slave 4044374764cc4303058de2289593b596cc76855b 0 1589360229596 5 connected
43636106a986e32490a2d88649200758210e78a0 172.18.0.1:7004@17004 slave 7e9a29a90e80a5cfa686babffef7106a8d587604 0 1589360229000 4 connected
896a6d82a6d0c5a2e30a673ee0d9761d3d24ec36 172.18.0.1:7003@17003 master - 0 1589360229000 3 connected 10923-16383
7e9a29a90e80a5cfa686babffef7106a8d587604 172.18.0.4:7001@17001 myself,master - 0 1589360230000 1 connected 0-5460
10.0.0.41:7001>
9.创建一个键对值并验证
10.0.0.41:7001> set name tom
-> Redirected to slot [ 5798] located at 172.18.0.1:7002
OK
172.18.0.1:7002> set class 1708a
OK
172.18.0.1:7002> set hh jj
-> Redirected to slot [ 12077] located at 172.18.0.1:7003
OK
172.18.0.1:7003> get class
-> Redirected to slot [ 7755] located at 172.18.0.1:7002
"1708a"
172.18.0.1:7002> get name
"tom"
172.18.0.1:7002>