redis 三主六从高可用docker(不固定ip)

redis集群(cluster)笔记

redis 三主三从高可用集群docker swarm

redis 三主六从高可用docker(不固定ip)

此博客解决,redis加入集群后,是用于停掉后重启,将nodes.conf中的旧的Ip替换为新的IP,从而达到不会因为IP变化导致集群无法正常使用

跨主机安装rediscluster集群,本文采用swarm的方式,使用同一个网络,然后分别在对应的机器启动

1.环境准备

  • docker
  • docker-compose
  • swarm集群
  • 安装文件

1.1 swarm环境安装

主机 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

1.2 在3台主机上配置swarm

  • 在node1上执行:
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
  • 在node2、node3上执行上面的返回结果:
docker swarm join --token SWMTKN-1-2c2xopn2rld8oltcof24sue370681ijhbo3bwcqarjlhq9lkea-2g53o5qn2anre4j9puv4hecrn 192.168.0.101:2377

1.3 创建swarm网络

在node1上执行以下命令:

docker network create -d overlay --attachable redis-net

1.4 查看node

docker node ls

在这里插入图片描述

2.安装文件准备

2.1 redisnode1文件

filePath="/home/redis/redisnode1"
mkdir -p $filePath
cd $filePath
cat > ${filePath}/docker-compose.yml << EOF
version: '3'

networks:
  redis-net:
    external: true 

services:
  redis1:
    image: redis:6.0-alpine
    hostname: redis1
    container_name: redis1
    volumes:
      - "\$PWD/redis1/data:/data"
      - "\$PWD/redis.conf:/etc/redis.conf"
      - "\$PWD/redisStart.sh:/redis/redisStart.sh"
    command: "sh /redis/redisStart.sh"
    restart: always
    privileged: true
    networks:
     - redis-net
  redis2:
    image: redis:6.0-alpine
    hostname: redis2
    container_name: redis2
    volumes:
      - "\$PWD/redis2/data:/data"
      - "\$PWD/redis.conf:/etc/redis.conf"
      - "\$PWD/redisStart.sh:/redis/redisStart.sh"
    command: "sh /redis/redisStart.sh"
    restart: always
    privileged: true
    networks:
     - redis-net
  redis3:
    image: redis:6.0-alpine
    hostname: redis3
    container_name: redis3
    volumes:
      - "\$PWD/redis3/data:/data"
      - "\$PWD/redis.conf:/etc/redis.conf"
      - "\$PWD/redisStart.sh:/redis/redisStart.sh"
    command: "sh /redis/redisStart.sh"
    restart: always
    privileged: true
    networks:
     - redis-net
EOF

cat > ${filePath}/redis.conf << EOF
port 6379
masterauth '123456' 
requirepass '123456'
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
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
}

# 通过ifconfig命令获取IP地址
IP=\$(ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{print \$1}')
log "Container IP Address is: \${IP}"
CLUSTER_CONFIG="/data/nodes.conf"

if [ -f \${CLUSTER_CONFIG} ]; then
    if [ -z "\${IP}" ]; then
	log "Unable to determine 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}/redisSalveTem.sh << EOF
#!/bin/bash

# redis1 容器内执行集群创建

# redis1  redis4  redis7
# redis2  redis5  redis8
# redis3  redis6  redis9
# 1 5 9
# 4 2 8
# 7 3 6
# 进入 redis1 容器
docker exec -it redis1 /bin/sh
# 指定主从节点交叉分布
# redis1 容器内执行集群创建
# 创建主节点
redis-cli --cluster create REDIS1:6379 REDIS4:6379 REDIS7:6379 -a '123456'

# 确认
yes

# 指定主从节点交叉分布

redis-cli --cluster add-node REDIS5:6379 REDIS1:6379 --cluster-slave -a '123456'
redis-cli --cluster add-node REDIS9:6379 REDIS1:6379 --cluster-slave -a '123456'
redis-cli --cluster add-node REDIS2:6379 REDIS4:6379 --cluster-slave -a '123456'
redis-cli --cluster add-node REDIS8:6379 REDIS4:6379 --cluster-slave -a '123456'
redis-cli --cluster add-node REDIS3:6379 REDIS7:6379 --cluster-slave -a '123456'
redis-cli --cluster add-node REDIS6:6379 REDIS7:6379 --cluster-slave -a '123456'

# 检查集群状态"
redis-cli -c -a '123456'
# 查看集群节点 9个
CLUSTER NODES

# 退出 redis
exit

# 退出容器
exit

EOF

cat > ${filePath}/redisSetIp.sh << EOF
#!/bin/bash
\cp redisSalveTem.sh redisSalve.sh

function getIp(){
    redis1=\$(docker exec -it redis1 sh -c "ping \$1 -c 1 | sed '1{s/[^(]*(//;s/).*//;q}'")
    echo "\${redis1}"
}



echo "redis1=\$(getIp redis1)"
echo "redis2=\$(getIp redis2)"
echo "redis3=\$(getIp redis3)"
echo "redis4=\$(getIp redis4)"
echo "redis5=\$(getIp redis5)"
echo "redis6=\$(getIp redis6)"
echo "redis7=\$(getIp redis7)"
echo "redis8=\$(getIp redis8)"
echo "redis9=\$(getIp redis9)"


echo 'sed -i "s#REDIS1#\$redis1#g" redisSalve.sh'
echo 'sed -i "s#REDIS2#\$redis2#g" redisSalve.sh'
echo 'sed -i "s#REDIS3#\$redis3#g" redisSalve.sh'
echo 'sed -i "s#REDIS4#\$redis4#g" redisSalve.sh'
echo 'sed -i "s#REDIS5#\$redis5#g" redisSalve.sh'
echo 'sed -i "s#REDIS6#\$redis6#g" redisSalve.sh'
echo 'sed -i "s#REDIS7#\$redis7#g" redisSalve.sh'
echo 'sed -i "s#REDIS8#\$redis8#g" redisSalve.sh'
echo 'sed -i "s#REDIS9#\$redis9#g" redisSalve.sh'
echo 'cat redisSalve.sh'
EOF

2.2 redisnode2文件

filePath="/home/redis/redisnode2"
mkdir -p $filePath
cat > ${filePath}/docker-compose.yml << EOF
version: '3'

networks:
  redis-net:
    external: true 

services:
  redis4:
    image: redis:6.0-alpine
    hostname: redis4
    container_name: redis4
    volumes:
      - "\$PWD/redis4/data:/data"
      - "\$PWD/redis.conf:/etc/redis.conf"
      - "\$PWD/redisStart.sh:/redis/redisStart.sh"
    command: "sh /redis/redisStart.sh"
    restart: always
    privileged: true
    networks:
     - redis-net
  redis5:
    image: redis:6.0-alpine
    hostname: redis5
    container_name: redis5
    volumes:
      - "\$PWD/redis5/data:/data"
      - "\$PWD/redis.conf:/etc/redis.conf"
      - "\$PWD/redisStart.sh:/redis/redisStart.sh"
    command: "sh /redis/redisStart.sh"
    restart: always
    privileged: true
    networks:
     - redis-net
  redis6:
    image: redis:6.0-alpine
    hostname: redis6
    container_name: redis6
    volumes:
      - "\$PWD/redis6/data:/data"
      - "\$PWD/redis.conf:/etc/redis.conf"
      - "\$PWD/redisStart.sh:/redis/redisStart.sh"
    command: "sh /redis/redisStart.sh"
    restart: always
    privileged: true
    networks:
     - redis-net
EOF

cat > ${filePath}/redis.conf << EOF
port 6379
masterauth '123456' 
requirepass '123456'
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
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
}

# 通过ifconfig命令获取IP地址
IP=\$(ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{print \$1}')
log "Container IP Address is: \${IP}"
CLUSTER_CONFIG="/data/nodes.conf"

if [ -f \${CLUSTER_CONFIG} ]; then
    if [ -z "\${IP}" ]; then
	log "Unable to determine 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

2.3 redisnode3文件

filePath="/home/redis/redisnode3"
mkdir -p $filePath
cat > ${filePath}/docker-compose.yml << EOF
version: '3'

networks:
  redis-net:
    external: true 

services:
  redis7:
    image: redis:6.0-alpine
    hostname: redis7
    container_name: redis7
    volumes:
      - "\$PWD/redis7/data:/data"
      - "\$PWD/redis.conf:/etc/redis.conf"
      - "\$PWD/redisStart.sh:/redis/redisStart.sh"
    command: "sh /redis/redisStart.sh"
    restart: always
    privileged: true
    networks:
     - redis-net
  redis8:
    image: redis:6.0-alpine
    hostname: redis8
    container_name: redis8
    volumes:
      - "\$PWD/redis8/data:/data"
      - "\$PWD/redis.conf:/etc/redis.conf"
      - "\$PWD/redisStart.sh:/redis/redisStart.sh"
    command: "sh /redis/redisStart.sh"
    restart: always
    privileged: true
    networks:
     - redis-net
  redis9:
    image: redis:6.0-alpine
    hostname: redis9
    container_name: redis9
    volumes:
      - "\$PWD/redis9/data:/data"
      - "\$PWD/redis.conf:/etc/redis.conf"
      - "\$PWD/redisStart.sh:/redis/redisStart.sh"
    command: "sh /redis/redisStart.sh"
    restart: always
    privileged: true
    networks:
     - redis-net
EOF

cat > ${filePath}/redis.conf << EOF
port 6379
masterauth '123456' 
requirepass '123456'
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
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
}

# 通过ifconfig命令获取IP地址
IP=\$(ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{print \$1}')
log "Container IP Address is: \${IP}"
CLUSTER_CONFIG="/data/nodes.conf"

if [ -f \${CLUSTER_CONFIG} ]; then
    if [ -z "\${IP}" ]; then
	log "Unable to determine 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

3. 启动 redisnode1

进入 node1服务器

cd /home/redis/redisnode1

docker-compose up -d 

4. 启动 redisnode2

进入 node2服务器

cd /home/redis/redisnode2

docker-compose up -d 

5. 启动 redisnode3

进入 node3服务器

cd /home/redis/redisnode3

docker-compose up -d 

6. 创建集群

进入 node1服务器

bash redisSetIp.sh

redis 三主六从高可用docker(不固定ip)_第1张图片

复制上面打印的命令进行执行生成加入集群的语句
redis 三主六从高可用docker(不固定ip)_第2张图片

按照步骤一步一步的加入

# redis1 容器内执行集群创建

# redis1  redis4  redis7
# redis2  redis5  redis8
# redis3  redis6  redis9
# 1 5 9
# 4 2 8
# 7 3 6
# 进入 redis1 容器
docker exec -it redis1 /bin/sh
# 指定主从节点交叉分布
# redis1 容器内执行集群创建
# 创建主节点
redis-cli --cluster create 10.0.1.149:6379 10.0.1.157:6379 10.0.1.29:6379 -a '123456'

# 确认
yes

# 指定主从节点交叉分布

redis-cli --cluster add-node 10.0.1.156:6379 10.0.1.149:6379 --cluster-slave -a '123456'
redis-cli --cluster add-node 10.0.1.176:6379 10.0.1.149:6379 --cluster-slave -a '123456'
redis-cli --cluster add-node 10.0.1.150:6379 10.0.1.157:6379 --cluster-slave -a '123456'
redis-cli --cluster add-node 10.0.1.175:6379 10.0.1.157:6379 --cluster-slave -a '123456'
redis-cli --cluster add-node 10.0.1.146:6379 10.0.1.29:6379 --cluster-slave -a '123456'
redis-cli --cluster add-node 10.0.1.158:6379 10.0.1.29:6379 --cluster-slave -a '123456'

# 检查集群状态"
redis-cli -c -a '123456'
# 查看集群节点 9个
CLUSTER NODES

# 退出 redis
exit

# 退出容器

redis 三主六从高可用docker(不固定ip)_第3张图片

7.测试用例

7.1 用例1

设置一个值
将对应值的节点关闭
查看集群,查看设置的参数
启动关闭的节点,查看集群是否加入

在这里插入图片描述
在这里插入图片描述redis 三主六从高可用docker(不固定ip)_第4张图片
在这里插入图片描述
redis 三主六从高可用docker(不固定ip)_第5张图片

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