Redis+sentinel 高可用实践
1、环境规划
10.213.50.138(主) redis+sentinel
10.213.50.168(从) redis+sentinel
10.213.50.227 作为客户端测试插入数
2、redis部署
wget http://download.redis.io/releases/redis-3.2.9.tar.gz
1: 解压到/usr/local/src 目录:放源码包 root@redis1:/usr/local/src# tar zxvf redis-3.2.9.tar.gz root@redis1:/usr/local/src# ls -lh drwxrwxr-x 6 root root 4.0K 2月 22 15:01 redis-3.2.9 2:创建目录:/usr/local/redis,编译安装到目录/usr/local/redis/ --放执行文件 make PREFIX=/usr/local/redis install 3:程序做软连接到bin目录,方便直接执行 ln -s /usr/local/redis/bin/redis-cli /usr/local/bin/redis-cli ln -s /usr/local/redis/bin/redis-sentinel /usr/local/bin/redis-sentinel ln -s /usr/local/redis/bin/redis-server /usr/local/bin/redis-server
1:复制配置文件,在源码包里有sentinel.conf和redis.conf文件,复制到/etc/redis/目录下,如果有多个实例,建议改名,如本实例用的redis端口是7021,sentinel是17021: cp /usr/local/src/redis-3.2.9/redis.conf /etc/redis/redis_7021.conf cp /usr/local/src/redis-3.2.9/sentinel.conf /etc/redis/sentinel_17021.conf 后面会给出该配置文件的相关参数 2:做成一个服务,在源码包里的utils目录下的redis_init_script文件复制到/etc/init.d/下,并且改名成redis,以后就可以通过这个程序进行关闭和开启了。 cp /usr/local/src/redis-3.2.9/utils/redis_init_script /etc/init.d/redis 3:修改/etc/init.d/redis文件(后面会给出redis程序的代码),使得开启和关闭,以及关闭的时候需要输入密码(假设redis需要认证)。
3、redis配置(master)
bind 0.0.0.0 protected-mode yes port 6379 daemonize yes pidfile "/var/run/redis_6379.pid" logfile "/var/log/redis_6379.log" redis从配置 bind 0.0.0.0 protected-mode yes port 6379 daemonize yes pidfile "/var/run/redis_6379.pid" logfile "/var/log/redis_6379.log" slaveof 10.213.50.138 6379 #从redis比主redis多这一行,
#######master设置密码的情况下同步数据,其实很简单,我们只要让slave能连上master就可以了,我们在slave的配置文件中加一句话即可。
masterauth 123456
sentinel 则也要加入加密,放在monitor下面
sentinel monitor master 127.0.0.1 6379 1
sentinel auth-pass mymaster redis123
启动redis并检查info replication 是否正常 redis-server /etc/redis/6379.conf 在从redis上面观察
4、配置redis-sentinel
protected-mode no daemonize yes port 26379 logfile "/var/log/redis/sentinel.log" pidfile "/var/run/sentinel.pid" sentinel monitor mymaster 10.213.50.138 6379 1 sentinel down-after-milliseconds mymaster 5000 sentinel failover-timeout mymaster 15000 sentinel client-reconfig-script mymaster /etc/notify_mymaster.sh 启动redis-sentinel 并检查状态 redis-sentinel /etc/sentinel.conf
5、vip漂移脚本,需要执行权限
cat /etc/notify_mymaster.sh #!/bin/bash MASTER_IP=$6 LOCAL_IP='10.213.50.168' VIP='10.213.50.188' NETMASK='24' INTERFACE='eth0' if [ ${MASTER_IP} = ${LOCAL_IP} ]; then /sbin/ip addr add ${VIP}/${NETMASK} dev ${INTERFACE} /sbin/arping -q -c 3 -A ${VIP} -I ${INTERFACE} exit 0 else /sbin/ip addr del ${VIP}/${NETMASK} dev ${INTERFACE} exit 0 fi exit 1
6、客户端测试数据
cat redis.sh #!/bin/bash for ((i=0;i<20000;i++)) do echo -en "helloworld" | redis-cli -h 10.213.50.188 -x set name$i >>redis.log sleep 1 done
7、绑定vip到主库
/sbin/ip addr add 10.213.50.188/24 dev eth0
8、停止主redis观察从库
从库变成了主库,并且vip地址漂移过来了
客户端插入数据会中断,中断时间就是redis vip漂移及redis从库提升到主库的时间