redis 三主六从高可用dockerswarm高级版(不固定ip)
此博客解决,redis加入集群后,是用于停掉后重启,将nodes.conf中的旧的Ip替换为新的IP,从而达到不会因为IP变化导致集群无法正常使用,以及使用docker stack 启动多个副本 ,自动化脚本加入集群
跨主机安装rediscluster集群,本文采用swarm的方式,使用同一个网络,通过对挂载目录的使配置文件互相同步,从而让redis集群失败自重启达到集群的高可用。
主机 | IP |
---|---|
node1 | 192.168.56.100 |
node2 | 192.168.56.101 |
node3 | 192.168.56.102 |
在三台分别执行 开放防火墙:
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.100" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.101" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.102" accept"
firewall-cmd --reload
firewall-cmd --list-all
systemctl restart docker
docker swarm init --advertise-addr 192.168.56.100
docker swarm join-token manager
返回类似以下内容:
docker swarm join --token SWMTKN-1-614xi9dvksycykobgifxb4pgopc1wwgczwqct5wqkq8zao6tmx-0ds4jj3ozclrr2wukcaoakxso 192.168.56.100:2377
docker swarm join --token SWMTKN-1-2c2xopn2rld8oltcof24sue370681ijhbo3bwcqarjlhq9lkea-2g53o5qn2anre4j9puv4hecrn 192.168.0.101:2377
在node1上执行以下命令:
docker network create -d overlay --attachable scsdm-net
docker node ls
/home/redis 目录在三台机器互相同步监听可以查看这个文档进行部署,如果使用k3s,k8s及其他编排方式,使用同一个挂载目录就不进行文件同步
filePath="/home/redis"
mkdir -p ${filePath}/data
cd $filePath
cat > ${filePath}/docker-stack-redis.yml << EOF
version: '3'
networks:
scsdm-net:
external: true
services:
redis-cluster:
image: redis:6.2.4-alpine
container_name: "redis-cluster{{.Task.Slot}}"
hostname: "redis-cluster"
environment:
- MY_TASK_SLOT={{.Task.Slot}}
volumes:
- "$filePath/data:/data"
- "$filePath/redisStart.sh:/redis/redisStart.sh"
command: "sh /redis/redisStart.sh"
restart: always
privileged: true
networks:
- scsdm-net
deploy:
replicas: 9
EOF
cat > ${filePath}/redisStart.sh << EOF
#!/bin/sh
CURRENT_DIR=\$(
cd "\$(dirname "\$0")"
pwd
)
nowDate=\$(date "+%Y%m%d%H%M%S")
logFileName="redisStart_"\$nowDate.log
touch \$CURRENT_DIR/\${logFileName}
# 日志函数
log(){
echo "["\$(date "+%Y-%m-%d %H:%M:%S")"]"\$1 | tee -a \$CURRENT_DIR/\$logFileName
}
# 创建当前的
mkdir -p /data/\${MY_TASK_SLOT}
redisConfPath="/etc/redis.conf"
echo "" >> \$redisConfPath
echo "dir /data/\${MY_TASK_SLOT}" >> \$redisConfPath
echo "port 6379" >> \$redisConfPath
echo "masterauth '123456'" >> \$redisConfPath
echo "requirepass '123456'" >> \$redisConfPath
echo "cluster-enabled yes" >> \$redisConfPath
echo "cluster-config-file nodes.conf" >> \$redisConfPath
echo "cluster-node-timeout 5000" >> \$redisConfPath
echo "# 禁用AOF" >> \$redisConfPath
echo "appendonly no" >> \$redisConfPath
# 通过ifconfig命令获取IP地址
IP=\$(ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{print \$1}')
log "Container IP Address is: \${IP}"
CLUSTER_CONFIG="/data/\${MY_TASK_SLOT}/nodes.conf"
echo "\$IP" > /data/\${MY_TASK_SLOT}/ip
if [ -f \${CLUSTER_CONFIG} ]; then
if [ -z "\${IP}" ]; then
log "Unable to determine Pod IP address!"
exit 1
fi
log "Updating my IP to \${IP} in \${CLUSTER_CONFIG}"
sed -i -e "/myself/s/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/\${IP}/g" \${CLUSTER_CONFIG}
fi
redis-server /etc/redis.conf
EOF
cat > ${filePath}/join-redis-cluster.sh << EOF
#!/bin/bash
CURRENT_DIR=\$(
cd "\$(dirname "\$0")"
pwd
)
# 指定要遍历的目录
directory="\${CURRENT_DIR}/data"
# 指定要查找的文件名
filename="ip"
temFileName="ipTemp.txt"
rm -rf \$temFileName
find "\$directory" -type f -name "\$filename" | while read -r file
do
echo -n "\$(cat \$file):6379 " >> \$temFileName
done
REDIS_CLUSTER_CMD="redis-cli --cluster create "\$(cat \$temFileName)" --cluster-replicas 2 -a '123456'"
rm -rf \$temFileName
# 获取容器名称
REDIS_CONTAINER_NAME=\$(docker ps -a | grep "redis-cluster" | awk '{print \$1}'| head -n 1 )
# 生成 redis-cli 命令
echo "生成 redis-cli 命令:\$REDIS_CLUSTER_CMD"
# 进入 Redis 容器并执行命令
DOCKER_EXEC_CMD="echo 'yes' | docker exec -i \$REDIS_CONTAINER_NAME sh -c \"\$REDIS_CLUSTER_CMD\""
echo "Executing Docker Exec Command:"
echo "\$DOCKER_EXEC_CMD"
echo 'yes' | docker exec -i \$REDIS_CONTAINER_NAME sh -c "\$REDIS_CLUSTER_CMD"
echo "查询节点情况"
echo "docker exec -it \$REDIS_CONTAINER_NAME sh -c \"redis-cli -c -a '123456'\""
echo "cluster nodes"
echo "测试其他连接情况"
echo "docker run -it --rm --net=scsdm-net redis:6.2.4-alpine sh -c \"redis-cli -h redis-cluster -c -a '123456'\""
EOF
cat > ${filePath}/startRedis.sh << EOF
#!/bin/bash
docker stack deploy -c docker-stack-redis.yml redis
EOF
cat > ${filePath}/stopReids.sh << EOF
#!/bin/bash
docker stack rm redis
EOF
cat > ${filePath}/restartRedis.sh << EOF
#!/bin/bash
# rm
docker stack rm redis
# start
docker stack deploy -c docker-stack-redis.yml redis
EOF
cat > ${filePath}/showRedisStatus.sh << EOF
#!/bin/bash
# show status
docker stack ps redis --no-trunc
EOF
进入 node1
服务器
cd ${filePath}
bash startRedis.sh
ls data
当出现下面文件后执行命令加入集群
bash join-redis-cluster.sh
springboot 配置集群
spring.redis.port = 6379
spring.redis.password = 123456
spring.redis.cluster.nodes[0] = redis-cluster:6379
设置一个值
将对应值的节点关闭
查看集群是否全部启动正常
查看设置的参数
结果:
master改变了,服务自动创建了新的节点自动加入
重启redis 查看集群是否正常
全部重启暂时不正常,可以重新部署
使用同一个网络连接集群,查看值
docker run -it --rm --net=scsdm-net redis:6.2.4-alpine sh -c "redis-cli -h redis-cluster -c -a '123456'"