#/bin/bash
# Created by 31 on 2021-02-01
# 集群目录,可自定义
REDIS_BASE_PATH='/home/redis-cluster'
# 获取服务器公网IP
REDIS_IP=$(host myip.opendns.com resolver1.opendns.com | grep "myip.opendns.com has" | awk '{print $4}')
echo "当前公网IP:"${REDIS_IP}
# 停止和删除docker容器
echo "开始停止和删除docker容器..."
while read line
do
docker stop "redis-"$line
docker rm "redis-"$line
done < ./nodes-port-list.txt
echo“docker容器清理完成,开始创建集群配置文件......”
# 写入集群配置文件
cat>${REDIS_BASE_PATH}/redis-cluster.conf<<EOF
port \${PORT}
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip $REDIS_IP
cluster-announce-port \${PORT}
cluster-announce-bus-port 1\${PORT}
appendonly yes
masterauth mypassword
requirepass mypassword
EOF
echo "集群配置文件创建完毕,开始创建节点配置..."
# 创建集群各节点配置文件
while read line
do
port=$line
rm -rf ${REDIS_BASE_PATH}/${port}
mkdir -p ${REDIS_BASE_PATH}/${port}/conf \
&& PORT=${port} envsubst < ${REDIS_BASE_PATH}/redis-cluster.conf > ${REDIS_BASE_PATH}/${port}/conf/redis.conf \
&& mkdir -p ${REDIS_BASE_PATH}/${port}/data; \
done < ./nodes-port-list.txt
echo “节点配置完成,开始创建新docker容器......”
#创建redis docker容器
while read line
do
port=$line
docker run -d -it \
-v /home/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /home/redis-cluster/${port}/data:/data \
--restart always --name redis-${port} --net host \
redis:latest redis-server /usr/local/etc/redis/redis.conf; \
done < ./nodes-port-list.txt
echo “docker容器创建完成,开始进行集群配置......”
# 拼接集群列表
NODE_LIST=""
while read line
do
NODE=${REDIS_IP}":"${line}
NODE_LIST=${NODE_LIST}" "${NODE}
done < ./nodes-port-list.txt
# 执行命令创建集群
redis-cli --cluster create $NODE_LIST --cluster-replicas 1 -a mypassword
echo "集群创建完成!"
exit 0
nodes-port-list.txt
6111
6112
6113
6114
6115
6116
====================
#!/bin/bash
#服务器内网ip
ip="172.16.252.126"
#redis镜像版本
version="5.0.0"
#集群文件防止目录
redisdir="/home/redis-cluster"
#判断问加减是否存在 如果存在删除后创建,不存在创建
rm -f -r ${redisdir}
# if [ -f "${redisdir}"} ];then
# rm -f -r ${redisdir}
# fi
mkdir -p ${redisdir}
echo "构建redis集群目录..."
for port in `seq 7000 7005`; do \
mkdir -p ${redisdir}/${port}/conf \
&& PORT=${port} IP=${ip} envsubst < ./redis-cluster.tmpl > ${redisdir}/${port}/conf/redis.conf \
&& mkdir -p ${redisdir}/${port}/data; \
done
echo "拉取redis:${version}镜像..."
#拉取redis镜像
docker pull redis:${version}
#为redis集群创建docker网桥
docker network create redis-net
echo "创建网桥..."
#查看docker所有的网桥
docker network ls
echo "创建并运行redis集群容器..."
#定义一个初始值变量,用于叠加ip
execsh='/usr/local/bin/redis-cli --cluster create '
#创建redis运行容器
for port in `seq 7000 7005`; do \
docker run -d -ti -p ${port}:${port} -p 1${port}:1${port} \
-v ${redisdir}/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v ${redisdir}/${port}/data:/data \
--restart always --name redis-${port} --net redis-net \
--sysctl net.core.somaxconn=1024 redis:${version} redis-server /usr/local/etc/redis/redis.conf; \
#获取docker分配的ip
execsh=${execsh}`docker inspect redis-${port} | grep "IPAddress" | grep --color=auto -P '(\d{1,3}.){3}\d{1,3}' -o`:${port}' '
done
execsh=${execsh}'--cluster-replicas 1'
#将拼接好的命令打印到控制台
echo ${execsh}
#将控制台的打印命令写入exe.sh脚本,docker容器直接执行就好
echo "${execsh}" > ${redisdir}/7000/data/exe.sh
#给exe.sh权限
chmod 777 ${redisdir}/7000/data/exe.sh
#进入第一个redis容器内
docker exec -it redis-7000 bash
redis-cluster.tmpl
port ${PORT}
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip ${IP}
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
appendonly yes
redis-cli --cluster create 192.168.66.101:6111 \
192.168.66.101:6112 192.168.66.101:6113 \
192.168.66.101:6114 192.168.66.101:6115 \
192.168.66.101:6116 --cluster-replicas 1 -a mypassword
|