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=0net.ipv4.ip_forward=0net.ipv4.conf.all.send_redirects=0net.ipv4.conf.default.send_redirects=0net.ipv4.conf.all.accept_source_route=0net.ipv4.conf.default.accept_source_route=0net.ipv4.conf.all.accept_redirects=0net.ipv4.conf.default.accept_redirects=0net.ipv4.conf.all.secure_redirects=0net.ipv4.conf.default.secure_redirects=0net.ipv4.icmp_echo_ignore_broadcasts=1net.ipv4.icmp_ignore_bogus_error_responses=1net.ipv4.conf.all.rp_filter=1net.ipv4.conf.default.rp_filter=1net.ipv4.tcp_syncookies=1kernel.dmesg_restrict=1net.ipv6.conf.all.accept_redirects=0net.ipv6.conf.default.accept_redirects=0###新增以下配置
vm.overcommit_memory =1net.core.somaxconn=65535#配置生效sysctl-p
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
[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
! redis2 Published ports are discarded when using host network mode 0.0s
! redis4 Published ports are discarded when using host network mode 0.0s
! redis1 Published ports are discarded when using host network mode 0.0s
! redis3 Published ports are discarded when using host network mode 0.0s
! redis5 Published ports are discarded when using host network mode 0.0s
! redis6 Published ports are discarded when using host network mode 0.0s
[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 05110.0.0.0:16379 0.0.0.0:*
LISTEN 05110.0.0.0:16380 0.0.0.0:*
LISTEN 05110.0.0.0:16381 0.0.0.0:*
LISTEN 05110.0.0.0:16382 0.0.0.0:*
LISTEN 05110.0.0.0:16383 0.0.0.0:*
LISTEN 05110.0.0.0:16384 0.0.0.0:*
LISTEN 05110.0.0.0:6379 0.0.0.0:*
LISTEN 05110.0.0.0:6380 0.0.0.0:*
LISTEN 05110.0.0.0:6381 0.0.0.0:*
LISTEN 05110.0.0.0:6382 0.0.0.0:*
LISTEN 05110.0.0.0:6383 0.0.0.0:*
LISTEN 05110.0.0.0:6384 0.0.0.0:*
配置集群
#随机登录一个redis节点dockerexec-it redis-1 sh
redis-cli -a"Redis密码"--cluster create redis-1:6379 redis-2:6380 redis-3:6381 redis-4:6382 redis-5:6383 redis-6:6384 --cluster-replicas 1
提示输入yes即可,等待出现以下内容,这表示成功
[0K] All nodes agree about slots configuration.
Check foropen 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.
redis1:6379> cluster nodes
1524644f7d48338ed71d1ab5f52fc0eb4c9be6f0 redis-3:6381@16381 master - 017561057565943 connected 10923-16383
ad63c71a588e0b995cf7d3553c79a06c9de0e11f redis-2:6380@16380 master - 017561057571962 connected 5461-10922
f037c2daffe44535d695556586d396b3b8937164 redis-1:6379@16379 myself,master - 017561057550001 connected 0-5460
e35bd80bb7a0f812dffa348f249a602930730eea redis-6:6384@16384 slave ad63c71a588e0b995cf7d3553c79a06c9de0e11f 017561057570002 connected
f2c3c49e0fc9af41477235abc2cd4e923e823965 redis-4:6382@16382 slave 1524644f7d48338ed71d1ab5f52fc0eb4c9be6f0 017561057560003 connected
26f6e295c0d0de9972e72f06c08030fbfdf6bfa0 redis-5:6383@16383 slave f037c2daffe44535d695556586d396b3b8937164 017561057566941 connected
redis1: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:88
cluster_stats_messages_pong_sent:95
cluster_stats_messages_sent:183
cluster_stats_messages_ping_received:90
cluster_stats_messages_pong_received:88
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:183
total_cluster_links_buffer_limit_exceeded:0
#从其他服务器登录链接redis写入数据进行验证
登录10.0.11.20
ssh root@10.0.11.20
dockerexec-it redis sh (登录11.20的redis服务)
redis-cli -c-a"Redis密码"-h10.0.xx.xx -p6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.xx.xx:6379>settest'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 2121: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 historyfile appendonly.aof.2.incr.aof in the background
1:S 23 Feb 2024 03:28:28.878 * Removing the historyfile appendonly.aof.2.base.rdb in the background
1:S 23 Feb 2024 03:28:28.921 * Background AOF rewrite finished successfully