Redis-一主二从三哨兵高可用模式部署



操作系统环境:
[root@node200 ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@node200 ~]# uname -a
Linux node200 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

这里使用三台服务器,每台服务器上开启一个redis-server和redis-sentinel服务,redis-server端口为6379,redis-sentinel的端口为26379
拓扑主机:
IP                                                                           主机名                                                        类型
172.16.1.200   node200     master
172.16.1.201   node201     slave
172.16.1.202   node202     slave

注意:配置哨兵的前题是,原redis已配置好主从环境,这里配置成一主二从环境

三台都需要配置:
设置内核参数:
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo 1 > /proc/sys/vm/overcommit_memory
echo 511 > /proc/sys/net/core/somaxconn

安装redis:
[root@node200 ~]# yum install redis -y
[root@node200 ~]# rpm -qa | grep redis
redis-3.2.12-2.el7.x86_64

一主两从redis环境配置:
node200 master配置:
[root@node200 ~]# egrep -v '^$|^#' /etc/redis.conf
bind 0.0.0.0     
protected-mode no     
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised systemd
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/var/log/redis/redis.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 "/var/lib/redis"
slave-serve-stale-data yes
slave-read-only no             
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass "123456"     
appendonly no
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 500
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
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


node201 slave配置:
[root@node201 ~]# egrep -v '^$|^#' /etc/redis.conf
bind 0.0.0.0
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised systemd
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/var/log/redis/redis.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 "/var/lib/redis"
slaveof 172.16.1.202 6379
masterauth "123456"
slave-serve-stale-data yes
slave-read-only no
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass "123456"
appendonly no
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-size -2
list-compress-depth 0
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


node202 slave配置:
[root@node202 etc]# egrep -v '^$|^#' /etc/redis.conf
bind 0.0.0.0
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised systemd
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/var/log/redis/redis.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 "/var/lib/redis"
masterauth "123456"
slave-serve-stale-data yes
slave-read-only no
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass "123456"
appendonly no
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-size -2
list-compress-depth 0
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


配置参数说明:
# 绑定的地址
bind 0.0.0.0
# 关闭保护模式
protected-mode no
port 6379
daemonize yes
pidfile "/var/run/redis_6379.pid"
logfile "/var/log/redis/redis.log"
# 默认为yes,设置为no为主从复制,在哨兵模式下,这个要配置为no,因为从节点有可能变成主节点
slave-read-only no
# redis密码
requirepass "123456"
# 从服务器配置连接主服务器的密码,都需要配置,最好主从redis密码一样
masterauth "123456" 
其它两个节点配置一样


启动三个节点的redis服务:
[root@node200 ~]#systemctl start redis
[root@node200 ~]# ps -ef | grep redis
root      6415  6320  0 09:59 pts/0    00:00:00 tail -f /var/log/redis/sentinel.log
redis     6464     1  0 10:09 ?        00:00:10 /usr/bin/redis-server 0.0.0.0:6379
root      6576  6077  0 10:41 pts/1    00:00:00 grep --color=auto redis
[root@node201~]#systemctl start redis
[root@node202 ~]#systemctl start redis


4、修改sentinel.conf文件
# 三台都一致,直接清除原来的配置,变更为如下内容即可:
# vim /etc/redis-sentinel.conf
daemonize yes
logfile /var/log/redis/sentinel.log
protected-mode no
port 26379
dir /tmp
sentinel monitor mymaster 172.16.1.200 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 1800
sentinel auth-pass mymaster 123456


配置注释:
#保护模式 
protected-mode no 

#端口 
port 26379

#守护进程 
daemonize yes

#设置日志文件路径 
logfile /var/log/redis/sentinel.log
#工作路径,不用改 
dir /tmp

# 哨兵监控这个master,在至少quorum个哨兵实例都认为master down后把master标记为odown 
# odown:(objective down) 客观down
# sdown:(subjective down)主观down
# slaves是自动发现,不用指定slaves。 
sentinel monitor mymaster 10.11.0.210 6379 2


# master或slave多长时间(默认30秒)不能使用后标记为s_down状态。 
sentinel down-after-milliseconds mymaster 30000 

#选项指定了在执行故障转移时, 最多可以有多少个从服务器同时对新的主服务器进行同步, 这个数字越小, 完成故障转移所需的时间就越长。 
sentinel parallel-syncs mymaster 1 

#若sentinel在该配置值内未能完成failover操作(即故障时master/slave自动切换),则认为本次failover失败。 
sentinel failover-timeout mymaster 180000


#设置master和slaves的密码
sentinel auth-pass mymaster 123456


5、三台机器都启动哨兵sentinel程序
[root@node200 ~]# systemctl enable redis-sentinel && systemctl start redis-sentinel
[root@node200 ~]# ps -ef | grep redis
root      6415  6320  0 09:59 pts/0    00:00:00 tail -f /var/log/redis/sentinel.log
redis     6464     1  0 10:09 ?        00:00:10 /usr/bin/redis-server 0.0.0.0:6379
redis     6545     1  0 10:30 ?        00:00:02 /usr/bin/redis-sentinel *:26379 [sentinel]
root      6576  6077  0 10:41 pts/1    00:00:00 grep --color=auto redis
[root@node201 ~]# systemctl enable redis-sentinel && systemctl start redis-sentinel
[root@node202 ~]# systemctl enable redis-sentinel && systemctl start redis-sentinel


# 测试
1.关闭主服务器可以自动将另外的slave其中一台提升为master,关闭另外的一台slave,master也可以正常运行
2.将原来的master启动,此时会变成slave
主从选举日志:
[root@node200 ~]#   tail -f /var/log/redis/sentinel.log
6111:X 30 Apr 09:58:43.093 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
6111:X 30 Apr 09:58:43.101 # Sentinel ID is 37e3b426889a5a5272c5a08c08c68d30860b73cd
6111:X 30 Apr 09:58:43.101 # +monitor master mymaster 172.16.1.200 6379 quorum 2
6111:X 30 Apr 09:58:43.103 * +slave slave 172.16.1.202:6379 172.16.1.202 6379 @ mymaster 172.16.1.200 6379
6111:X 30 Apr 09:58:43.104 * +slave slave 172.16.1.201:6379 172.16.1.201 6379 @ mymaster 172.16.1.200 6379
6111:X 30 Apr 09:59:57.130 * +sentinel sentinel d78dbbbd351cb4e2540361c5ab2e524ab032c2f7 172.16.1.200 26379 @ mymaster 172.16.1.200 6379
6111:X 30 Apr 10:02:17.464 * +sentinel sentinel 28263624e5a4c9d5af15bfa1fe059b40ead0ec21 172.16.1.202 26379 @ mymaster 172.16.1.200 6379
6111:X 30 Apr 10:05:52.790 # +new-epoch 1
6111:X 30 Apr 10:05:52.791 # +vote-for-leader 28263624e5a4c9d5af15bfa1fe059b40ead0ec21 1
6111:X 30 Apr 10:05:52.828 # +sdown master mymaster 172.16.1.200 6379
6111:X 30 Apr 10:05:52.901 # +odown master mymaster 172.16.1.200 6379 #quorum 3/2
6111:X 30 Apr 10:05:52.902 # Next failover delay: I will not start a failover before Fri Apr 30 10:05:56 2021
6111:X 30 Apr 10:05:53.497 # +config-update-from sentinel 28263624e5a4c9d5af15bfa1fe059b40ead0ec21 172.16.1.202 26379 @ mymaster 172.16.1.200 6379
6111:X 30 Apr 10:05:53.497 # +switch-master mymaster 172.16.1.200 6379 172.16.1.202 6379
6111:X 30 Apr 10:05:53.497 * +slave slave 172.16.1.201:6379 172.16.1.201 6379 @ mymaster 172.16.1.202 6379
6111:X 30 Apr 10:05:53.497 * +slave slave 172.16.1.200:6379 172.16.1.200 6379 @ mymaster 172.16.1.202 6379
6111:X 30 Apr 10:06:23.532 # +sdown slave 172.16.1.200:6379 172.16.1.200 6379 @ mymaster 172.16.1.202 6379
6111:X 30 Apr 10:09:03.428 # -sdown slave 172.16.1.200:6379 172.16.1.200 6379 @ mymaster 172.16.1.202 6379




主节点DOWN后,如果再重新,将会变成从节点
查看主从复制信息:
127.0.0.1:6379> info Replication
# Replication
role:slave
master_host:172.16.1.202
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1
master_link_down_since_seconds:1619749930
slave_priority:100
slave_read_only:0
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0


通过哨兵26379端口查看集群的信息:
$ redis-cli -p 26379
sentinel master mymaster                //查看master的状态
SENTINEL slaves mymaster         //查看salves的状态
SENTINEL sentinels mymaster //查看哨兵的状态
SENTINEL get-master-addr-by-name mymaster//获取当前master的地址
info sentinel        //查看哨兵信息


[root@node200 ~]# redis-cli -p 26379
127.0.0.1:26379> info
# Server
redis_version:3.2.12
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:7897e7d0e13773f
redis_mode:sentinel
os:Linux 3.10.0-957.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.8.5
process_id:6545
run_id:01b22cc3fc25e793a673998b65191aae9d4cf556
tcp_port:26379
uptime_in_seconds:332
uptime_in_days:0
hz:13
lru_clock:9137442
executable:/usr/bin/redis-sentinel
config_file:/etc/redis-sentinel.conf


# Clients
connected_clients:3
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0


# CPU
used_cpu_sys:1.05
used_cpu_user:0.17
used_cpu_sys_children:0.00
used_cpu_user_children:0.00


# Stats
total_connections_received:3
total_commands_processed:962
instantaneous_ops_per_sec:2
total_net_input_bytes:53762
total_net_output_bytes:5857
instantaneous_input_kbps:0.11
instantaneous_output_kbps:0.01
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets: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=172.16.1.202:6379,slaves=2,sentinels=3  

注意:这种配置模式可能保证三个节点的集群高可用,但需要应用修改IP地址和端口号,可以通过haproxy来实现对应用端透明访问

分割线
感谢打赏
江西数库信息技术有限公司
YWSOS.COM 平台代运维解决方案
 评论
 发表评论
姓   名:

Powered by AKCMS