docker搭建Redis的主从集群

服务器:Centos7
前提:selinux关掉
一,安装docker并配置镜像加速(我用的阿里加速)

[root@localhost ~]# yum -y install docker
[root@localhost ~]# cd /etc/docker/
[root@localhost docker]# cat daemon.json 
{
"registry-mirrors": ["https://yol1de5p.mirror.aliyuncs.com"]
}
[root@localhost docker]# systemctl restart docker

二,拉取redis镜像

[root@localhost ~]# docker pull redis
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/redis     latest              36304d3b4540        10 days ago         104 MB

三,运行redis-server容器并做持久化配置

[root@localhost ~]# mkdir /data
主:
[root@localhost ~]# docker run --name redis-master -d -p 6379:6379 -v /opt/redis-master:/data redis
俩从:
[root@localhost ~]# docker run --name redis-slave1 -d -p 6380:6379 -v /opt/redis-slave1:/data redis
[root@localhost ~]# docker run --name redis-slave2 -d -p 6381:6379 -v /opt/redis-slave2:/data redis

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
28e215ff4bc2        redis               "docker-entrypoint..."   21 seconds ago      Up 20 seconds       0.0.0.0:6381->6379/tcp   redis-slave2
7e579f3ec09c        redis               "docker-entrypoint..."   56 seconds ago      Up 56 seconds       0.0.0.0:6380->6379/tcp   redis-slave1
d7af410bffc3        redis               "docker-entrypoint..."   2 minutes ago       Up 2 minutes        0.0.0.0:6379->6379/tcp   redis-master

四,查看容器IP

[root@localhost ~]# docker inspect redis-master redis-slave1 redis-slave2 | grep IPAd
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.3",
                    "IPAddress": "172.17.0.3",
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.4",
                    "IPAddress": "172.17.0.4",

注:这里记住主的IP:172.17.0.2

五,配置redis集群,一主两从
我们将redis-master配置为主,redis-slave1和redis-slave2配置为从
登录从redis-slave1

[root@localhost ~]# docker exec -it redis-slave1 redis-cli
127.0.0.1:6379> SLAVEOF 172.17.0.2 6379
OK
127.0.0.1:6379> ROLE
1) "slave"
2) "172.17.0.2"
3) (integer) 6379
4) "connected"
5) (integer) 14

登录从redis-slave2

[root@localhost ~]# docker exec -it redis-slave2 redis-cli
127.0.0.1:6379> SLAVEOF 172.17.0.2 6379
OK
127.0.0.1:6379> ROLE
1) "slave"
2) "172.17.0.2"
3) (integer) 6379
4) "connected"
5) (integer) 112

登录主redis-master查看主从情况

[root@localhost ~]# docker exec -it redis-master redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.17.0.3,port=6379,state=online,offset=252,lag=0
slave1:ip=172.17.0.4,port=6379,state=online,offset=252,lag=1

六,验证主从是否集群成功

[root@localhost ~]# docker exec -it redis-master redis-cli
127.0.0.1:6379> set name tom
OK
127.0.0.1:6379> exit
[root@localhost ~]# docker exec -it redis-slave1 redis-cli
127.0.0.1:6379> get name
"tom"
127.0.0.1:6379> exit
[root@localhost ~]# docker exec -it redis-slave2 redis-cli
127.0.0.1:6379> get name
"tom"
127.0.0.1:6379> 

posted @ 2020-06-08 19:35  厶訫  阅读(6)  评论(0编辑  收藏  举报  来源