docker搭建redis集群

13 篇文章 0 订阅
#!/bin/bash
#Author: 臆想的一只猫
#Created: 2022-04-06 17:42:33
#Description: 搭建redis集群

function menu() {
  
  clear
  echo -e "\t\033[1;3;34m----------搭建redis集群----------\033[0m\n"
  echo -e "\t\t\033[32m0、Exit menu\033[0m"
  echo -e "\t\t\033[32m1、搭建6主6从服务器\033[0m"
  echo -e "\t\t\033[32m2、主从扩容案例\033[0m"
  echo -e "\t\t\033[32m3、主从缩容案例\033[0m"
  echo -en "\033[46;37mEnter option:\033[0m\t"
  read -n 1 option
}

menu

# 启动redis集群
VERSION='redis:6.0.8'
function start_redis() {
  # --net host:使用宿主机的IP和端口,默认
  # --cluster-enabled:开启redis集群
  # --appendonly:开启持久化 
  for((i=1;i<=6;i++))
  do
    docker run -d --name redis-node-${i} --net host --privileged=true -v /data/volume/redis-cluster/redis-node-${i}:/data ${VERSION} --cluster-enabled yes --appendonly yes --port 638${i}
  done
}

HOST_IP=`ifconfig ens33 | awk 'NR==2{print $2}'`
function print_msg() {

  echo '进入:docker exec -it redis-node-1 /bin/bash'
  echo "执行:redis-cli --cluster create ${HOST_IP}:6381 ${HOST_IP}:6382 ${HOST_IP}:6383 ${HOST_IP}:6384 ${HOST_IP}:6385 ${HOST_IP}:6386 --cluster-replicas 1"
  echo '进入redis:redis-cli -p 6381 -c'
  echo '查看集群信息:cluster info'
  echo '查看集群结点:cluster nodes'
  echo "检查集群状态:redis-cli --cluster check ${HOST_IP}:6381"
  echo '当Master挂掉之后,Slave将会变成Master继续工作;当Master重新启动之后,将会变成Slave工作'
} 

IS_REDIS=`docker images | grep redis | grep 6.0.8`
function create_redis_cluster() {

  if [[ ${IS_REDIS} ]];
  then
    echo '----------开始创建redis集群----------'
    start_redis
    echo '----------redis集群创建完成----------'
    docker ps
    print_msg
  else
    docker pull ${VERSION}
    echo '----------开始创建redis集群----------'
    start_redis
    echo '----------redis集群创建完成----------'
    docker ps 
    print_msg
  fi
}

# redis集群扩容
function add_redis_cluster() {
  
  for((i=7;i<=8;i++))
  do
    docker run -d --name redis-node-${i} --net host --privileged=true -v /data/volume/redis-cluster/redis-node-${i}:/data ${VERSION} --cluster-enabled yes --appendonly yes --port 638${i}
  done
  docker ps
  echo '进入6387内部-----> docker exec -it redis-node-7 /bin/bash'
  echo "将新增的6387作为master节点加入集群 ----- redis-cli --cluster add-node ${HOST_IP}:6387 ${HOST_IP}:6381"
  echo "检查集群状态 ----- redis-cli --cluster check ${HOST_IP}:6381"
  echo "重新分派槽号 ----- redis-cli --cluster reshard ${HOST_IP}:6381"
  echo -e "\t\tHow many slots do you want to move(from 1 to 16384)? ----- 16384 / Master数量 = 4096"
  echo -e "\t\tWhat is the receiving node ID? ----- 新增Master主机的nodeId ------ eg:35291fb3a26...."
  echo -e "\t\tSource node #1:----- all"
  echo "检查集群状态 ----- redis-cli --cluster check ${HOST_IP}:6381"
  echo '添加新的集群的时候,hash槽不是重新分配,而是之前的匀出来一些给新增加的机器'
  echo '为Master:6387分配Slave:6388'
  echo "redis-cli --cluster add-node ${HOST_IP}:6388 ${HOST_IP}:6387 --cluster-slave --cluster-master-id 35291fb3a26....[这个是6387的编号,根据check出来的填写]"
  echo "检查集群状态 ----- redis-cli --cluster check ${HOST_IP}:6381"
  docker exec -it redis-node-7 /bin/bash
}

# redis集群缩容
function abatement_redis_cluster() {

  echo "检查集群状态 ----- redis-cli --cluster check ${HOST_IP}:6381"
  echo "删除Slave:6388 ----- redis-cli --cluster del-node ${HOST_IP}:6388 xxxxxxx[6388的编号,根据check出来的填写]"
  echo '将6387的槽号清空,本例中全部分配给6381'
  echo "重新分派槽号 ----- redis-cli --cluster reshard ${HOST_IP}:6381"
  echo -e "\t\tHow many slots do you want to move(from 1 to 16384)? ----- 16384 / Master数量 = 4096"
  echo -e "\t\tWhat is the receiving node ID? ----- Master:6381的nodeId ------ eg:35291fb3a26....[接收者]"
  echo -e "\t\tSource node #1:----- Master:6387的nodeId ------ eg:35291fb3a26....[贡献者]"
  echo -e "\t\tSource node #2:----- done"
  echo "检查集群状态 ----- redis-cli --cluster check ${HOST_IP}:6381"
  echo "删除Master:6387 ----- redis-cli --cluster del-node ${HOST_IP}:6387 xxxxx[6387的编号]"
  echo "检查集群状态 ----- redis-cli --cluster check ${HOST_IP}:6381"
  docker exec -it redis-node-1 /bin/bash
}

case $option in
0)
  echo -e "\n\n\n\n\033[3;1;35m已退出!!!\033[0m\n\n"
  exit;;
1)
  echo ''
  create_redis_cluster;;
2)
  echo ''
  add_redis_cluster;;
3)
  echo ''
  abatement_redis_cluster;;
*)
  clear
  echo -e "\n\n\033[3;1;35m键入参数有误,GoodBey!!!\033[0m\n\n";;
esac

部署

#!/bin/bash

VOLUME_PATH='/data/volumes/redis-cluster'
VERSION='redis:6.0.8'

for port in $(seq 2001 2006);
do
  mkdir -p ${VOLUME_PATH}/redis-node-${port}/conf
  touch ${VOLUME_PATH}/redis-node-${port}/conf/redis.conf
  cat << EOF >${VOLUME_PATH}/redis-node-${port}/conf/redis.conf
port ${port}
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 1.117.196.59
cluster-announce-port ${port}
cluster-announce-bus-port 1${port}
appendonly yes
EOF

docker run -p ${port}:${port} -p 1${port}:1${port} --name redis-${port} --privileged=true --restart=always -v ${VOLUME_PATH}/redis-node-${port}/data:/data -v ${VOLUME_PATH}/redis-node-${port}/conf/redis.conf:/etc/redis/redis.conf -d ${VERSION} redis-server /etc/redis/redis.conf;
done



# 外部访问使用公网IP redis-cli --cluster create ${HOST_IP}:2001 ${HOST_IP}:2002 ${HOST_IP}:2003 ${HOST_IP}:2004 ${HOST_IP}:2005 ${HOST_IP}:2006 --cluster-replicas 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值