Rocky linux9下部署redis数据库集群
发布时间:2026/9/25 18:53:52 锦皓数字建站

目录前言一、服务器规划二、系统优化所有节点执行1.关闭透明大页THP2.内存设置3. 设置文件描述符上限4. 内核网络与内存优化三、编译安装 Redis 最新版所有节点执行1.安装编译依赖2.下载最新版 Redis 源码3. 编译安装4.创建目录结构5. 创建 redis 用户并设置权限6. 配置环境变量7. 验证安装四、配置文件所有节点执行1.创建两个配置文件2.创建数据目录五、创建 systemd 服务所有节点执行1.创建服务配置文件2.加载服务项六、启动所有实例所有节点执行七、创建集群任意一个节点执行一次八、验证集群1.检查集群状态2. 查看节点列表3. 读写测试九、常用运维命令1.检查集群健康状态2.查看节点列表3.实时日志4.重启单个实例前言在 Rocky Linux 9 上部署 Redis 集群建议通过源码编译安装最新版Redis 8.x而不是使用dnf install redis。Rocky Linux 9 的 AppStream 仓库默认提供的是 Redis 6.2.7版本较旧且缺少多线程 I/O 等关键新特性。以下是基于3 台服务器、每台 2 个实例3 主 3 从的完整生产级部署方案适配 Rocky Linux 9。一、服务器规划服务器IP 地址实例1主实例2从Node1192.168.100.1296379 (Master)6380 (Slave)Node2192.168.100.1306379 (Master)6380 (Slave)Node3192.168.100.1316379 (Master)6380 (Slave)端口说明集群模式下每个实例需要两个端口——客户端端口6379/6380和集群总线端口16379/16380固定为客户端端口10000。二、系统优化所有节点执行1.关闭透明大页THPRedis fork 持久化时 THP 会导致内存翻倍和延迟抖动执行以下命令echo never /sys/kernel/mm/transparent_hugepage/enabled echo never /sys/kernel/mm/transparent_hugepage/defrag2.内存设置允许内存过量使用避免 fork 时失败执行以下命令sysctl -w vm.overcommit_memory13. 设置文件描述符上限执行如下内容cat /etc/security/limits.conf EOF * soft nofile 655350 * hard nofile 655350 * soft nproc 655350 * hard nproc 655350 EOF4. 内核网络与内存优化执行如下内容cat /etc/sysctl.conf EOF net.core.somaxconn 65535 net.ipv4.tcp_max_syn_backlog 65535 net.core.netdev_max_backlog 65535 vm.swappiness 1 net.ipv4.tcp_fin_timeout 30 net.ipv4.tcp_keepalive_time 300 EOF sysctl -p三、编译安装 Redis 最新版所有节点执行1.安装编译依赖[rootredis1 ~]# yum groupinstall -y Development Tools Last metadata expiration check: 0:52:33 ago on Sun 20 Sep 2026 02:24:44 PM CST. Dependencies resolved. Package Architecture Version Repository Size Upgrading: glibc x86_64 2.34-275.el9_8 baseos 2.0 M glibc-all-langpacks x86_64 2.34-275.el9_8 baseos 18 M glibc-common x86_64 2.34-275.el9_8 baseos 299 k glibc-gconv-extra x86_64 2.34-275.el9_8 baseos 1.5 M glibc-langpack-en x86_64 2.34-275.el9_8 baseos 557 k Installing group/module packages: asciidoc noarch 9.1.0-3.el9 appstream 238 k autoconf noarch 2.69-41.el9 appstream 665 k automake noarch 1.16.2-8.el9 appstream 662 k bison x86_64 3.7.4-5.el9 appstream 921 k byacc x86_64 2.0.20210109-4.el9 appstream 88 k diffstat x86_64 1.64-6.el9 appstream 43 k flex x86_64 2.6.4-9.el9 appstream 308 k gcc ..........2.下载最新版 Redis 源码cd /usr/local/src wget https://download.redis.io/releases/redis-8.0.1.tar.gz tar -zxvf redis-8.0.1.tar.gz cd redis-8.0.13. 编译安装make BUILD_TLSyes -j$(nproc) make install PREFIX/usr/local/redis4.创建目录结构mkdir -p /usr/local/redis/{conf,data,logs,run}5. 创建 redis 用户并设置权限useradd -r -s /sbin/nologin redis chown -R redis:redis /usr/local/redis6. 配置环境变量echo export PATH$PATH:/usr/local/redis/bin /etc/profile source /etc/profile7. 验证安装redis-server --version Redis server v8.0.1 sha00000000:1 mallocjemalloc-5.3.0 bits64 builda0879770e7764471显示以上内容说明安装成功。四、配置文件所有节点执行1.创建两个配置文件以 Node1192.168.100.129为例创建两个实例的配置文件cat /usr/local/redis/conf/redis_6379.conf EOF bind 192.168.100.129 127.0.0.1 port 6379 protected-mode no tcp-backlog 65535 timeout 0 tcp-keepalive 300 daemonize yes pidfile /usr/local/redis/run/redis_6379.pid loglevel notice logfile /usr/local/redis/logs/redis_6379.log databases 16 always-show-logo no dir /usr/local/redis/data/6379 # 持久化 save 900 1 save 300 10 save 60 10000 rdbcompression yes rdbchecksum yes dbfilename dump_6379.rdb appendonly yes appenddirname appendonlydir_6379 appendfilename appendonly.aof appendfsync everysec aof-use-rdb-preamble yes # 集群 cluster-enabled yes cluster-config-file nodes-6379.conf cluster-node-timeout 15000 cluster-require-full-coverage yes # 内存 maxmemory 8gb maxmemory-policy allkeys-lru maxmemory-samples 10 # 复制 replica-read-only yes repl-backlog-size 256mb repl-diskless-sync yes repl-diskless-sync-delay 5 # 安全 requirepass YourStrongPassword2024 masterauth YourStrongPassword2024 # 性能 io-threads 4 io-threads-do-reads yes lazyfree-lazy-eviction yes lazyfree-lazy-expire yes lazyfree-lazy-server-del yes # 客户端 maxclients 10000 client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 512mb 128mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 # 慢查询 slowlog-log-slower-than 10000 slowlog-max-len 1000 EOF复制为 6380 实例配置文件批量替换端口cp /usr/local/redis/conf/redis_6379.conf /usr/local/redis/conf/redis_6380.conf sed -i s/6379/6380/g /usr/local/redis/conf/redis_6380.conf2.创建数据目录mkdir -p /usr/local/redis/data/{6379,6380} chown -R redis:redis /usr/local/redis五、创建 systemd 服务所有节点执行1.创建服务配置文件cat /etc/systemd/system/redis.service EOF [Unit] DescriptionRedis Cluster Node %i Afternetwork.target [Service] Typeforking Userredis Groupredis ExecStart/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis_%i.conf ExecStop/usr/local/redis/bin/redis-cli -p %i -a YourStrongPassword2024 shutdown TimeoutStartSec30 TimeoutStopSec30 Restartalways RestartSec5 LimitNOFILE655350 [Install] WantedBymulti-user.target EOF2.加载服务项执行命令systemctl daemon-reload六、启动所有实例所有节点执行systemctl enable --now redis6379 systemctl enable --now redis6380 # 验证状态 systemctl status redis6379 ● redis6379.service - Redis Cluster Node 6379 Loaded: loaded (/etc/systemd/system/redis.service; enabled; preset: disabled) Active: active (running) since Tue 2026-09-22 14:07:45 CST; 1min 32s ago Main PID: 2793 (redis-server) Tasks: 9 (limit: 22760) Memory: 9.3M (peak: 9.8M) CPU: 429ms CGroup: /system.slice/system-redis.slice/redis6379.service └─2793 /usr/local/redis/bin/redis-server 192.168.100.129:6379 [cluster] systemctl status redis6380 ● redis6380.service - Redis Cluster Node 6380 Loaded: loaded (/etc/systemd/system/redis.service; enabled; preset: disabled) Active: active (running) since Tue 2026-09-22 14:08:10 CST; 1min 19s ago Process: 2841 ExecStart/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis_6380.conf (codeexited, status0/SUCCESS) Main PID: 2842 (redis-server) Tasks: 9 (limit: 22760) Memory: 9.2M (peak: 9.4M) CPU: 349ms CGroup: /system.slice/system-redis.slice/redis6380.service └─2842 /usr/local/redis/bin/redis-server 192.168.100.129:6380 [cluster] Sep 22 14:08:10 redis1 systemd[1]: Starting Redis Cluster Node 6380... Sep 22 14:08:10 redis1 systemd[1]: Started Redis Cluster Node 6380. # 验证进程 ps aux | grep redis avahi 851 0.7 0.2 17876 7732 ? Ss 13:59 0:04 avahi-daemon: running [redis1.local] redis 2793 0.4 0.3 383464 13552 ? Ssl 14:07 0:00 /usr/local/redis/bin/redis-server 192.168.100.129:6379 [cluster] redis 2842 0.3 0.3 383464 13564 ? Ssl 14:08 0:00 /usr/local/redis/bin/redis-server 192.168.100.129:6380 [cluster]七、创建集群任意一个节点执行一次执行命令以过程如下显示槽位分配方案确认无误后输入 yes[rootredis1 system]# redis-cli -a YourStrongPassword2024 --cluster create \ 192.168.100.129:6379 192.168.100.129:6380 \ 192.168.100.130:6379 192.168.100.130:6380 \ 192.168.100.131:6379 192.168.100.131:6380 \ --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.100.130:6380 to 192.168.100.129:6379 Adding replica 192.168.100.131:6380 to 192.168.100.130:6379 Adding replica 192.168.100.129:6380 to 192.168.100.131:6379 M: a6375a79ae6b091ca93994934d4459f5df0b1fe7 192.168.100.129:6379 slots:[0-5460] (5461 slots) master S: e4ad47ea2ef79bed5574f9490a0fe07c7b9a9522 192.168.100.129:6380 replicates 5186a7d3470da7c0f6b7b0fecd4fd37c5b9118d5 M: d80a71afe4c600e9aa39f7d884105ef709c2d6f6 192.168.100.130:6379 slots:[5461-10922] (5462 slots) master S: a6d23e2e728ed64b37a9c073630e424cdb6ad3c5 192.168.100.130:6380 replicates a6375a79ae6b091ca93994934d4459f5df0b1fe7 M: 5186a7d3470da7c0f6b7b0fecd4fd37c5b9118d5 192.168.100.131:6379 slots:[10923-16383] (5461 slots) master S: fde59ded93258fc8e987f05e8e6e897eeb7b0bbf 192.168.100.131:6380 replicates d80a71afe4c600e9aa39f7d884105ef709c2d6f6 Can I set the above configuration? (type yes to accept): #以下是输入yes之后显示的内容 [OK] All nodes agree about slots configuration. Check for open slots... Check slots coverage... [OK] All 16384 slots covered.八、验证集群1.检查集群状态从结果中可以看到cluster_state:ok集群整体状态正常。[rootredis1 system]# redis-cli -c -h 192.168.100.129 -p 6379 -a YourStrongPassword2024 cluster info Warning: Using a password with -a or -u option on the command line interface may not be safe. 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:178 cluster_stats_messages_pong_sent:174 cluster_stats_messages_sent:352 cluster_stats_messages_ping_received:169 cluster_stats_messages_pong_received:178 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:352 total_cluster_links_buffer_limit_exceeded:02. 查看节点列表输入命令可以查看到所有节点信息[rootredis1 system]# redis-cli -c -h 192.168.100.129 -p 6379 -a YourStrongPassword2024 cluster nodes Warning: Using a password with -a or -u option on the command line interface may not be safe. e4ad47ea2ef79bed5574f9490a0fe07c7b9a9522 192.168.100.129:638016380 slave 5186a7d3470da7c0f6b7b0fecd4fd37c5b9118d5 0 1790062027836 5 connected a6375a79ae6b091ca93994934d4459f5df0b1fe7 192.168.100.129:637916379 myself,master - 0 0 1 connected 0-5460 d80a71afe4c600e9aa39f7d884105ef709c2d6f6 192.168.100.130:637916379 master - 0 1790062026000 3 connected 5461-10922 5186a7d3470da7c0f6b7b0fecd4fd37c5b9118d5 192.168.100.131:637916379 master - 0 1790062027000 5 connected 10923-16383 a6d23e2e728ed64b37a9c073630e424cdb6ad3c5 192.168.100.130:638016380 slave a6375a79ae6b091ca93994934d4459f5df0b1fe7 0 1790062026741 1 connected fde59ded93258fc8e987f05e8e6e897eeb7b0bbf 192.168.100.131:638016380 slave d80a71afe4c600e9aa39f7d884105ef709c2d6f6 0 1790062025682 3 connected3. 读写测试执行以下命令可以查看到相应的结果[rootredis1 system]# redis-cli -c -h 192.168.100.129 -p 6379 -a YourStrongPassword2024 Warning: Using a password with -a or -u option on the command line interface may not be safe. 192.168.100.129:6379 set test_key hello_redis_cluster - Redirected to slot [15118] located at 192.168.100.131:6379 OK 192.168.100.130:6379 get test_key - Redirected to slot [15118] located at 192.168.100.131:6379 hello_redis_cluster 192.168.100.131:6379 get test_key hello_redis_cluster九、常用运维命令1.检查集群健康状态[rootredis1 system]# redis-cli -a YourStrongPassword2024 --cluster check 192.168.100.129:6379 Warning: Using a password with -a or -u option on the command line interface may not be safe. 192.168.100.129:6379 (a6375a79...) - 0 keys | 5461 slots | 1 slaves. 192.168.100.130:6379 (d80a71af...) - 0 keys | 5462 slots | 1 slaves. 192.168.100.131:6379 (5186a7d3...) - 1 keys | 5461 slots | 1 slaves. [OK] 1 keys in 3 masters. 0.00 keys per slot on average. Performing Cluster Check (using node 192.168.100.129:6379) M: a6375a79ae6b091ca93994934d4459f5df0b1fe7 192.168.100.129:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: e4ad47ea2ef79bed5574f9490a0fe07c7b9a9522 192.168.100.129:6380 slots: (0 slots) slave replicates 5186a7d3470da7c0f6b7b0fecd4fd37c5b9118d5 M: d80a71afe4c600e9aa39f7d884105ef709c2d6f6 192.168.100.130:6379 slots:[5461-10922] (5462 slots) master 1 additional replica(s) M: 5186a7d3470da7c0f6b7b0fecd4fd37c5b9118d5 192.168.100.131:6379 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: a6d23e2e728ed64b37a9c073630e424cdb6ad3c5 192.168.100.130:6380 slots: (0 slots) slave replicates a6375a79ae6b091ca93994934d4459f5df0b1fe7 S: fde59ded93258fc8e987f05e8e6e897eeb7b0bbf 192.168.100.131:6380 slots: (0 slots) slave replicates d80a71afe4c600e9aa39f7d884105ef709c2d6f6 [OK] All nodes agree about slots configuration. Check for open slots... Check slots coverage... [OK] All 16384 slots covered.2.查看节点列表[rootredis1 system]# redis-cli -c -h 192.168.100.129 -p 6379 -a YourStrongPassword2024 info memory Warning: Using a password with -a or -u option on the command line interface may not be safe. # Memory used_memory:2835192 used_memory_human:2.70M used_memory_rss:14749696 used_memory_rss_human:14.07M used_memory_peak:3035968 used_memory_peak_human:2.90M used_memory_peak_perc:93.39% used_memory_overhead:2554444 used_memory_startup:2458504 used_memory_dataset:280748 used_memory_dataset_perc:74.53% allocator_allocated:3972504 allocator_active:4493312 allocator_resident:7835648 allocator_muzzy:0 total_system_memory:3798966272 total_system_memory_human:3.54G used_memory_lua:31744 used_memory_vm_eval:31744 used_memory_lua_human:31.00K used_memory_scripts_eval:0 number_of_cached_scripts:0 number_of_functions:0 number_of_libraries:0 used_memory_vm_functions:32768 used_memory_vm_total:64512 used_memory_vm_total_human:63.00K used_memory_functions:192 used_memory_scripts:192 used_memory_scripts_human:192B maxmemory:8589934592 maxmemory_human:8.00G maxmemory_policy:allkeys-lru allocator_frag_ratio:1.13 allocator_frag_bytes:444776 allocator_rss_ratio:1.74 allocator_rss_bytes:3342336 rss_overhead_ratio:1.88 rss_overhead_bytes:6914048 mem_fragmentation_ratio:5.24 mem_fragmentation_bytes:11932472 mem_not_counted_for_evict:8 mem_replication_backlog:20508 mem_total_replication_buffers:20504 mem_replica_full_sync_buffer:0 mem_clients_slaves:0 mem_clients_normal:0 mem_cluster_links:10720 mem_aof_buffer:8 mem_allocator:jemalloc-5.3.0 mem_overhead_db_hashtable_rehashing:0 active_defrag_running:0 lazyfree_pending_objects:0 lazyfreed_objects:03.实时日志[rootredis1 system]# tail -f /usr/local/redis/logs/redis_6379.log 2793:M 22 Sep 2026 15:21:24.089 * Node e4ad47ea2ef79bed5574f9490a0fe07c7b9a9522 () is no longer master of shard 53def7a083d43e61534b02ca01149018ad1c447f; removed all 0 slot(s) it used to own 2793:M 22 Sep 2026 15:21:24.089 * Node e4ad47ea2ef79bed5574f9490a0fe07c7b9a9522 () is now part of shard 43aa836d884420e6246731e9436fbfd14534f90e 2793:M 22 Sep 2026 15:21:26.726 * Cluster state changed: ok 2793:M 22 Sep 2026 15:21:28.256 * Starting BGSAVE for SYNC with target: replicas sockets (rdb-channel) 2793:M 22 Sep 2026 15:21:28.256 * Starting to deliver RDB and replication stream to replica: 192.168.100.130:6380 2793:M 22 Sep 2026 15:21:28.257 * Background RDB transfer started by pid 3108 to replica socket 3108:C 22 Sep 2026 15:21:28.258 * Fork CoW for RDB: current 0 MB, peak 0 MB, average 0 MB 2793:M 22 Sep 2026 15:21:28.262 * Synchronization with replica 192.168.100.130:6380 succeeded 2793:M 22 Sep 2026 15:21:28.263 * Connection with replica (rdbchannel) 192.168.100.130:6380 lost. 2793:M 22 Sep 2026 15:21:28.359 * Background RDB transfer terminated with success4.重启单个实例systemctl restart redis6379
锦
锦皓数字建站
深耕本土企业品牌数字化升级,专注原创端正雅致商务官网,从视觉设计到稳定运维全程保驾护航。