Docker安装Redis集群

安装Docker

yum update
yum install docker -y
systemctl start docker

 安装Redis(三主三从)

准备配置文件

配置文件基本信息

port: 7000
protected-mode: no
daemonize: no
appendonly: yes
cluster-enabled: yes
cluster-config-file: nodes.conf

创建目录

mkdir -p /opt/redis/700{0,1,2,3,4,5}/data

将配置文件信息修改port,写入目录

for i in {0..5}; do
  PORT=$((7000+i))
  echo "port $PORT" > /opt/redis/$PORT/redis.conf
  echo "protected-mode no" >> /opt/redis/$PORT/redis.conf
  echo "daemonize no" >> /opt/redis/$PORT/redis.conf
  echo "appendonly yes" >> /opt/redis/$PORT/redis.conf
  echo "cluster-enabled yes" >> /opt/redis/$PORT/redis.conf
  echo "cluster-config-file nodes.conf" >> /opt/redis/$PORT/redis.conf
done

准备启动脚本

启动脚本基本信息

docker run \
-p 7000:7000 \
-p 17000:17000 \ #需要映射集群端口
-v /opt/redis/7000/redis.conf:/etc/redis/redis.conf \
-v /opt/redis/7000/data:/data \
--name redis-7000 \
-d redis:6.2.6 redis-server /etc/redis/redis.conf

启动脚本写入文件

for i in {0..5}; do
  PORT=$((7000+i))
  echo "docker run \\" > redis-$PORT.sh
  echo "-p $PORT:$PORT \\" >> redis-$PORT.sh
  echo "-p $((17000+i)):$((17000+i)) \\" >> redis-$PORT.sh
  echo "-v /opt/redis/$PORT/redis.conf:/etc/redis/redis.conf \\" >> redis-$PORT.sh
  echo "-v /opt/redis/$PORT/data:/data \\" >> redis-$PORT.sh
  echo "--name redis-$PORT \\" >> redis-$PORT.sh
  echo "-d redis:6.2.6 redis-server /etc/redis/redis.conf" >> redis-$PORT.sh
done

执行脚本文件 

for i in {0..5}; do
  sh redis-$((7000+i)).sh
done

建立集群

#IP为宿主机IP
redis-cli --cluster create --cluster-replicas 1 \
172.31.13.116:7000 172.31.13.116:7001 172.31.13.116:7002 \
172.31.13.116:7003 172.31.13.116:7004 172.31.13.116:7005

访问集群

#加-c访问集群
redis-cli -h 172.31.13.116 -p 7000 -c

#集群信息
172.31.13.116:7000> cluster nodes

#设置key
172.31.13.116:7000> set 123 aaa

#获取key
172.31.13.116:7000> get 123

增加节点(一主一从)

启动

mkdir -p /opt/redis/700{6,7}/data
for i in {6..7}; do
  PORT=$((7000+i))
  echo "port $PORT" > /opt/redis/$PORT/redis.conf
  echo "protected-mode no" >> /opt/redis/$PORT/redis.conf
  echo "daemonize no" >> /opt/redis/$PORT/redis.conf
  echo "appendonly yes" >> /opt/redis/$PORT/redis.conf
  echo "cluster-enabled yes" >> /opt/redis/$PORT/redis.conf
  echo "cluster-config-file nodes.conf" >> /opt/redis/$PORT/redis.conf
done
for i in {6..7}; do
  PORT=$((7000+i))
  echo "docker run \\" > redis-$PORT.sh
  echo "-p $PORT:$PORT \\" >> redis-$PORT.sh
  echo "-p $((17000+i)):$((17000+i)) \\" >> redis-$PORT.sh
  echo "-v /opt/redis/$PORT/redis.conf:/etc/redis/redis.conf \\" >> redis-$PORT.sh
  echo "-v /opt/redis/$PORT/data:/data \\" >> redis-$PORT.sh
  echo "--name redis-$PORT \\" >> redis-$PORT.sh
  echo "-d redis:6.2.6 redis-server /etc/redis/redis.conf" >> redis-$PORT.sh
done
for i in {6..7}; do
  sh redis-$((7000+i)).sh
done

加入集群

#加入主节点
redis-cli --cluster add-node 172.31.13.116:7006 172.31.13.116:7000

#再平衡,移动4096个slots,包含所有节点
redis-cli --cluster reshard 172.31.13.116:7000

#加入从节点
redis-cli --cluster add-node 172.31.13.116:7007 172.31.13.116:7006 --cluster-slave

删除集群

docker stop redis-7000 redis-7001 redis-7002 redis-7003 redis-7004 redis-7005 redis-7006 redis-7007

rm -f /opt/redis/700{0,1,2,3,4,5,6,7}//data/nodes.conf 

docker start redis-7000 redis-7001 redis-7002 redis-7003 redis-7004 redis-7005 redis-7006 redis-7007

其他

启动服务脚本

#!/bin/bash

# Loop from 7000 to 7007
for ((ip=7000; ip<=7007; ip++))
do
    container_name="redis-${ip}"

    if [ "$(docker ps -aq -f name=$container_name)" ]; then
        docker start $container_name
        echo "Started Redis container: $container_name"
    else
        docker run -d --name $container_name -p $ip:6379 redis:latest
        echo "Created and started Redis container: $container_name"
    fi
done

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值