用docker-compose 创建redis cluster集群

马野
2023-12-01

redis集群官方教程:https://redis.io/topics/cluster-tutorial

参考文章:
https://blog.csdn.net/java_Murcielago/article/details/102776182
https://blog.csdn.net/qq_39508627/article/details/95967240

docker-compose环境已配好。

1.创建redis配置文件

创建7001,7002,7003,7004,7005,7006六个文件夹,文件夹内放配置文件redis.conf,然后依次修改
官方redis.conf下载地址:http://download.redis.io/redis-stable/redis.conf

# bind 127.0.0.1 //加上注释#
protected-mode no //关闭保护模式
port 7001  //绑定自定义端口
#daemonize yes //禁止redis后台运行
pidfile /var/run/redis_7001.pid 
cluster-enabled yes //开启集群 把注释#去掉
cluster-config-file nodes_6061.conf //集群的配置 配置文件首次启动自动生成 

2.编写docker-compose.yml文件

在官方教程中,每个节点都需要打开两个tcp端口,第一个端口是用于服务客户端的常规Redis TCP端口,如6379。在第一个端口上加上10000所得的16379为第二个端口,这个端口用于集群总线,即使用二进制协议的节点到节点通信通道。节点将群集总线用于故障检测,配置更新,故障转移授权等。

version: '3.4'
services:
  node1:
    image: redis
    container_name: redis1
    restart: always
    ports:
      - 7001:7001
      - 17001:17001
    volumes:
      - /Users/didi/docker-cluster/7001/redis.conf:/etc/redis/redis.conf
    command:
      redis-server /etc/redis/redis.conf

  node2:
    image: redis
    container_name: redis2
    restart: always
    ports:
      - 7002:7002
      - 17002:17002
    volumes:
      - /Users/didi/docker-cluster/7002/redis.conf:/etc/redis/redis.conf
    command:
      redis-server /etc/redis/redis.conf

  node3:
    image: redis
    container_name: redis3
    restart: always
    ports:
      - 7003:7003
      - 17003:17003
    volumes:
      - /Users/didi/docker-cluster/7003/redis.conf:/etc/redis/redis.conf
    command:
      redis-server /etc/redis/redis.conf

  node4:
    image: redis
    container_name: redis4
    restart: always
    ports:
      - 7004:7004
      - 17004:17004
    volumes:
      - /Users/didi/docker-cluster/7004/redis.conf:/etc/redis/redis.conf
    command:
      redis-server /etc/redis/redis.conf

  node5:
    image: redis
    container_name: redis5
    restart: always
    ports:
      - 7005:7005
      - 17005:17005
    volumes:
      - /Users/didi/docker-cluster/7005/redis.conf:/etc/redis/redis.conf
    command:
      redis-server /etc/redis/redis.conf

  node6:
    image: redis
    container_name: redis6
    restart: always
    ports:
      - 7006:7006
      - 17006:17006
    volumes:
      - /Users/didi/docker-cluster/7006/redis.conf:/etc/redis/redis.conf
    command:
      redis-server /etc/redis/redis.conf

3.启动服务

可用 docker ps 命令查看6个redis容器是否启动

docker-compose up -d

4. 创建集群

–replicas 1表示每个主机都有一个从机

docker run --rm -it inem0o/redis-trib create --replicas 1 192.168.2.209:7001 192.168.2.209:7002 192.168.2.209:7003 192.168.2.209:7004 192.168.2.209:7005 192.168.2.209:7006

执行后:

M: e7d9d0a119caa7c425c79ca38aae3e8fb0adadb6 192.168.2.209:7001
   slots:0-5460 (5461 slots) master
M: d9fd95974defcda9d41d5d9bc8d3ab8c6b8ab2e2 192.168.2.209:7002
   slots:5461-10922 (5462 slots) master
M: d8c454d264fe5f39d18884bb645474785c9a75c1 192.168.2.209:7003
   slots:10923-16383 (5461 slots) master
S: c7e0667dd23fe83688cc609c8b3bed7ca4c94b28 192.168.2.209:7004
   replicates e7d9d0a119caa7c425c79ca38aae3e8fb0adadb6
S: b3ea664a18212c63ad137b08131c598c582a78fc 192.168.2.209:7005
   replicates d9fd95974defcda9d41d5d9bc8d3ab8c6b8ab2e2
S: a6a9b0d098a959d7c85c74c5f516dff029e32203 192.168.2.209:7006
   replicates d8c454d264fe5f39d18884bb645474785c9a75c1
Can I set the above configuration? (type 'yes' to accept): yes 

M表示主机,S表示从机,输入yes表示接受配置,继续…

如果卡在下面的情况说明ip或端口号不正确:

>>> 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.......................................

纠正错误后继续执行,出现下面的结果后说明配置成功。

[OK] All 16384 slots covered.

5. 测试

1)使用 docker exec -it redis1 bash 命令进入容器内
2)使用redis-cli -c -h 192.168.2.209 -p 7001连接集群内的任意一个服务端, -c表示集群模式
3)cluster nodes 和 cluster info查看集群内的节点信息
4)key a b c 分别被分配到不同的节点内

didideAir:compose didi$ docker exec -it redis1 bash
root@f1a34ae1ee36:/data# redis-cli -c -h 192.168.2.209 -p 7001
192.168.2.209:7001> cluster nodes
a2ded8ed4af17d711c50e8119df994049b122b72 172.18.0.3:7001@17001 myself,master - 0 1594822563000 1 connected 0-5460
77df3a6c3ef50fec3734bd59a8def5555a608519 172.18.0.1:7006@17006 slave a5bd94552585fd0e350edd540cb70937e947f65b 0 1594822565663 6 connected
a40202ce777c970847b531ac906a03110d4c8f76 172.18.0.1:7002@17002 master - 0 1594822562000 2 connected 5461-10922
be7f35668f969ee25d83f82546e4c59a86feb69f 172.18.0.1:7005@17005 slave a40202ce777c970847b531ac906a03110d4c8f76 0 1594822564635 5 connected
a5bd94552585fd0e350edd540cb70937e947f65b 172.18.0.1:7003@17003 master - 0 1594822564000 3 connected 10923-16383
208628a6c82b592f538fbdf693478b255becda74 172.18.0.1:7004@17004 slave a2ded8ed4af17d711c50e8119df994049b122b72 0 1594822563609 4 connected
192.168.2.209:7001> set a aaa
-> Redirected to slot [15495] located at 172.18.0.1:7003
OK
172.18.0.1:7003> get a
"aaa"
172.18.0.1:7003> set b bbb
-> Redirected to slot [3300] located at 172.18.0.1:7001
OK
172.18.0.1:7001> set c ccc
-> Redirected to slot [7365] located at 172.18.0.1:7002
OK
172.18.0.1:7002> 
 类似资料: