单主机docker 搭建 redis-cluster

1、#安装docker

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce -y
systemctl start docker
systemctl enable docker

2、 #拉取redis镜像

docker pull redis:6.0

3、# 通过脚本创建6个redis配置,并启动容器

for port in $(seq 7001 7006); \
do \
mkdir -p /data/redis/node-${port}/conf
touch /data/redis/node-${port}/conf/redis.conf
cat << EOF >/data/redis/node-${port}/conf/redis.conf
port  ${port}
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 192.168.1.158
cluster-announce-port ${port}
cluster-announce-bus-port 1${port}
logfile /data/nodes.log
appendonly yes
EOF
docker run -p ${port}:${port} -p 1${port}:1${port} --name redis-${port} \
-v /data/redis/node-${port}/data:/data \
-v /data/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d redis:6.0 redis-server /etc/redis/redis.conf; \
done

4、# 配置&测试集群


docker exec -it redis-7001 bash
redis-cli --cluster create 192.168.1.58:7001 192.168.1.58:7002 192.168.1.58:7003 192.168.1.58:7004 192.168.1.58:7005 192.168.1.58:7006 --cluster-replicas 1
 
 
redis-cli -c -h 192.168.1.58  -p 7001
192.168.1.58:7003> CLUSTER NODES
792a643e61a3db99245635e97bf4726c581150db 192.168.1.58:7002@17002 master - 0 1653903139032 2 connected 5461-10922
3dc1c3791b6c7fe265a7b403c519b10e7318d847 192.168.1.58:7003@17003 myself,master - 0 1653903138000 3 connected 10923-16383
79ecf2544233c2629bed4b1712bd663d99857a3c 192.168.1.58:7004@17004 slave 3dc1c3791b6c7fe265a7b403c519b10e7318d847 0 1653903137000 3 connected
8f6841d0cad18980b17209d7982d00947e0d3859 192.168.1.58:7005@17005 slave 5a4b215a8ab628e155a0ccf5cb3a3765466e8ebf 0 1653903137026 1 connected
f5219260978067fb3c7551cac69fb0a4fba8eede 192.168.1.58:7006@17006 slave 792a643e61a3db99245635e97bf4726c581150db 0 1653903138030 2 connected
5a4b215a8ab628e155a0ccf5cb3a3765466e8ebf 192.168.1.58:7001@17001 master - 0 1653903138000 1 connected 0-5460

5、错误提示:(警告超委托内存设置为0!后台保存可能在低内存条件下失败。写入redis失败,并无报错)

WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

解决:(在宿主机上)

###立即生效,不需要重启
echo 1 > /proc/sys/vm/overcommit_memory
###将参数加入到/etc/sysctl.conf文件中,防止重新失效
vm.overcommit_memory=1

你可能感兴趣的:(redis,docker,redis)