环境准备
系统:centos7.7
redis:5.0.5
master1:192.168.33.9:7071
master2:192.168.33.10:7072
master3:192.168.33.11:7073
slave1:192.168.33.9:7074
slave2:192.168.33.10:7075
slave3:192.168.33.11:7076
一.搭建redis集群(以下步骤在每台服务器上都要执行)
1.>安装docker
yum -y install docker systemctl start dockersystemctl enable docker
2.>下载redis镜像
docker pull docker.io/redis:5.0.5
3.>下载redis官方配置文件redis.conf
wget http://download.redis.io/redis-stable/redis.conf
4.>对下载的redis.conf进行配置(因为是三主三从模式,因此端口号为7071~7076):
# bind 127.0.0.1 //加上注释# protected-mode no //关闭保护模式 port 7071 //绑定自定义端口 #daemonize yes //禁止redis后台运行 pidfile /var/run/redis_7071.pid cluster-enabled yes //开启集群 把注释#去掉 cluster-config-file nodes_7071.conf //集群的配置 配置文件首次启动自动生成
5.>在/usr/local/下创建redis-master文件夹和redis-slave文件夹
mkdir /usr/local/redis-master mkdir /usr/local/redis-slave
6.>把redis.conf配置文件分别复制到刚才创建的两个文件夹下
cp redis.conf /usr/local/redis-master cp redis.conf /usr/local/redis-slave
7.>以docker run命令在三台虚拟机上以指定配置文件启动redis容器(每台虚拟机上启动一个redis-master容器,一个reidis-slave容器)
docker run -it --name=redis-master1 --privileged -v /etc/localtime:/etc/localtime:ro -v /usr/local/redis-master/data:/data -v /usr/local/redis-master/redis.conf:/etc/redis/redis.conf -p 7071:7071 -p 17071:17071 -d docker.io/redis:5.0.5 redis-server /etc/redis/redis.conf --appendonly yes
docker run -it --name=redis-slave1 --privileged -v /etc/localtime:/etc/localtime:ro -v /usr/local/redis-slave/data:/data -v /usr/local/redis-slave/redis.conf:/etc/redis/redis.conf -p 7074:7074 -p 17074:17074 -d docker.io/redis:5.0.5 redis-server /etc/redis/redis.conf --appendonly yes
备注:现在还是单独的节点,需要创建主节点并加入从节点!!!
二.连接集群(在任意一台服务器上执行)
1.>创建redis集群的三个master节点
docker run --rm -it zvelo/redis-trib create --replicas 0 192.168.33.9:7071 192.168.33.10:7072 192.168.33.11:7073
2.>指定三个对应的slave节点
docker run --rm -it zvelo/redis-trib add-node --slave --master-id 4364c57b6d04800ce5fbda1427dd70446d8e4d76 192.168.33.10:7075 192.168.33.9:7071 docker run --rm -it zvelo/redis-trib add-node --slave --master-id f9458f269afa84249b74dcd9c3ea3aff7234848c 192.168.33.11:7076 192.168.33.10:7072 docker run --rm -it zvelo/redis-trib add-node --slave --master-id a5bc2c9371b3883f15085a894edb357c614dc960 192.168.33.9:7074 192.168.33.11:7073
3.>查看集群状态
docker exec -it redis-master1 bash redis-cli -c -h 192.168.33.9 -p 7071 cluster info
原文地址:https://www.cnblogs.com/lfl17718347843/p/12332396.html
时间: 2024-10-11 01:54:17