Redis集群环境配置
首先要创建docker内的网络环境
docker network create redis --subnet 172.28.0.0/16
配置文件的第一种声明方式内网运行,这种方式在虚拟机外电脑上会出现Can't connect to master的错误
for port in $(seq 1 6); \
do \
mkdir -p ~/redis/node-${port}/conf
touch ~/redis/node-${port}/conf/redis.conf
cat << EOF > ~/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.28.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
protected-mode no
EOF
done
第二种声明方式,cluster-announce-ip cluster-announce-port,cluster-announce-bus-port均声明为后面所设置的宿主机的端口和IP,后续在redission连接集群的时候就不会有问题
for port in $(seq 1 6); \
do \
mkdir -p ~/redis/node-${port}/conf
touch ~/redis/node-${port}/conf/redis.conf
cat << EOF > ~/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 192.168.200.129
cluster-announce-port 637${port}
cluster-announce-bus-port 1637${port}
appendonly yes
protected-mode no
EOF
done
依次在docker内启动几台Redis
docker run -p 6371:6379 -p 16371:16379 --name redis-1 \
-v ~/redis/node-1/data:/data \
-v ~/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.28.0.11 redis:6.0.5 redis-server /etc/redis/redis.conf
docker run -p 6372:6379 -p 16372:16379 --name redis-2 \
-v ~/redis/node-2/data:/data \
-v ~/redis/node-2/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.28.0.12 redis:6.0.5 redis-server /etc/redis/redis.conf
docker run -p 6373:6379 -p 16373:16379 --name redis-3 \
-v ~/redis/node-3/data:/data \
-v ~/redis/node-3/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.28.0.13 redis:6.0.5 redis-server /etc/redis/redis.conf
docker run -p 6374:6379 -p 16374:16379 --name redis-4 \
-v ~/redis/node-4/data:/data \
-v ~/redis/node-4/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.28.0.14 redis:6.0.5 redis-server /etc/redis/redis.conf
docker run -p 6375:6379 -p 16375:16379 --name redis-5 \
-v ~/redis/node-5/data:/data \
-v ~/redis/node-5/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.28.0.15 redis:6.0.5 redis-server /etc/redis/redis.conf
docker run -p 6376:6379 -p 16376:16379 --name redis-6 \
-v ~/redis/node-6/data:/data \
-v ~/redis/node-6/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.28.0.16 redis:6.0.5 redis-server /etc/redis/redis.conf
进入docker机并使用上述的两种方式连接,推荐第二种,因为后续使用redission连接不会出现错误
docker exec -it redis-1 /bin/sh
redis-cli --cluster create 172.28.0.11:6379 172.28.0.12:6379 172.28.0.13:6379 172.28.0.14:6379 172.28.0.15:6379 172.28.0.16:6379 --cluster-replicas 1
redis-cli --cluster create 192.168.200.129:6371 192.168.200.129:6372 192.168.200.129:6373 192.168.200.129:6374 192.168.200.129:6375 192.168.200.129:6376 --cluster-replicas 1
set name Stephen
// docker stop redis-2
cluster nodes
get name
增加配置文件
for port in $(seq 7 8); \
do \
mkdir -p ~/redis/node-${port}/conf
touch ~/redis/node-${port}/conf/redis.conf
cat << EOF > ~/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.28.0.${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
protected-mode no
EOF
done
增加节点
docker run -p 6377:6379 -p 16377:16379 --name redis-7 \
-v ~/redis/node-7/data:/data \
-v ~/redis/node-7/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.28.0.7 redis:6.0.5 redis-server /etc/redis/redis.conf
扩容和迁移哈希槽
redis-cli --cluster add-node 192.168.200.129:6377 192.168.200.129:6371
redis-cli --cluster reshard 192.168.200.129:6371 --cluster-from
ace4dd7a1bb73e41b1f7a333a3985b97e7924674,a217ca96441fa2b1a7c6fe76488b747eba4fc96d,166f6ff5a32801d938408124edd86bb88206697b --cluster-to e2897d53e41c9d6f84bd2d405d36247aeefee148 --cluster-slots 100
参考 https://stephen520.cn/blog/10271
|