哨兵模式主要用于主从库异常检测,自动完成异常状态下的切换,演示下配置过程。
配置环境:
[root@Redis-01 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@Redis-01 ~]# uname -a
Linux Redis-01 3.10.0-693.17.1.el7.x86_64 #1 SMP Thu Jan 25 20:13:58 UTC 2018 x86_64 x86_64 x86_64 GNU/LinuxSourceByrd's Weblog-https://note.t4x.org/basic/redis-sentinel-listen/主库: 1.1.1.151 从库:1.1.1.152 SourceByrd's Weblog-https://note.t4x.org/basic/redis-sentinel-listen/
主从搭建:SourceByrd's Weblog-https://note.t4x.org/basic/redis-sentinel-listen/
SourceByrd's Weblog-https://note.t4x.org/basic/redis-sentinel-listen/
012345678910111213141516171819202122 $ yum install gcc gcc-c++ vim wget tcl tcl-devel -y$ cd redis-3.2.0$ make MALLOC=jemalloc$ make PREFIX=/opt/redis-3.2.0 install$ ln -s opt/redis-3.2.0 /usr/local/redis$ mkdir /usr/local/redis/{etc,cache}$ cp redis.conf /usr/local/redis/etc$ sysctl vm.overcommit_memory=1$ echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf$ echo 1024 >/proc/sys/net/core/somaxconn$ cd /usr/local/redis/etc$ cp redis.conf redis.conf.$(date +%F)$ sed -i "s#daemonize no#daemonize yes#g" redis.conf$ sed -i 's#logfile ""#logfile "/var/log/redis.log"#g' redis.conf$ sed -i 's#dir ./#dir /usr/local/redis/cache#g' redis.conf$ sed -i "s#appendonly no#appendonly yes#g" redis.conf$ mkdir /usr/local/redis/{etc,cache}##主##$ sed -i 's/\# requirepass foobared/requirepass 123456/g' redis.conf##从##$ sed -i 's/\# slaveof <masterip> <masterport>/slaveof 192.168.227.23 6379/g' redis.conf$ sed -i 's/\# masterauth <master-password>/masterauth 123456/g' redis.conf主库状态:
012345678910111213 $ /usr/local/redis/bin/redis-cli -h 192.168.227.23 -p 6379 -a 123456 info ReplicationWarning: Using a password with '-a' option on the command line interface may not be safe.# Replicationrole:masterconnected_slaves:1slave0:ip=192.168.227.24,port=6379,state=online,offset=182,lag=1master_replid:e7e539b15b1c0014d88e66a0cee61b0503138733master_replid2:0000000000000000000000000000000000000000master_repl_offset:196second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:196
从库状态:
SourceByrd's Weblog-https://note.t4x.org/basic/redis-sentinel-listen/
01234567891011121314151617181920 $ /usr/local/redis/bin/redis-cli -h 192.168.227.24 -p 6379 -a 123456 info ReplicationWarning: Using a password with '-a' option on the command line interface may not be safe.# Replicationrole:slavemaster_host:192.168.227.23master_port:6379master_link_status:upmaster_last_io_seconds_ago:7master_sync_in_progress:0slave_repl_offset:238slave_priority:100slave_read_only:1connected_slaves:0master_replid:e7e539b15b1c0014d88e66a0cee61b0503138733master_replid2:0000000000000000000000000000000000000000master_repl_offset:238second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:43repl_backlog_histlen:196
哨兵模式:SourceByrd's Weblog-https://note.t4x.org/basic/redis-sentinel-listen/
SourceByrd's Weblog-https://note.t4x.org/basic/redis-sentinel-listen/
01234567891011121314151617 $ cat /usr/local/redis/etc/sentinel.confprotected-mode no# 切换主从需要2台同意,可以配置为1台sentinel monitor t4x 192.168.227.23 6379 1sentinel down-after-milliseconds t4x 30000sentinel failover-timeout t4x 180000sentinel notification-script t4x /data/scripts/redis_sentinel.shprotected-mode noport 26379dir /tmpsentinel monitor mymaster 127.0.0.1 6379 2sentinel auth-pass mymaster 123456sentinel down-after-milliseconds mymaster 20000sentinel parallel-syncs mymaster 1sentinel failover-timeout mymaster 20000sentinel deny-scripts-reconfig yes
Redis启动脚本:
0 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 |
#!/bin/bash # Author:Byrd # Version:0.1 # Site:note.t4x.org # Contact:root#t4x.org redis_dir="/usr/local/redis" redis_config="/usr/local/redis/etc/redis.conf" redis_status=`netstat -tunpl | grep 6379 | wc -l` start () { if [ "{redis_status}" -eq 0 ]; then {redis_dir}/bin/redis-server ${redis_config} & return_value=$? if [ "${return_value}" -eq 0 ]; then echo "redis start successful" else echo "redis start fault, please check config file" fi else echo "redis in running" fi } stop () { if [ {redis_status} -gt 0 ]; then redis_pid=`netstat -tunpl | grep redis | awk "NR==1" | awk '{print $7}' | awk -F "/" '{print $1}'` kill -9 {redis_pid} return_value=$? if [ "${return_value}" -eq 0 ]; then echo "redis stop successful" else echo "redis stop fault, please check redis status" fi else echo "redis in not running, please execure $0 start" fi } case "$1" in start) start && exit 0 ;; stop) stop || exit 2 ;; *) echo $"Usage: $0 {start|stop}" exit 2 esac exit $? |
参考文档
1:http://blog.csdn.net/u014756827/article/details/52383103
2:http://blog.csdn.net/a67474506/article/details/50435498SourceByrd's Weblog-https://note.t4x.org/basic/redis-sentinel-listen/
SourceByrd's Weblog-https://note.t4x.org/basic/redis-sentinel-listen/