redis-3.2.11哨兵模式的配置
设置内核参数: echo never > /sys/kernel/mm/transparent_hugepage/enabled echo 1 > /proc/sys/vm/overcommit_memory echo 511 > /proc/sys/net/core/somaxconn 1、下载安装包redis-3.2.11.tar.gz # cd /usr/local/src # wget http://download.redis.io/releases/redis-3.2.11.tar.gz 2、解压及安装 [root@node1 src]# tar zxf redis-3.2.11.tar.gz [root@node1 src]# cd redis-3.2.11/src/ [root@node1 redis-3.2.8]# make PREFIX=/usr/local/redis3 install #指定安装路径 [root@node1 src]# mkdir /etc/redis [root@node1 src]# cp ../redis.conf /etc/redis/redis_6379.conf # 设置启动脚本 [root@node1 redis-3.2.11]# vim /etc/init.d/redis3 #!/bin/sh # chkconfig: 2345 90 10 # Simple Redis init.d script conceived to work on Linux systems # as it does use of the /proc filesystem. REDISPORT=6379 EXEC=/usr/local/redis3/bin/redis-server CLIEXEC=/usr/local/redis3/bin/redis-cli PIDFILE=/var/run/redis_${REDISPORT}.pid CONF="/etc/redis/redis_${REDISPORT}.conf" usage(){ echo "usage: $0 [start|stop|status|restart]" } redis_start(){ if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF fi } redis_stop(){ if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $CLIEXEC -h 10.11.0.210 -p $REDISPORT -a redispass shutdown # 需要修改为各自主机的内网IP地址 while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi } redis_restart(){ redis_stop sleep 1 redis_start } redis_status(){ ps -ef|grep redis|grep -v grep|grep -v status } main(){ case "$1" in start) redis_start;; stop) redis_stop;; status) redis_status;; restart) redis_restart;; *) usage; esac } main $1 3、修改redis配置 master redis_6379.conf配置: [root@node1 redis-3.2.11]# egrep -v '^$|^#' /etc/redis/redis_6379.conf # 绑定的地址 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为主从复制 slave-read-only no # redis密码 requirepass "redispass" # 从服务器配置连接主服务器的密码,都需要配置,最好主从redis密码一样 masterauth "redispass" slave01 redis_6379.conf配置: [root@node02 redis-3.2.11]# egrep -v '^$|^#' /etc/redis/redis_6379.conf bind 0.0.0.0 protected-mode no port 6379 daemonize yes pidfile "/var/run/redis_6379.pid" dir "/usr/local/src/redis-3.2.11" slave-read-only no requirepass "redispass" masterauth "redispass" slaveof 10.11.0.210 6379 slave01 redis_6379.conf配置: [root@node5 ~]# egrep -v '^$|^#' /etc/redis/redis_6379.conf bind 0.0.0.0 protected-mode no port 6379 daemonize yes pidfile "/var/run/redis_6379.pid" dir "/usr/local/src/redis-3.2.11" slave-read-only no slave-priority 100 requirepass "redispass" slaveof 10.11.0.210 6379 masterauth "redispass" 4、修改sentinel.conf文件 # 三台都一致,直接清除原来的配置,变更为如下内容即可: # vim sentinel.conf daemonize yes logfile /var/log/sentine.log protected-mode no port 26379 dir /tmp sentinel monitor mymaster 10.11.0.210 6379 2 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 1800 sentinel auth-pass mymaster redispass 配置注释: #保护模式 protected-mode no #端口 port 26381 #守护进程 daemonize yes #设置日志文件路径 logfile /var/log/sentine.log #工作路径,不用改 dir /tmp # 哨兵监控这个master,在至少quorum个哨兵实例都认为master down后把master标记为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 redispass 5、三台机器都启动哨兵sentinel程序 [root@node1 redis-3.2.11]# src/redis-sentinel sentinel.conf # 测试 1.关闭主服务器可以自动将另外的slave其中一台提升为master,关闭另外的一台slave,master也可以正常运行 2.将原来的master启动,此时会变成slave 查看主从复制信息: [root@node1 redis-3.2.11]# redis-cli -a redispass info replication # Replication role:master connected_slaves:2 slave0:ip=10.11.0.215,port=6379,state=online,offset=111812,lag=0 slave1:ip=10.11.0.212,port=6379,state=online,offset=111812,lag=0 master_repl_offset:111812 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:111811 # 关闭redis服务命令 [root@node1 redis-3.2.11]# redis-cli -a redispass shutdown 观察日志: # tail -f /var/log/sentine.log