redis配置哨兵模式
环境:
redis版本:4.0.14
os:Centos7
主: 192.168.1.85:8001
从: 192.168.1.85:8002
哨兵:192.168.1.85:8999
1.主节点安装部署
[root@localhost src]# cd /soft/redis-4.0.14/src
[root@localhost src]# make PREFIX=/opt/redis-shaobing/master install
[root@localhost src]# cp redis-trib.rb /opt/redis-shaobing/master/
[root@localhost src]# mkdir -p /opt/redis-shaobing/master/conf
[root@localhost src]# mkdir -p /opt/redis-shaobing/master/logs
[root@localhost src]# mkdir -p /opt/redis-shaobing/master/run
[root@localhost src]# mkdir -p /opt/redis-shaobing/master/data
2.从节点安装部署
[root@localhost src]# cd /soft/redis-4.0.14/src
[root@localhost src]# make PREFIX=/opt/redis-shaobing/slave install
[root@localhost src]# cp redis-trib.rb /opt/redis-shaobing/slave/
[root@localhost src]# mkdir -p /opt/redis-shaobing/slave/conf
[root@localhost src]# mkdir -p /opt/redis-shaobing/slave/logs
[root@localhost src]# mkdir -p /opt/redis-shaobing/slave/run
[root@localhost src]# mkdir -p /opt/redis-shaobing/slave/data
3.哨兵节点安装
[root@localhost src]# cd /soft/redis-4.0.14/src
[root@localhost src]# make PREFIX=/opt/redis-shaobing/sentinel install
[root@localhost src]# cp redis-trib.rb /opt/redis-shaobing/sentinel/
[root@localhost src]# mkdir -p /opt/redis-shaobing/sentinel/conf
[root@localhost src]# mkdir -p /opt/redis-shaobing/sentinel/logs
[root@localhost src]# mkdir -p /opt/redis-shaobing/sentinel/data
4.主节点配置文件(8001.conf)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | [root@localhost conf]# more 8001.conf bind 192.168.1.85 daemonize yes pidfile "/opt/redis-shaobing/master/run/redis-8001.pid" port 8001 tcp-backlog 511 timeout 300 tcp-keepalive 300 loglevel notice logfile "/opt/redis-shaobing/master/logs/redis-8001.log" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes- on -bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump.rdb" dir "/opt/redis-shaobing/master/data" slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 appendonly yes appendfilename "appendonly.aof" appendfsync everysec no-appendfsync- on -rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set -max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes # Generated by CONFIG REWRITE masterauth "hxlpasswd" requirepass "hxlpasswd" protected -mode yes |
5.从节点配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | [root@localhost conf]# more 8002.conf bind 192.168.1.85 daemonize yes pidfile "/opt/redis-shaobing/slave/run/redis-8002.pid" port 8002 tcp-backlog 511 timeout 300 tcp-keepalive 300 loglevel notice logfile "/opt/redis-shaobing/slave/logs/redis-8002.log" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes- on -bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump.rdb" dir "/opt/redis-shaobing/slave/data" slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 appendonly yes appendfilename "appendonly.aof" appendfsync everysec no-appendfsync- on -rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set -max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes # Generated by CONFIG REWRITE masterauth "hxlpasswd" requirepass "hxlpasswd" protected -mode yes slaveof 192.168.1.85 8001 |
注意这里slaveof
slaveof 192.168.1.85 8001
6.哨兵节点配置文件sentinel.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | port 8999 daemonize yes dir /opt/redis-shaobing/sentinel/data sentinel myid 3154c22b52f5fdca833d7a972bb0104e11e63b82 ##myid可以自己定义 sentinel monitor mymaster 192.168.1.85 8001 1 ##主节点信息 sentinel config-epoch mymaster 1 sentinel leader-epoch mymaster 1 sentinel down-after-milliseconds mymaster 15000 sentinel auth-pass mymaster hxlpasswd ##若是有密码验证的 这里必须加上该项 否则主从无法切换 logfile "/opt/redis-shaobing/sentinel/logs/sentinel.log" protected -mode no # Generated by CONFIG REWRITE ##下面的选择项是系统切换后自动生成的,开始配置的时候不需要 sentinel leader-epoch mymaster 6 sentinel known-slave mymaster 192.168.1.85 8002 sentinel current-epoch 6 |
7.启动主从节点
主节点
/opt/redis-shaobing/master/bin/redis-server /opt/redis-shaobing/master/conf/8001.conf
从节点
/opt/redis-shaobing/slave/bin/redis-server /opt/redis-shaobing/slave/conf/8002.conf
验证主从节点的正确性
主库设置key
/opt/redis-shaobing/master/bin/redis-cli -h 192.168.1.85 -p 8001 -a hxlpasswd
192.168.1.85:7001> set name hxl
从库读取key
/opt/redis-shaobing/slave/bin/redis-cli -h 192.168.1.85 -p 8002 -a hxlpasswd
192.168.1.85:7002> get name
"hxl"
8.启动哨兵
/opt/redis-shaobing/sentinel/bin/redis-sentinel /opt/redis-shaobing/sentinel/conf/sentinel.conf
登陆哨兵
/opt/redis-shaobing/sentinel/bin/redis-cli -h 192.168.1.85 -p 8999
查看信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | [root@localhost logs]# /opt/redis-shaobing/sentinel/bin/redis-cli -h 192.168.1.85 -p 8999 192.168.1.85:8999> info # Server redis_version:4.0.14 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:4af9eba2e02c1ef9 redis_mode:sentinel os:Linux 3.10.0-862.el7.x86_64 x86_64 arch_bits:64 multiplexing_api:epoll atomicvar_api:atomic-builtin gcc_version:4.8.5 process_id:17899 run_id:1b23616c45b7e0ff82aad6d5ea34f5065827c301 tcp_port:8999 uptime_in_seconds:12 uptime_in_days:0 hz:15 lru_clock:7441241 executable:/opt/redis-shaobing/sentinel/bin/redis-sentinel config_file:/opt/redis-shaobing/sentinel/conf/sentinel.conf # Clients connected_clients:1 client_longest_output_list:0 client_biggest_input_buf:0 blocked_clients:0 # CPU used_cpu_sys:0.02 used_cpu_user:0.01 used_cpu_sys_children:0.00 used_cpu_user_children:0.00 # Stats total_connections_received:1 total_commands_processed:0 instantaneous_ops_per_sec:0 total_net_input_bytes:31 total_net_output_bytes:60 instantaneous_input_kbps:0.00 instantaneous_output_kbps:0.00 rejected_connections:0 sync_full:0 sync_partial_ok:0 sync_partial_err:0 expired_keys:0 expired_stale_perc:0.00 expired_time_cap_reached_count:0 evicted_keys:0 keyspace_hits:0 keyspace_misses:0 pubsub_channels:0 pubsub_patterns:0 latest_fork_usec:0 migrate_cached_sockets:0 slave_expires_tracked_keys:0 active_defrag_hits:0 active_defrag_misses:0 active_defrag_key_hits:0 active_defrag_key_misses:0 # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=192.168.1.85:8001,slaves=0,sentinels=1 |
9.模拟主节点down掉
/opt/redis-shaobing/master/bin/redis-cli -h 192.168.1.85 -p 8001 -a hxlpasswd shutdown
再次登陆哨兵查看信息,发现现在的主节点完成了切换
[root@localhost logs]# /opt/redis-shaobing/sentinel/bin/redis-cli -h 192.168.1.85 -p 8999
192.168.1.85:8999> info
.
.
.
前面输出省略
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.1.85:8002,slaves=1,sentinels=1
10.原来down掉的节点重启启动
启动后发现原来停掉的节点不会重新成为主节点,而是成为从节点
11.python程序连接哨兵模式
# !/usr/bin/env python # -*- coding:utf-8 -*- import redis from redis.sentinel import Sentinel # 连接哨兵服务器(主机名也可以用域名) sentinel = Sentinel([('192.168.1.85', 8999)],socket_timeout=0.5) # 获取主服务器地址 master = sentinel.discover_master('mymaster') print(master) # 获取从服务器地址 slave = sentinel.discover_slaves('mymaster') print(slave) # 获取主服务器进行写入 master = sentinel.master_for('mymaster', socket_timeout=0.5, password='hxlpasswd') w_ret = master.set('foo', 'bar') # 输出:True # # 获取从服务器进行读取(默认是round-roubin) slave = sentinel.slave_for('mymaster', socket_timeout=0.5, password='hxlpasswd') r_ret = slave.get('foo') print(r_ret) # # 输出:bar
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?