Docker初见(四)Docker 安装Redis集群

本次安装Redis版本如下:

  • Redis 5.0.5
  • 集群创建镜像:inem0o/redis-trib

1.创建文件夹

cd /home
mkdir redis-cluster 
cd redis-cluster

2.编写redis配置文件redis-cluster.tmpl#端口

port ${PORT}
#开启集群
cluster-enabled yes
#配置文件
cluster-config-file nodes.conf
cluster-node-timeout 5000
#更新操作后进行日志记录
appendonly yes
#保护模式是禁止公网访问
protected-mode no
#设置主服务的连接密码
masterauth a123456
#设置从服务的连接密码
requirepass a123456

3.编写shell文件 redis.sh

#!/bin/bash
 
#在/home/redis-cluster下生成conf和data目标,并生成配置信息
for port in `seq 8000 8005`; 
do 
  mkdir -p ./${port}/conf && PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf && mkdir -p ./${port}/data;
done
 
#创建6个redis容器
for port in `seq 8000 8005`;
do
    docker run -d -it -p ${port}:${port} -p 1${port}:1${port} -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 redis-net --sysctl net.core.somaxconn=1024 redis:5.0.5 redis-server /usr/local/etc/redis/redis.conf;
done
 
#查找ip
for port in `seq 8000 8005`;
do
    echo  -n "$(docker inspect --format '{{ (index .NetworkSettings.Networks "redis-net").IPAddress }}' "redis-${port}")":${port}" ";
done
 
#换行
echo -e "\n"
 
#输入信息
read -p "请把输入要启动的docker容器名称,默认redis-8000:" DOCKER_NAME
 
#判断是否为空
if [ ! $DOCKER_NAME ]; 
    then DOCKER_NAME='redis-8000'; 
fi
 
#进入容器
docker exec -it redis-8000 /bin/bash

4.编写stop.sh:卸载redis集群

#!/bin/bash
docker stop redis-8000 redis-8001 redis-8002 redis-8003 redis-8004 redis-8005
docker rm redis-8000 redis-8001 redis-8002 redis-8003 redis-8004 redis-8005
rm -rf 8000 8001 8002 8003 8004 8005

5.设置权限:

chmod 755 redis.sh stop.sh

6.创建自定义network(redis-net)

docker network create redis-net

7.打开防火墙端口

firewall-cmd --zone=public --add-port=8000-8005/tcp --permanent
firewall-cmd --zone=public --add-port=18000-18005/tcp --permanent
firewall-cmd --reload

8.执行redis.sh脚本自动创建redis容器,然后自动进入redis-8000

./redis.sh

9.执行集群,然后输入:yes,启动集群(-a a123456 是密码)。

redis-cli --cluster create 172.19.0.2:8000 172.19.0.3:8001 172.19.0.4:8002 172.19.0.5:8003 172.19.0.6:8004 172.19.0.7:8005 -a a123456 --cluster-replicas 1

日志如下:

[root@localhost redis-cluster]# redis-cli --cluster create 172.19.0.2:8000 172.19.0.3:8001 172.19.0.4:8002 172.19.0.5:8003 172.19.0.6:8004 172.19.0.7:8005 -a a123456 --cluster-replicas 1
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.19.0.4:8001
172.19.0.2:8002
172.19.0.6:8003
Adding replica 172.19.0.5:8004 to 172.19.0.4:8001
Adding replica 172.19.0.3:8005 to 172.19.0.2:8002
Adding replica 172.19.0.7:8006 to 172.19.0.6:8003
M: 2c95d2d6cfd54060c86c3c9f3170aa6913ed3340 172.19.0.4:8001
   slots:0-5460 (5461 slots) master
M: 74363f850970e1136c17a8bcb6a51fde9685d9c0 172.19.0.2:8002
   slots:5461-10922 (5462 slots) master
M: 70e19b8347b917e23d44c43d451a7c47479c97e3 172.19.0.6:8003
   slots:10923-16383 (5461 slots) master
S: e0ac8d6bb0b840247d18c6193a3b583dad9bdae7 172.19.0.5:8004
   replicates 2c95d2d6cfd54060c86c3c9f3170aa6913ed3340
S: bc5f5042181420572d686f5bae89fe6f5d14e4e3 172.19.0.3:8005
   replicates 74363f850970e1136c17a8bcb6a51fde9685d9c0
S: 0488147a4da79a1e865981a2e939514622d6442f 172.19.0.7:8006
   replicates 70e19b8347b917e23d44c43d451a7c47479c97e3
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join...
>>> Performing Cluster Check (using node 172.19.0.4:8001)
M: 2c95d2d6cfd54060c86c3c9f3170aa6913ed3340 172.19.0.4:8001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 74363f850970e1136c17a8bcb6a51fde9685d9c0 172.19.0.2:8002@18002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: bc5f5042181420572d686f5bae89fe6f5d14e4e3 172.19.0.3:8005@18005
   slots: (0 slots) slave
   replicates 74363f850970e1136c17a8bcb6a51fde9685d9c0
S: 0488147a4da79a1e865981a2e939514622d6442f 172.19.0.7:8006@18006
   slots: (0 slots) slave
   replicates 70e19b8347b917e23d44c43d451a7c47479c97e3
M: 70e19b8347b917e23d44c43d451a7c47479c97e3 172.19.0.6:8003@18003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: e0ac8d6bb0b840247d18c6193a3b583dad9bdae7 172.19.0.5:8004@18004
   slots: (0 slots) slave
   replicates 2c95d2d6cfd54060c86c3c9f3170aa6913ed3340
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

注:记得中间输入yes

10.进入容器,验证集群可用性:

#连接redis
redis-cli -c -p 8000 -a a123456
#打印节点信息
cluster info

若是成功,日志如下:

127.0.0.1:8000> 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:252
cluster_stats_messages_pong_sent:250
cluster_stats_messages_sent:502
cluster_stats_messages_ping_received:245
cluster_stats_messages_pong_received:252
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:502

你可能感兴趣的:(Docker初见(四)Docker 安装Redis集群)