环境

  1. 系统 麒麟Kylin (V10 SP3)
  2. Redis 7.2.4
  3. 集群采用host模式

前期准备

  1. 调整系统参数
less -N /etc//sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
kernel.sysrq=0
net.ipv4.ip_forward=0
net.ipv4.conf.all.send_redirects=0
net.ipv4.conf.default.send_redirects=0
net.ipv4.conf.all.accept_source_route=0
net.ipv4.conf.default.accept_source_route=0
net.ipv4.conf.all.accept_redirects=0
net.ipv4.conf.default.accept_redirects=0
net.ipv4.conf.all.secure_redirects=0
net.ipv4.conf.default.secure_redirects=0
net.ipv4.icmp_echo_ignore_broadcasts=1
net.ipv4.icmp_ignore_bogus_error_responses=1
net.ipv4.conf.all.rp_filter=1
net.ipv4.conf.default.rp_filter=1
net.ipv4.tcp_syncookies=1
kernel.dmesg_restrict=1
net.ipv6.conf.all.accept_redirects=0
net.ipv6.conf.default.accept_redirects=0
###新增以下配置
vm.overcommit_memory = 1
net.core.somaxconn=65535

#配置生效
sysctl -p
  1. 创建配置文件
 注:
 如果节点配置文件设置了密码:requirepass xxxxx
 那么节点配置文件需要添加 masterauth xxxxx  参数
cat create_conf.sh
#!/bin/bash

for port in $(seq 6379 6384);
do
mkdir -p common/node-${port}/conf
touch common/node-${port}/conf/redis.conf
cat  << EOF > common/node-${port}/conf/redis.conf
port ${port}
requirepass xxxxx (redis 密码,可以不设)
masterauth xxxxx  
bind 0.0.0.0
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip   127.0.0.1
cluster-announce-port ${port}
cluster-announce-bus-port 1${port}
EOF
done
  1. 创建数据目录
mkdir data/node-{6379..6384}
查看目录
[root@Kylin redis]# ls
common  create_conf.sh  data  docker-compose.yml  
查看数据目录 
[root@Kylin redis]# tree data
data
├── node-6379
├── node-6380
├── node-6381
├── node-6382
├── node-6383
└── node-6384

服务启动

  1. 查看docker-cmopose文件
version: "3"

services:
 redis-1:
   image: redis:7.2.4
   container_name: redis-1
   restart: on-failure:3
   privileged: true
   environment:
     - TZ=Asia/Shanghai
   volumes:
     - ./common/node-6379/conf/redis.conf:/usr/local/etc/redis/redis.conf
     - ./data/node-6379:/data
   command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
   network_mode: host


 redis-2:
   image: redis:7.2.4
   container_name: redis-2
   restart: on-failure:3
   privileged: true
   environment:
     - TZ=Asia/Shanghai
   volumes:
     - ./common/node-6380/conf/redis.conf:/usr/local/etc/redis/redis.conf
     - ./data/node-6380:/data
   command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
   network_mode: host

 redis-3:
   image: redis:7.2.4
   container_name: redis-3
   restart: on-failure:3
   privileged: true
   environment:
     - TZ=Asia/Shanghai
   volumes:
     - ./common/node-6381/conf/redis.conf:/usr/local/etc/redis/redis.conf
     - ./data/node-6381:/data
   command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
   network_mode: host
 redis-4:
   image: redis:7.2.4
   container_name: redis-4
   restart: on-failure:3
   privileged: true
   environment:
     - TZ=Asia/Shanghai
   volumes:
     - ./common/node-6382/conf/redis.conf:/usr/local/etc/redis/redis.conf
     - ./data/node-6382:/data
   command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
   network_mode: host

 redis-5:
   image: redis:7.2.4
   container_name: redis-5
   restart: on-failure:3
   privileged: true
   environment:
     - TZ=Asia/Shanghai
   volumes:
     - ./common/node-6383/conf/redis.conf:/usr/local/etc/redis/redis.conf
     - ./data/node-6383:/data
   command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
   network_mode: host
 redis-6:
   image: redis:7.2.4
   container_name: redis-6
   restart: on-failure:3
   privileged: true
   environment:
     - TZ=Asia/Shanghai
   volumes:
     - ./common/node-6384/conf/redis.conf:/usr/local/etc/redis/redis.conf
     - ./data/node-6384:/data
   command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
   network_mode: host
  1. 服务查看
[root@Kylin redis]# docker-compose up -d
[+] Running 12/12
 ✔ Container redis-1                                                 Started   2.6s
 ✔ Container redis-3                                                 Started   2.6s
 ✔ Container redis-4                                                 Started   2.4s
 ✔ Container redis-5                                                 Started   2.6s
 ✔ Container redis-2                                                 Started   2.4s
 ✔ Container redis-6                                                 Started   1.8s

[root@Kylin redis]# docker-compose ps -a
NAME      IMAGE         COMMAND                   SERVICE   CREATED             STATUS             PORTS
redis-1   redis:7.2.4   "docker-entrypoint.s…"   redis1    About an hour ago   Up About an hour
redis-2   redis:7.2.4   "docker-entrypoint.s…"   redis2    About an hour ago   Up About an hour
redis-3   redis:7.2.4   "docker-entrypoint.s…"   redis3    About an hour ago   Up About an hour
redis-4   redis:7.2.4   "docker-entrypoint.s…"   redis4    About an hour ago   Up About an hour
redis-5   redis:7.2.4   "docker-entrypoint.s…"   redis5    About an hour ago   Up About an hour
redis-6   redis:7.2.4   "docker-entrypoint.s…"   redis6    About an hour ago   Up About an hour


[root@Kylin redis]# ss -ntl
State               Recv-Q              Send-Q                           Local Address:Port                            Peer Address:Port              Process
LISTEN              0                   511                                    0.0.0.0:16379                                0.0.0.0:*
LISTEN              0                   511                                    0.0.0.0:16380                                0.0.0.0:*
LISTEN              0                   511                                    0.0.0.0:16381                                0.0.0.0:*
LISTEN              0                   511                                    0.0.0.0:16382                                0.0.0.0:*
LISTEN              0                   511                                    0.0.0.0:16383                                0.0.0.0:*
LISTEN              0                   511                                    0.0.0.0:16384                                0.0.0.0:*
LISTEN              0                   511                                    0.0.0.0:6379                                 0.0.0.0:*
LISTEN              0                   511                                    0.0.0.0:6380                                 0.0.0.0:*
LISTEN              0                   511                                    0.0.0.0:6381                                 0.0.0.0:*
LISTEN              0                   511                                    0.0.0.0:6382                                 0.0.0.0:*
LISTEN              0                   511                                    0.0.0.0:6383                                 0.0.0.0:*
LISTEN              0                   511                                    0.0.0.0:6384                                 0.0.0.0:*

配置集群

#随机登录一个redis节点
docker exec -it redis-1 sh
redis-cli  -a "Redis密码" --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster-replicas 1
提示输入yes即可,等待出现以下内容,这表示成功
[0K] All nodes agree about slots configuration.
Check for open slots...
>>> Check slots coverage...
[0K] All 16384 slots covered.

测试

#再次登录redis-1 容器
[root@10 mysql_redis]# docker exec -it redis-1 redis-cli  -h redis-1 -p 6379  -a "Redis密码"
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> cluster nodes
e8a36e967367888416c4bb168fa96d98f7191093 127.0.0.1:6383@16383 slave 2d0079117e814b969b2906c82212185817623b93 0 1756955113000 3 connected
2d0079117e814b969b2906c82212185817623b93 127.0.0.1:6381@16381 master - 0 1756955113000 3 connected 10923-16383
93589efa5bb09c1422ae01aaf47601fac0a0b3d3 127.0.0.1:6384@16384 slave 13c5fb22fdc26f338707c677a76be1178daaad99 0 1756955113779 1 connected
843c91cdaac9bd5670e3ce7dca71f2487fcc1d28 127.0.0.1:6382@16382 slave e808b2709fe6f728208c88bca42baa89820ab419 0 1756955112575 2 connected
13c5fb22fdc26f338707c677a76be1178daaad99 127.0.0.1:6379@16379 myself,master - 0 1756955112000 1 connected 0-5460
e808b2709fe6f728208c88bca42baa89820ab419 127.0.0.1:6380@16380 master - 0 1756955114081 2 connected 5461-10922
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:4917
cluster_stats_messages_pong_sent:4874
cluster_stats_messages_sent:9791
cluster_stats_messages_ping_received:4874
cluster_stats_messages_pong_received:4917
cluster_stats_messages_publish_received:1
cluster_stats_messages_received:9792
total_cluster_links_buffer_limit_exceeded:0
#从其他服务器登录链接redis写入数据进行验证
登录10.0.11.20 
ssh root@10.0.11.20
docker exec -it redis sh  (登录11.20的redis服务)
redis-cli -c -a "Redis密码" -h 10.0.xx.xx -p 6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.xx.xx:6379> set test 'hello world'
-> Redirected to slot [6918] located at 10.0.xx.xx:6380
OK
10.0.xx.xx:6380> get test
"hello world"


##通slave 容器日志验证结果
docker logs -f redis-4

1:S 23 Feb 2024 03:28:23.106 * Connecting to MASTER 10.0.xx.xx:6381
1:S 23 Feb 2024 03:28:23.106 * MASTER <-> REPLICA sync started
1:S 23 Feb 2024 03:28:23.106 * Cluster state changed: ok
1:S 23 Feb 2024 03:28:23.106 * Non blocking connect for SYNC fired the event.
1:S 23 Feb 2024 03:28:23.211 * Master replied to PING, replication can continue...
1:S 23 Feb 2024 03:28:23.279 * Trying a partial resynchronization (request 135dc76f87b7b3d07f78542e878deb65f1a733ad:1).
1:S 23 Feb 2024 03:28:28.681 * Full resync from master: 841dce9a89d9c248965abf04bd1a93d8ce1c8bf3:0
1:S 23 Feb 2024 03:28:28.684 * MASTER <-> REPLICA sync: receiving streamed RDB from master with EOF to disk
1:S 23 Feb 2024 03:28:28.684 * Discarding previously cached master state.
1:S 23 Feb 2024 03:28:28.684 * MASTER <-> REPLICA sync: Flushing old data
1:S 23 Feb 2024 03:28:28.685 * MASTER <-> REPLICA sync: Loading DB in memory
1:S 23 Feb 2024 03:28:28.723 * Loading RDB produced by version 7.2.4
1:S 23 Feb 2024 03:28:28.723 * RDB age 0 seconds
1:S 23 Feb 2024 03:28:28.723 * RDB memory usage when created 1.61 Mb
1:S 23 Feb 2024 03:28:28.723 * Done loading RDB, keys loaded: 0, keys expired: 0.
1:S 23 Feb 2024 03:28:28.723 * MASTER <-> REPLICA sync: Finished with success
1:S 23 Feb 2024 03:28:28.723 * Creating AOF incr file temp-appendonly.aof.incr on background rewrite
1:S 23 Feb 2024 03:28:28.724 * Background append only file rewriting started by pid 21
21:C 23 Feb 2024 03:28:28.747 * Successfully created the temporary AOF base file temp-rewriteaof-bg-21.aof
21:C 23 Feb 2024 03:28:28.748 * Fork CoW for AOF rewrite: current 4 MB, peak 4 MB, average 4 MB
1:S 23 Feb 2024 03:28:28.830 * Background AOF rewrite terminated with success
1:S 23 Feb 2024 03:28:28.830 * Successfully renamed the temporary AOF base file temp-rewriteaof-bg-21.aof into appendonly.aof.3.base.rdb
1:S 23 Feb 2024 03:28:28.830 * Successfully renamed the temporary AOF incr file temp-appendonly.aof.incr into appendonly.aof.3.incr.aof
1:S 23 Feb 2024 03:28:28.878 * Removing the history file appendonly.aof.2.incr.aof in the background
1:S 23 Feb 2024 03:28:28.878 * Removing the history file appendonly.aof.2.base.rdb in the background
1:S 23 Feb 2024 03:28:28.921 * Background AOF rewrite finished successfully

官方集群搭建教程

创建redis集群
集群搭建脚本

更多推荐