docker搭建Redis集群

虚拟机的ip:192.168.182.128

当前docker版本:Docker version 23.0.1, build a5ee5b1

创建 Redis 容器

编写redis配置文件的模板

# 创建目录
mkdir -p /usr/local/docker-redis/redis-cluster
# 切换至指定目录
cd /usr/local/docker-redis/redis-cluster/
# 编写 redis-cluster.tmpl 文件
vi redis-cluster.tmpl
port ${PORT}
requirepass 1234
masterauth 1234
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 192.168.182.128
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}

说明:

  • port:节点端口;
  • requirepass:添加访问认证密码;
  • masterauth:如果主节点开启了访问认证,从节点访问主节点需要认证;
  • protected-mode:保护模式,默认值 yes,即开启。开启保护模式以后,需配置 bind ip 或者设置访问密码;关闭保护模式,外部网络可以直接访问;
  • daemonize:是否以守护线程的方式启动(后台启动),默认 no;
  • appendonly:是否开启 AOF 持久化模式,默认 no;
  • cluster-enabled:是否开启集群模式,默认 no;
  • cluster-config-file:集群节点信息文件;
  • cluster-node-timeout:集群节点连接超时时间;
  • cluster-announce-ip:集群节点 IP,填写宿主机的 IP;
  • cluster-announce-port:集群节点映射端口;
  • cluster-announce-bus-port:集群节点总线端口。

官方文档: https://redis.io/docs/management/scaling/

Redis群集TCP端口。
每个Redis集群节点需要两个开放的TCP连接:一个用于服务客户端的Redis TCP端口,例如6379,以及称为集群总线端口的第二个端口。默认情况下,通过将10000添加到数据端口(例如16379)来设置集群总线端口;但是,您可以在集群端口配置中覆盖此设置。

然后在此目录下执行

for port in `seq 6371 6376`; do \
  mkdir -p ${port}/conf \
  && PORT=${port} envsubst < redis-cluster.tmpl > ${port}/conf/redis.conf \
  && mkdir -p ${port}/data;\
done

查看文件创建结果

[root@localhost redis-cluster]# ls
6371  6372  6373  6374  6375  6376  redis-cluster.tmpl

创建6个容器

for port in $(seq 6371 6376); do \
  docker run -di --restart always --name redis-${port} --net host \
  -v /usr/local/docker-redis/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
  -v /usr/local/docker-redis/redis-cluster/${port}/data:/data \
  redis redis-server /usr/local/etc/redis/redis.conf; \
done

查看容器创建结果

[root@localhost redis-cluster]# docker ps |grep redis
f7919c1efbb3   redis                 "docker-entrypoint.s…"   48 minutes ago   Up 48 minutes                                                                                                    redis-6376
b258fea8e054   redis                 "docker-entrypoint.s…"   48 minutes ago   Up 48 minutes                                                                                                    redis-6375
1a0019bc4885   redis                 "docker-entrypoint.s…"   48 minutes ago   Up 48 minutes                                                                                                    redis-6374
24332985bc56   redis                 "docker-entrypoint.s…"   48 minutes ago   Up 48 minutes                                                                                                    redis-6373
52e3c1459f2b   redis                 "docker-entrypoint.s…"   48 minutes ago   Up 48 minutes                                                                                                    redis-6372
5a2c90978230   redis                 "docker-entrypoint.s…"   48 minutes ago   Up 48 minutes                                                                                                    redis-6371

创建 Redis Cluster 集群

随便进入一个容器节点,并进入 /usr/local/bin/ 目录:

# 进入容器
docker exec -it redis-6371 bash
# 切换至redis所在目录
cd /usr/local/bin/

执行

redis-cli -a 1234 --cluster create 192.168.182.128:6371 192.168.182.128:6372 192.168.182.128:6373 192.168.182.128:6374 192.168.182.128:6375 192.168.182.128:6376 --cluster-replicas 1

出现选择提示信息,输入 yes

输出结果如下

root@localhost:/usr/local/bin# redis-cli -a 1234 --cluster create 192.168.182.128:6371 192.168.182.128:6372 192.168.182.128:6373 192.168.182.128:6374 192.168.182.128:6375 192.168.182.128:6376 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.182.128:6375 to 192.168.182.128:6371
Adding replica 192.168.182.128:6376 to 192.168.182.128:6372
Adding replica 192.168.182.128:6374 to 192.168.182.128:6373
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 09a7815c91b56713d9a368998c285aa8401aa8d1 192.168.182.128:6371
   slots:[0-5460] (5461 slots) master
M: 6005ee4daa5d4ad2d7082bcabb16cd61fbe85c42 192.168.182.128:6372
   slots:[5461-10922] (5462 slots) master
M: 9c5c3f3dcf891a698db52ac10cfb20999baebff2 192.168.182.128:6373
   slots:[10923-16383] (5461 slots) master
S: c6d7a610dc333571f92dedd052afa76fe74b06e5 192.168.182.128:6374
   replicates 9c5c3f3dcf891a698db52ac10cfb20999baebff2
S: 1eed961cd0c7f4808c58703d6bea035cc54af07b 192.168.182.128:6375
   replicates 09a7815c91b56713d9a368998c285aa8401aa8d1
S: 9f8335d71af2026e67e2d6674049d8491bee0c7d 192.168.182.128:6376
   replicates 6005ee4daa5d4ad2d7082bcabb16cd61fbe85c42
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 192.168.182.128:6371)
M: 09a7815c91b56713d9a368998c285aa8401aa8d1 192.168.182.128:6371
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 1eed961cd0c7f4808c58703d6bea035cc54af07b 192.168.182.128:6375
   slots: (0 slots) slave
   replicates 09a7815c91b56713d9a368998c285aa8401aa8d1
M: 9c5c3f3dcf891a698db52ac10cfb20999baebff2 192.168.182.128:6373
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: c6d7a610dc333571f92dedd052afa76fe74b06e5 192.168.182.128:6374
   slots: (0 slots) slave
   replicates 9c5c3f3dcf891a698db52ac10cfb20999baebff2
S: 9f8335d71af2026e67e2d6674049d8491bee0c7d 192.168.182.128:6376
   slots: (0 slots) slave
   replicates 6005ee4daa5d4ad2d7082bcabb16cd61fbe85c42
M: 6005ee4daa5d4ad2d7082bcabb16cd61fbe85c42 192.168.182.128:6372
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

看得到前三台是master,后三台是slave

M: 09a7815c91b56713d9a368998c285aa8401aa8d1 192.168.182.128:6371
   slots:[0-5460] (5461 slots) master
M: 6005ee4daa5d4ad2d7082bcabb16cd61fbe85c42 192.168.182.128:6372
   slots:[5461-10922] (5462 slots) master
M: 9c5c3f3dcf891a698db52ac10cfb20999baebff2 192.168.182.128:6373
   slots:[10923-16383] (5461 slots) master
S: c6d7a610dc333571f92dedd052afa76fe74b06e5 192.168.182.128:6374
   replicates 9c5c3f3dcf891a698db52ac10cfb20999baebff2
S: 1eed961cd0c7f4808c58703d6bea035cc54af07b 192.168.182.128:6375
   replicates 09a7815c91b56713d9a368998c285aa8401aa8d1
S: 9f8335d71af2026e67e2d6674049d8491bee0c7d 192.168.182.128:6376

查看集群状态

我们先进入容器,然后通过一些集群常用的命令查看一下集群的状态。

# 进入容器
docker exec -it redis-6371 bash
# 切换至指定目录
cd /usr/local/bin/

链接一个redis

root@localhost:/usr/local/bin# redis-cli -c -a 1234 -h 192.168.182.128 -p 6371

查看集群状态

192.168.182.128:6371> 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:3819
cluster_stats_messages_pong_sent:3897
cluster_stats_messages_sent:7716
cluster_stats_messages_ping_received:3892
cluster_stats_messages_pong_received:3819
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:7716
total_cluster_links_buffer_limit_exceeded:0


查看节点信息

192.168.182.128:6371> cluster nodes
1eed961cd0c7f4808c58703d6bea035cc54af07b 192.168.182.128:6375@16375 slave 09a7815c91b56713d9a368998c285aa8401aa8d1 0 1679626693000 1 connected
9c5c3f3dcf891a698db52ac10cfb20999baebff2 192.168.182.128:6373@16373 master - 0 1679626694000 3 connected 10923-16383
c6d7a610dc333571f92dedd052afa76fe74b06e5 192.168.182.128:6374@16374 slave 9c5c3f3dcf891a698db52ac10cfb20999baebff2 0 1679626691000 3 connected
9f8335d71af2026e67e2d6674049d8491bee0c7d 192.168.182.128:6376@16376 slave 6005ee4daa5d4ad2d7082bcabb16cd61fbe85c42 0 1679626692185 2 connected
09a7815c91b56713d9a368998c285aa8401aa8d1 192.168.182.128:6371@16371 myself,master - 0 1679626689000 1 connected 0-5460
6005ee4daa5d4ad2d7082bcabb16cd61fbe85c42 192.168.182.128:6372@16372 master - 0 1679626694201 2 connected 5461-10922

使用

192.168.182.128:6371> set name Jack
-> Redirected to slot [5798] located at 192.168.182.128:6372
OK
192.168.182.128:6372> get name
"Jack"
192.168.182.128:6372> set num 999
-> Redirected to slot [2765] located at 192.168.182.128:6371
OK
192.168.182.128:6371> set word Hello
-> Redirected to slot [9755] located at 192.168.182.128:6372
OK
192.168.182.128:6372> set age 18
-> Redirected to slot [741] located at 192.168.182.128:6371
OK

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