环境准备

为了让环境更加真实,本文使用多机环境

IP 操作系统 内存大小 数据淘汰策略 内存淘汰策略
192.168.168.143 CentOS Linux release 7.9.2009 (Core) 最大内存*3/4
192.168.168.144 CentOS Linux release 7.9.2009 (Core)
192.168.168.145 CentOS Linux release 7.9.2009 (Core)
192.168.168.146 CentOS Linux release 7.9.2009 (Core)
192.168.168.147 CentOS Linux release 7.9.2009 (Core)
192.168.168.148 CentOS Linux release 7.9.2009 (Core)

创建Redis容器

整体搭建步骤主要分为以下几步:

  • 下载 Redis 镜像(可以省略,创建容器时如果本地镜像不存在就会去远程拉取)
  • 编写 Redis 配置文件
  • 创建 Redis 容器
  • 创建 Redis Cluster 集群

    Step1:编写Redis配置文件

    获取 redis.conf 修改默认配置

  • bind 127.0.0.1 # 修改为 0.0.0.0 或指定IP,127.0.0.1限制Redis 只能本地访问

  • protected-mode no # 默认 yes ,开启保护模式限制为本地访问
  • daemonize no # 默认 no ,改为 yes 意为以守护进程方式启动可后台运行,改为 yes 会使配置文件方式启动redis失败
  • dir ./ # 输入本地 Redis 数据库存放文件夹(可选)
  • appendonly yes # Redis 持久化(可选)
  • cluster-enabled # 设置为集群节点

    Step2:创建并启动Redis容器

    1. docker run -d --net host --name redis -v /usr/confs/redis.conf:/etc/redis/redis.conf -v /usr/redisdata:/data redis redis-server /etc/redis/redis.conf --appendonly yes

    命令解释:

  • --name redis 指定容器名称

  • -v 挂载目录,前表示主机部分/后表示容器部分
  • -d 表示后台启动 Redis
  • redis-server /etc/redis/redis.conf 以配置文件启动 Redis ,加载容器内的 conf 文件,最终找到的是 /usr/confs/redis.conf
  • appendonly yes 开启 Redis 持久化
  • --requirepass “如果有密码”

在每台虚机上重复执行 Step1Step2

创建Redis集群

  1. [root@centos-01 ~]# docker exec -it redis redis-cli --cluster create 192.168.168.143:6379 192.168.168.144:6379 192.168.168.145:6379 192.168.168.146:6379 192.168.168.147:6379 192.168.168.148:6379 --cluster-replicas 1
  2. >>> Performing hash slots allocation on 6 nodes...
  3. Master[0] -> Slots 0 - 5460
  4. Master[1] -> Slots 5461 - 10922
  5. Master[2] -> Slots 10923 - 16383
  6. Adding replica 192.168.168.147:6379 to 192.168.168.143:6379
  7. Adding replica 192.168.168.148:6379 to 192.168.168.144:6379
  8. Adding replica 192.168.168.146:6379 to 192.168.168.145:6379
  9. M: 4c10ce08fcf23e58c1397a3b515232bae6e8ce0f 192.168.168.143:6379
  10. slots:[0-5460] (5461 slots) master
  11. M: fe8889c921fb602fa080a0604cb127d610d160d7 192.168.168.144:6379
  12. slots:[5461-10922] (5462 slots) master
  13. M: 00067aa1ac68cb803cf903223f663a8b5832fd51 192.168.168.145:6379
  14. slots:[10923-16383] (5461 slots) master
  15. S: 8438ede3355fda20d2e944cc64d7a70619425e38 192.168.168.146:6379
  16. replicates 00067aa1ac68cb803cf903223f663a8b5832fd51
  17. S: 1ad0517208ac2eb1b9aeceb48ed942d0f36e174c 192.168.168.147:6379
  18. replicates 4c10ce08fcf23e58c1397a3b515232bae6e8ce0f
  19. S: 5d2ba31f6b0445037e2819939d1c262f535bd3ff 192.168.168.148:6379
  20. replicates fe8889c921fb602fa080a0604cb127d610d160d7
  21. Can I set the above configuration? (type 'yes' to accept): yes
  22. >>> Nodes configuration updated
  23. >>> Assign a different config epoch to each node
  24. >>> Sending CLUSTER MEET messages to join the cluster
  25. Waiting for the cluster to join
  26. >>> Performing Cluster Check (using node 192.168.168.143:6379)
  27. M: 4c10ce08fcf23e58c1397a3b515232bae6e8ce0f 192.168.168.143:6379
  28. slots:[0-5460] (5461 slots) master
  29. 1 additional replica(s)
  30. M: fe8889c921fb602fa080a0604cb127d610d160d7 192.168.168.144:6379
  31. slots:[5461-10922] (5462 slots) master
  32. 1 additional replica(s)
  33. S: 8438ede3355fda20d2e944cc64d7a70619425e38 192.168.168.146:6379
  34. slots: (0 slots) slave
  35. replicates 00067aa1ac68cb803cf903223f663a8b5832fd51
  36. S: 5d2ba31f6b0445037e2819939d1c262f535bd3ff 192.168.168.148:6379
  37. slots: (0 slots) slave
  38. replicates fe8889c921fb602fa080a0604cb127d610d160d7
  39. M: 00067aa1ac68cb803cf903223f663a8b5832fd51 192.168.168.145:6379
  40. slots:[10923-16383] (5461 slots) master
  41. 1 additional replica(s)
  42. S: 1ad0517208ac2eb1b9aeceb48ed942d0f36e174c 192.168.168.147:6379
  43. slots: (0 slots) slave
  44. replicates 4c10ce08fcf23e58c1397a3b515232bae6e8ce0f
  45. [OK] All nodes agree about slots configuration.
  46. >>> Check for open slots...
  47. >>> Check slots coverage...
  48. [OK] All 16384 slots covered.

查看结果

  1. [root@centos-01 ~]# docker exec -it redis /bin/bash
  2. root@centos-01:/data# redis-cli
  3. 127.0.0.1:6379> CLUSTER nodes
  4. 00067aa1ac68cb803cf903223f663a8b5832fd51 192.168.168.145:6379@16379 master - 0 1625646473525 3 connected 10923-16383
  5. 4c10ce08fcf23e58c1397a3b515232bae6e8ce0f 192.168.168.143:6379@16379 master - 0 1625646471506 1 connected 0-5460
  6. 8438ede3355fda20d2e944cc64d7a70619425e38 192.168.168.146:6379@16379 slave 00067aa1ac68cb803cf903223f663a8b5832fd51 0 1625646471000 3 connected
  7. 5d2ba31f6b0445037e2819939d1c262f535bd3ff 192.168.168.148:6379@16379 slave fe8889c921fb602fa080a0604cb127d610d160d7 0 1625646472517 2 connected
  8. fe8889c921fb602fa080a0604cb127d610d160d7 192.168.168.144:6379@16379 myself,master - 0 1625646470000 2 connected 5461-10922
  9. 1ad0517208ac2eb1b9aeceb48ed942d0f36e174c 192.168.168.147:6379@16379 slave 4c10ce08fcf23e58c1397a3b515232bae6e8ce0f 0 1625646472000 1 connected
  10. 127.0.0.1:6379> CLUSTER info
  11. cluster_state:ok
  12. cluster_slots_assigned:16384
  13. cluster_slots_ok:16384
  14. cluster_slots_pfail:0
  15. cluster_slots_fail:0
  16. cluster_known_nodes:6
  17. cluster_size:3
  18. cluster_current_epoch:6
  19. cluster_my_epoch:2
  20. cluster_stats_messages_ping_sent:166
  21. cluster_stats_messages_pong_sent:189
  22. cluster_stats_messages_meet_sent:1