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)

 

[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.從節點配置文件

[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

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
查看信息

 

[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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM