table of Contents
Install Docker, Docker-compose and configuration acceleration
Run the container and map the cluster ip and port
surroundings
centos 7.5 192.168.253.110 resource package: https://download.csdn.net/download/yeyslspi59/12888631
Install Docker, Docker-compose and configuration acceleration
1) Configure Ali's docker source
[root@redis-master ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
2) Install docker, docker-compose
[root@redis-master ~]# yum -y install docker docker-compose
[root@redis-master ~]# vim /etc/docker/daemon.json #配置加速
{"registry-mirrors": ["https://26ahzfln.mirror.aliyuncs.com"]}
3) Start docker and close the firewall
[root@redis-master ~]# systemctl stop firewalld ##做集群我们需要关闭防火墙,否则后面会出现错误
[root@redis-master ~]# setenforce 0
[root@redis-master ~]# systemctl start docker
Ready to work
1) Upload the redis package and unzip it
[root@redis-master ~]# mkdir redis-cluster
[root@redis-master redis-cluster]# ls
redis-5.0.9 redis-5.0.9.tar.gz #最好是转换成tar.gz,我的资源下载获取的是zip格式
2) Write a .yaml file
[root@redis-master redis-cluster]# vim docker-compose.yml
version: '3'
services:
redis1:
image: publicisworldwide/redis-cluster
restart: always
volumes:
- /data/redis/8001/data:/data
environment:
- REDIS_PORT=8001
ports:
- '8001:8001' #服务端口
- '18001:18001' #集群端口
redis2:
image: publicisworldwide/redis-cluster
restart: always
volumes:
- /data/redis/8002/data:/data
environment:
- REDIS_PORT=8002
ports:
- '8002:8002'
- '18002:18002'
redis3:
image: publicisworldwide/redis-cluster
restart: always
volumes:
- /data/redis/8003/data:/data
environment:
- REDIS_PORT=8003
ports:
- '8003:8003'
- '18003:18003'
redis4:
image: publicisworldwide/redis-cluster
restart: always
volumes:
- /data/redis/8004/data:/data
environment:
- REDIS_PORT=8004
ports:
- '8004:8004'
- '18004:18004'
redis5:
image: publicisworldwide/redis-cluster
restart: always
volumes:
- /data/redis/8005/data:/data
environment:
- REDIS_PORT=8005
ports:
- '8005:8005'
- '18005:18005'
redis6:
image: publicisworldwide/redis-cluster
restart: always
volumes:
- /data/redis/8006/data:/data
environment:
- REDIS_PORT=8006
ports:
- '8006:8006'
- '18006:18006'
3) Start and view the prepared .yaml file
[root@redis-master redis-cluster]# docker-compose up -d
Creating rediscluster_redis1_1 ... done
Creating rediscluster_redis2_1 ... done
Creating rediscluster_redis3_1 ... done
Creating rediscluster_redis4_1 ... done
Creating rediscluster_redis5_1 ... done
Creating rediscluster_redis6_1 ... done
[root@redis-master redis-cluster]# docker-compose ps ##状态都为up表示成功
Name Command State Ports
-----------------------------------------------------------------------------------------------
rediscluster_redis1_1 /usr/local/bin/entrypoint. ... Up 0.0.0.0:18001->18001/tcp,
6379/tcp, 7000/tcp,
0.0.0.0:8001->8001/tcp
rediscluster_redis2_1 /usr/local/bin/entrypoint. ... Up 0.0.0.0:18002->18002/tcp,
6379/tcp, 7000/tcp,
0.0.0.0:8002->8002/tcp
rediscluster_redis3_1 /usr/local/bin/entrypoint. ... Up 0.0.0.0:18003->18003/tcp,
6379/tcp, 7000/tcp,
0.0.0.0:8003->8003/tcp
rediscluster_redis4_1 /usr/local/bin/entrypoint. ... Up 0.0.0.0:18004->18004/tcp,
6379/tcp, 7000/tcp,
0.0.0.0:8004->8004/tcp
rediscluster_redis5_1 /usr/local/bin/entrypoint. ... Up 0.0.0.0:18005->18005/tcp,
6379/tcp, 7000/tcp,
0.0.0.0:8005->8005/tcp
rediscluster_redis6_1 /usr/local/bin/entrypoint. ... Up 0.0.0.0:18006->18006/tcp,
6379/tcp, 7000/tcp,
0.0.0.0:8006->8006/tcp
Run the container and map the cluster ip and port
1) Run
[root@redis-master redis-cluster]# docker run --rm -it inem0o/redis-trib create --replicas 1 192.168.253.110:8001 192.168.253.110:8002 192.168.253.110:8003 192.168.253.110:8004 192.168.253.110:8005 192.168.253.110:8006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.253.110:8001
192.168.253.110:8002
192.168.253.110:8003
Adding replica 192.168.253.110:8004 to 192.168.253.110:8001
Adding replica 192.168.253.110:8005 to 192.168.253.110:8002
Adding replica 192.168.253.110:8006 to 192.168.253.110:8003
M: e5a8e05241101f505642a3bd6cc977a800b99813 192.168.253.110:8001
slots:0-5460 (5461 slots) master
M: bfda14f6ffe177b2fb741e48dd40a842a7ad73f0 192.168.253.110:8002
slots:5461-10922 (5462 slots) master
M: 4035cd160aa2fd516c366a82366a62ab1a252fb4 192.168.253.110:8003
slots:10923-16383 (5461 slots) master
S: 3a7411a7ab46b5a078b4c19be2cea70614e139f6 192.168.253.110:8004
replicates e5a8e05241101f505642a3bd6cc977a800b99813
S: 81e450c1663567fdece0dff96f97003557c314a5 192.168.253.110:8005
replicates bfda14f6ffe177b2fb741e48dd40a842a7ad73f0
S: 06d174f0133183d72c25a243e253725a33211b0e 192.168.253.110:8006
replicates 4035cd160aa2fd516c366a82366a62ab1a252fb4
Can I set the above configuration? (type 'yes' to accept): yes
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 192.168.253.110:8001)
M: e5a8e05241101f505642a3bd6cc977a800b99813 192.168.253.110:8001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 3a7411a7ab46b5a078b4c19be2cea70614e139f6 172.18.0.1:8004@18004
slots: (0 slots) slave
replicates e5a8e05241101f505642a3bd6cc977a800b99813
S: 06d174f0133183d72c25a243e253725a33211b0e 172.18.0.1:8006@18006
slots: (0 slots) slave
replicates 4035cd160aa2fd516c366a82366a62ab1a252fb4
M: bfda14f6ffe177b2fb741e48dd40a842a7ad73f0 172.18.0.1:8002@18002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 4035cd160aa2fd516c366a82366a62ab1a252fb4 172.18.0.1:8003@18003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 81e450c1663567fdece0dff96f97003557c314a5 172.18.0.1:8005@18005
slots: (0 slots) slave
replicates bfda14f6ffe177b2fb741e48dd40a842a7ad73f0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
2) Check whether it is successful
[root@redis-master redis-cluster]# docker ps #up状态就是成功
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8d92ee14aa00 publicisworldwide/redis-cluster "/usr/local/bin/en..." 16 minutes ago Up 16 minutes 6379/tcp, 0.0.0.0:8005->8005/tcp, 7000/tcp, 0.0.0.0:18005->18005/tcp rediscluster_redis5_1
a662333b0d69 publicisworldwide/redis-cluster "/usr/local/bin/en..." 16 minutes ago Up 16 minutes 6379/tcp, 0.0.0.0:8001->8001/tcp, 7000/tcp, 0.0.0.0:18001->18001/tcp rediscluster_redis1_1
d3b6e038294f publicisworldwide/redis-cluster "/usr/local/bin/en..." 16 minutes ago Up 16 minutes 6379/tcp, 0.0.0.0:8003->8003/tcp, 7000/tcp, 0.0.0.0:18003->18003/tcp rediscluster_redis3_1
3e6b273e6010 publicisworldwide/redis-cluster "/usr/local/bin/en..." 16 minutes ago Up 16 minutes 6379/tcp, 0.0.0.0:8006->8006/tcp, 7000/tcp, 0.0.0.0:18006->18006/tcp rediscluster_redis6_1
6047aed4eaac publicisworldwide/redis-cluster "/usr/local/bin/en..." 16 minutes ago Up 16 minutes 6379/tcp, 0.0.0.0:8004->8004/tcp, 7000/tcp, 0.0.0.0:18004->18004/tcp rediscluster_redis4_1
ead9190e444e publicisworldwide/redis-cluster "/usr/local/bin/en..." 16 minutes ago Up 16 minutes 6379/tcp, 0.0.0.0:8002->8002/tcp, 7000/tcp, 0.0.0.0:18002->18002/tcp rediscluster_redis2_1
test
1) Setting value
[root@redis-master redis-cluster]# ./redis-5.0.9/src/redis-cli -c -p 8001
127.0.0.1:8001> set hya 11
-> Redirected to slot [15500] located at 172.18.0.1:8003
OK
172.18.0.1:8003> get hya
"11"
##在8001端口设置
2) Specify the port to get the value
[root@redis-master redis-cluster]# ./redis-5.0.9/src/redis-cli -c -p 8003
127.0.0.1:8003> get hya
"11"
###其他端口能获取到值 表示集群成功
3) Get values from other ports
[root@redis-master redis-cluster]# ./redis-5.0.9/src/redis-cli -c -p 8006
127.0.0.1:8006> get hya
-> Redirected to slot [15500] located at 172.18.0.1:8003
"11"
####奥利给 可以的
to sum up
At the self-study stage, if you don't like it, don't spray. I feel that redis-cluster is still quite noble. I suggest that you are interested to understand.