原创

Docker部署redis集群-Cluster

由于硬件资源有限,redis集群在一台机器上以docker方式部署

redis集群是3主3从

一、创建数据存储目录、配置文件

# 创建目录
mkdir -p /data/redis-data/
cd /data/redis-data/

# 创建数据存储目录
mkdir -p {node1,node2,node3,node4,node5,node6}

# 创建配置文件目录
mkdir conf

# 创建配置文件,并将配置写入,IP用docker网络分配
for node in $(seq 1 6)
do
touch conf/redis-${node}.conf
cat << EOF > conf/redis-${node}.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.20.0.1${nodes}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done

# 授权
chmod -R 775 /data/redis-data/

二、docker启动配置redis并创建集群

创建docker网路

# 该网段已写入第一步的配置文件
docker network create redis --subnet 172.20.0.0/16

启动reids

for node in $(seq 1 6)
do

docker run -p 637${node}:6379 -p 16371:16379 --name redis-${node} -v /data/redis-data/node${node}:/data -v /data/redis-data/conf/redis-${node}.conf:/etc/redis/redis.conf -d --net redis --ip 172.20.0.1${node} redis redis-server /etc/redis/redis.conf

done

创建集群

# 进入reids节点
docker exec -it redis-1 bash

# 创建集群
redis-cli --cluster create 172.20.0.11:6379 172.20.0.12:6379 172.20.0.13:6379 172.20.0.14:6379 172.20.0.15:6379 172.20.0.16:6379 --cluster-replicas 1


三、docker部署集群脚本

#/bin/bash
# 定义变量
redisdata=/data/redis-data/node
redisconf=/data/redis-data/conf/

# 创建docker网络
docker network create redis --subnet 172.20.0.0/16

# 创建配置文件和存储目录
mkdir -p /data/redis-data
mkdir ${redisconf}

for nodes in $(seq 1 6)
do

# 创建目录
mkdir ${redisdata}${nodes}

# 创建配置文件
touch ${redisconf}redis-${nodes}.conf
cat << EOF > ${redisconf}redis-${nodes}.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.20.0.1${nodes}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
chmod 775 -R /data/redis-data

# 启动容器
echo "start docker node${nodes}"
docker run -p 637${nodes}:6379 -p 1637${nodes}:16379 --name redis-${nodes} -v ${redisdata}${nodes}:/data -v ${redisconf}redis-${nodes}.conf:/etc/redis/redis.conf -d --net redis --ip 172.20.0.1${nodes} redis redis-server /etc/redis/redis.conf

done


脚本执行完后,执行创建集群操作

# 进入reids节点
docker exec -it redis-1 bash

# 创建集群
redis-cli --cluster create 172.20.0.11:6379 172.20.0.12:6379 172.20.0.13:6379 172.20.0.14:6379 172.20.0.15:6379 172.20.0.16:6379 --cluster-replicas 1


四、集群验证

# 查看集群信息

# 进入任意节点的redis
redis-cli -c

#查看信息 
127.0.0.1:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:7542
cluster_stats_messages_pong_sent:7620
cluster_stats_messages_sent:15162
cluster_stats_messages_ping_received:7615
cluster_stats_messages_pong_received:7542
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:15162
total_cluster_links_buffer_limit_exceeded:0

# 查看节点信息
127.0.0.1:6379> cluster nodes
b3c50cc6f899b7318f89453907032ca6f101d332 172.20.0.12:6379@16379 master - 0 1694595377832 2 connected 5461-10922
35d64f728a856e22a23505a1ad500ff3b8530891 172.20.0.14:6379@16379 slave 99fa3923efaf079f28572fc31bfe4676db7dd705 0 1694595378000 3 connected
ae2bfb6c217e695b88205aab041d6cec0744f21d 172.20.0.11:6379@16379 myself,master - 0 1694595377000 1 connected 0-5460
99fa3923efaf079f28572fc31bfe4676db7dd705 172.20.0.13:6379@16379 master - 0 1694595378840 3 connected 10923-16383
0a9632f30b6c4d939cc6c8e1bd2bdad9782d92c1 172.20.0.16:6379@16379 slave b3c50cc6f899b7318f89453907032ca6f101d332 0 1694595378538 2 connected
750f532850bf13428dfac831ff3b0aa3d4698a78 172.20.0.15:6379@16379 slave ae2bfb6c217e695b88205aab041d6cec0744f21d 0 1694595379547 1 connected
正文到此结束
评论

登录后才能发表评论 登录/注册

0评论
  • 还没有评论,快来抢沙发吧!