环境准备两台机器
1.192.168.10.77 2.192.168.10.99 关闭防火墙 关闭selinux
192.168.10.77
一.拉取redis镜像
docker pull redis
二.创建文件夹与创建redis-cluster.tmpl
mkdir -p /usr/local/docker-redis/redis-cluster
vim redis-cluster.tmpl
port ${PORT}
masterauth 123456
requirepass 123456
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 192.168.10.77
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
port(端口号)
(1)masterauth(设置集群节点间访问密码,跟下面一致)
(2)requirepass(设置redis访问密码)
(3)cluster-enabled yes(启动集群模式)
(4)cluster-config-file nodes.conf(集群节点信息文件)
(5)cluster-node-timeout 5000(redis节点宕机被发现的时间)
(6)cluster-announce-ip(集群节点的汇报ip,防止nat)
(7)cluster-announce-port(集群节点的汇报port,防止nat)
(8)cluster-announce-bus-port(集群节点的汇报bus-port,防止nat)
三.在 redis-cluster下生成conf和data目标,并生成配置信息
for port in `seq 6371 6373`; do mkdir -p ${port}/conf && PORT=${port} envsubst < redis-cluster.tmpl > ${port}/conf/redis.conf && mkdir -p ${port}/data; done
文件结构 若没有tree,则
yum install tree -y
四.创建容器
创建3个容器
for port in $(seq 6374 6376);do \
docker run -di --restart always --name redis-${port} --net host \
-v /usr/local/docker-redis/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /usr/local/docker-redis/redis-cluster/${port}/data:/data/ \
redis redis-server /usr/local/etc/redis/redis.conf; \
done
五.进入任意一个redis容器
docker exec -it redis-6372 bash
接着执行集群命令(只在192.168.10.77敲)
redis-cli -a 123456 --cluster create 192.168.10.77:6371 192.168.10.77:6372 192.168.10.77:6373 192.168.10.99:6374 192.168.10.99:6375 192.168.10.99:6376 --cluster-replicas 1
六.检查集群(容器里面敲)
redis-cli -a 123456 --cluster check 192.168.10.99:6374
验证集群
redis-cli -c -a 123456 -h 192.168.10.77 -p 6372
set age 111
get age
192.168.10.99与192.168.10.77大同小异
|