一、环境
系统 CentOS 6.4x64最小化安装
redis-m 192.168.3.61
redis-s 192.168.3.62
vip 192.168.3.63
二、安装redis-m
#下载软件 [root@redis-m ~]# wget http://download.redis.io/releases/redis-2.8.9.tar.gz [root@redis-m ~]# tar xf redis-2.8.9.tar.gz [root@redis-m ~]# cd redis-2.8.9 [root@redis-m redis-2.8.9]# make [root@redis-m redis-2.8.9]# make PREFIX=/usr/local/redis-2.8.9 install [root@redis-m redis-2.8.9]# ln -s /usr/local/redis-2.8.9/ /usr/local/redis [root@redis-m redis-2.8.9]# echo ‘PATH=$PATH:/usr/local/redis/bin‘ >>/etc/profile [root@redis-m redis-2.8.9]# source /etc/profile #复制配置文件 [root@redis-m ~]# mkdir /usr/local/redis/conf [root@redis-m ~]# cp /root/redis-2.8.9/redis.conf /usr/local/redis/conf/ #修改redis启动产生的警告 [root@redis-m ~]# sysctl vm.overcommit_memory=1 vm.overcommit_memory = 1 [root@redis-m ~]# echo "vm.overcommit_memory = 1" >>/etc/sysctl.conf [root@redis-m ~]# sysctl -p
创建启动脚本
[root@redis-m ~]# vim /etc/init.d/redis #!/bin/bash # chkconfig: 2345 50 30 # # description: Redis service # #Script:Redis command Redisserver=/usr/local/redis/bin/redis-server Rediscli=/usr/local/redis/bin/redis-cli Redisconf=/usr/local/redis/conf/redis.conf function_start() { printf "start redis-server..." $Redisserver $Redisconf &>/dev/null & if [ $? -eq 0 ];then echo "runing" fi } function_stop() { printf "stop redis-server..." $Rediscli -p 6379 shutdown if [ $? -eq 0 ];then echo "stop" fi } function_restart() { function_start function_stop } function_kill() { killall redis-server } function_status() { a=`ps -A|grep "redis-server\>" -c` if [ $a -ge 1 ];then echo -e "The Redis is [\e[0;32;5m runing \e[0m]" else echo -e "The Redis is [\e[0;31;5m not run \e[0m]" fi } case "$1" in start) function_start ;; stop) function_stop ;; restart) function_stop function_start ;; kill) function_kill ;; status) function_status ;; *) echo "Usage: /etc/init.d/redis {start|stop|restart|kill|status}" esac exit #将redis服务添加到开机自启动 [root@redis-m ~]# chmod +x /etc/init.d/redis [root@redis-m ~]# chkconfig --add redis [root@redis-m ~]# chkconfig redis on [root@redis-m ~]# service redis start #启动redis服务 start redis-server...runing [root@redis-m ~]# netstat -tunlp |grep redis tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 3277/redis-server * tcp 0 0 :::6379 :::* LISTEN 3277/redis-server *
修改redis配置文件
[root@redis-m ~]# egrep -v "^$|^#" /usr/local/redis/conf/redis.conf daemonize no pidfile /var/run/redis.pid port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice logfile "" 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 /usr/local/redis slave-serve-stale-data yes slave-read-only yes repl-disable-tcp-nodelay no slave-priority 100 appendonly yes #需要打开aof持久化 appendfilename "appendonly.aof" appendfsync everysec #选择everysec方式 no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 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
重启redis服务
[root@redis-m ~]# service redis restart stop redis-server...stop start redis-server...runing [root@redis-m ~]# netstat -tunlp |grep redis tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 3324/redis-server * tcp 0 0 :::6379 :::* LISTEN 3324/redis-server *
三、安装redis-s
安装过程和redis-m基本上是一样的
[root@redis-s ~]# tar xf redis-2.8.9.tar.gz [root@redis-s ~]# cd redis-2.8.9 [root@redis-s redis-2.8.9]# make [root@redis-s redis-2.8.9]# make PREFIX=/usr/local/redis-2.8.9 install [root@redis-s redis-2.8.9]# ln -s /usr/local/redis-2.8.9/ /usr/local/redis [root@redis-s ~]# echo ‘PATH=$PATH:/usr/local/redis/bin‘ >>/etc/profile [root@redis-s ~]# source /etc/profile [root@redis-s ~]# mkdir /usr/local/redis/conf [root@redis-s ~]# cp /root/redis-2.8.9/redis.conf /usr/local/redis/conf/ [root@redis-s ~]# sysctl vm.overcommit_memory=1 vm.overcommit_memory = 1 [root@redis-s ~]# echo "vm.overcommit_memory = 1" >>/etc/sysctl.conf [root@redis-s ~]# sysctl -p
修改后的redis.conf文件内容如下
[root@redis-s ~]# egrep -v "^$|^#" /usr/local/redis/conf/redis.conf daemonize no pidfile /var/run/redis.pid port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice logfile "" 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 /usr/local/redis slaveof 192.168.3.61 6379 #主从同步的信息 slave-serve-stale-data yes slave-read-only yes repl-disable-tcp-nodelay no slave-priority 100 appendonly yes #从服务器也需要打开aof持久化 appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 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
从redis-m上复制启动脚本,并添加到开机自启动
[root@redis-m ~]# scp /etc/init.d/redis 192.168.3.62:/etc/init.d/ [root@redis-s ~]# chkconfig --add redis [root@redis-s ~]# chkconfig redis on [root@redis-s ~]# service redis start [root@redis-s ~]# netstat -tunlp |grep redis tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 3336/redis-server * tcp 0 0 :::6379 :::* LISTEN 3336/redis-server *
四、测试主从同步
在redis-m上查看主从同步信息
[root@redis-m ~]# redis-cli info replication # Replication role:master connected_slaves:1 slave0:ip=192.168.3.62,port=6379,state=online,offset=113,lag=1 master_repl_offset:113 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:112
在redis-s上查看主从同步信息
[root@redis-s ~]# redis-cli info replication # Replication role:slave master_host:192.168.3.61 master_port:6379 master_link_status:up master_last_io_seconds_ago:5 master_sync_in_progress:0 slave_repl_offset:99 slave_priority:100 slave_read_only:1 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
在redis-m上写入一个数据
[root@redis-m ~]# redis-cli set k1 v1 OK [root@redis-m ~]# redis-cli get k1 "v1"
在redis-s上查看数据k1
[root@redis-s ~]# hostname redis-s [root@redis-s ~]# redis-cli get k1 "v1"
从上面结果能看出主从同步时正常的
五、在redis-m上安装keepalived
[root@redis-m ~]# yum install openssl openssl-devel -y [root@redis-m ~]# wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz [root@redis-m ~]# tar xf keepalived-1.2.13.tar.gz [root@redis-m ~]# cd keepalived-1.2.13 [root@redis-m keepalived-1.2.13]# ./configure [root@redis-m keepalived-1.2.13]# make && make install [root@redis-m keepalived-1.2.13]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/ [root@redis-m keepalived-1.2.13]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ [root@redis-m keepalived-1.2.13]# mkdir /etc/keepalived [root@redis-m keepalived-1.2.13]# ln -s /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ [root@redis-m keepalived-1.2.13]# ln -s /usr/local/sbin/keepalived /usr/sbin/ [root@redis-m keepalived-1.2.13]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
编辑后的keepalived配置文件内容如下
[root@redis-m keepalived-1.2.13]# cat /etc/keepalived/keepalived.conf vrrp_script chk_redis { script "/etc/keepalived/scripts/redis_check.sh" ###监控脚本 interval 2 ###监控时间 } vrrp_instance VI_1 { state MASTER ###设置为MASTER interface eth0 ###监控网卡 virtual_router_id 51 priority 101 ###权重值 authentication { auth_type PASS ###加密 auth_pass redis ###密码 } track_script { chk_redis ###执行上面定义的chk_redis } virtual_ipaddress { 192.168.3.63 ###VIP } notify_master /etc/keepalived/scripts/redis_master.sh notify_backup /etc/keepalived/scripts/redis_backup.sh notify_fault /etc/keepalived/scripts/redis_fault.sh notify_stop /etc/keepalived/scripts/redis_stop.sh }
创建监控redis的脚本
[root@redis-m ~]# mkdir /etc/keepalived/scripts #编写监控脚本 [root@redis-m ~]# cat /etc/keepalived/scripts/redis_check.sh #!/bin/bash ALIVE=`/usr/local/redis/bin/redis-cli PING` if [ "$ALIVE" == "PONG" ]; then echo $ALIVE exit 0 else echo $ALIVE exit 1 fi
创建进入master状态的脚本
[root@redis-m ~]# cat /etc/keepalived/scripts/redis_master.sh #!/bin/bash REDISCLI="/usr/local/redis/bin/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" echo "[master]" >> $LOGFILE date >> $LOGFILE echo "Being master...." >> $LOGFILE 2>&1 echo "Run SLAVEOF cmd ..." >> $LOGFILE $REDISCLI SLAVEOF 192.168.3.62 6379 >> $LOGFILE 2>&1 sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态 echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
创建进入backup状态的脚本
[root@redis-m ~]# cat /etc/keepalived/scripts/redis_backup.sh #!/bin/bash REDISCLI="/usr/local/redis/bin/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" echo "[backup]" >> $LOGFILE date >> $LOGFILE echo "Being slave...." >> $LOGFILE 2>&1 sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色 echo "Run SLAVEOF cmd ..." >> $LOGFILE $REDISCLI SLAVEOF 192.168.3.62 6379 >> $LOGFILE 2>&1
创建脚本/etc/keepalived/scripts/redis_fault.sh
[root@redis-m ~]# cat /etc/keepalived/scripts/redis_fault.sh #!/bin/bash LOGFILE=/var/log/keepalived-redis-state.log echo "[fault]" >> $LOGFILE date >> $LOGFILE
创建脚本/etc/keepalived/scripts/redis_stop.sh
[root@redis-m ~]# cat /etc/keepalived/scripts/redis_stop.sh #!/bin/bash LOGFILE=/var/log/keepalived-redis-state.log echo "[stop]" >> $LOGFILE date >> $LOGFILE
赋予脚本执行权限
[root@redis-m ~]# chmod +x /etc/keepalived/scripts/*.sh
六、在redis-s上安装keepalived
安装过程和redis-m上的是一样
[root@redis-s ~]# yum install openssl openssl-devel -y [root@redis-s ~]# tar xf keepalived-1.2.13.tar.gz [root@redis-s ~]# cd keepalived-1.2.13 [root@redis-s keepalived-1.2.13]# ./configure [root@redis-s keepalived-1.2.13]# make && make install [root@redis-s keepalived-1.2.13]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/ [root@redis-s keepalived-1.2.13]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ [root@redis-s keepalived-1.2.13]# mkdir /etc/keepalived [root@redis-s keepalived-1.2.13]# ln -s /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ [root@redis-s keepalived-1.2.13]# ln -s /usr/local/sbin/keepalived /usr/sbin/ [root@redis-s keepalived-1.2.13]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
修改后的keepalived.conf配置文件内容如下
[root@redis-s ~]# cat /etc/keepalived/keepalived.conf vrrp_script chk_redis { script "/etc/keepalived/scripts/redis_check.sh" ###监控脚本 interval 2 ###监控时间 } vrrp_instance VI_1 { state BACKUP ###设置为BACKUP interface eth0 ###监控网卡 virtual_router_id 51 priority 100 ###比MASTRE权重值低 authentication { auth_type PASS auth_pass redis ###密码与MASTRE相同 } track_script { chk_redis ###执行上面定义的chk_redis } virtual_ipaddress { 192.168.3.63 ###VIP } notify_master /etc/keepalived/scripts/redis_master.sh notify_backup /etc/keepalived/scripts/redis_backup.sh notify_fault /etc/keepalived/scripts/redis_fault.sh notify_stop /etc/keepalived/scripts/redis_stop.sh
创建监控redis的脚本
[root@redis-s ~]# mkdir /etc/keepalived/scripts #脚本内容如下 [root@redis-s ~]# cat /etc/keepalived/scripts/redis_check.sh #!/bin/bash ALIVE=`/usr/local/redis/bin/redis-cli PING` if [ "$ALIVE" == "PONG" ]; then echo $ALIVE exit 0 else echo $ALIVE exit 1 fi
创建进入master状态的脚本
[root@redis-s ~]# cat /etc/keepalived/scripts/redis_master.sh #!/bin/bash REDISCLI="/usr/local/redis/bin/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" echo "[master]" >> $LOGFILE date >> $LOGFILE echo "Being master...." >> $LOGFILE 2>&1 echo "Run SLAVEOF cmd ..." >> $LOGFILE $REDISCLI SLAVEOF 192.168.3.61 6379 >> $LOGFILE 2>&1 sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态 echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
创建进入backup的脚本
[root@redis-s ~]# cat /etc/keepalived/scripts/redis_backup.sh #!/bin/bash REDISCLI="/usr/local/redis/bin/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" echo "[backup]" >> $LOGFILE date >> $LOGFILE echo "Being slave...." >> $LOGFILE 2>&1 sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色 echo "Run SLAVEOF cmd ..." >> $LOGFILE $REDISCLI SLAVEOF 192.168.3.61 6379 >> $LOGFILE 2>&1
创建fault状态的脚本
[root@redis-s ~]# cat /etc/keepalived/scripts/redis_fault.sh #!/bin/bash LOGFILE=/var/log/keepalived-redis-state.log echo "[fault]" >> $LOGFILE date >> $LOGFILE
创建stop状态的脚本
[root@redis-s ~]# cat /etc/keepalived/scripts/redis_stop.sh #!/bin/bash LOGFILE=/var/log/keepalived-redis-state.log echo "[stop]" >> $LOGFILE date >> $LOGFILE
赋予脚本执行权限
[root@redis-s ~]# chmod +x /etc/keepalived/scripts/*.sh
七、测试
依次启动redis-m,redis-s上的keepalived服务
#启动redis-m的keepalived服务 [root@redis-m ~]# /etc/init.d/keepalived start Starting keepalived: [ OK ] #启动redis-s上的keepalived服务 [root@redis-s ~]# /etc/init.d/keepalived start Starting keepalived: [ OK ] [root@redis-m ~]# ip a |grep eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 inet 192.168.3.61/24 brd 192.168.3.255 scope global eth0 inet 192.168.3.63/32 scope global eth0 #结果正常 [root@redis-s ~]# ip a |grep eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 inet 192.168.3.62/24 brd 192.168.3.255 scope global eth0
尝试通过vip连接redis
#查看master的信息 [root@redis-s ~]# redis-cli -h 192.168.3.63 info Replication # Replication role:master connected_slaves:1 slave0:ip=192.168.3.62,port=6379,state=online,offset=85,lag=0 master_repl_offset:85 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:84 #写入一个数据k3,v3 [root@redis-s ~]# redis-cli -h 192.168.3.63 set k3 v3 OK [root@redis-s ~]# redis-cli -h 192.168.3.63 get k3 "v3" #连接到redis-m:192.168.3.61上查询结果 [root@redis-s ~]# redis-cli -h 192.168.3.61 get k3 "v3" #连接到redis-s:192.168.3.62上查询结果 [root@redis-s ~]# redis-cli -h 192.168.3.62 get k3 "v3"
模拟故障,将redis-m上的redis杀死
[root@redis-m ~]# killall -9 redis-server #查看redis-m上的keepalived日志 [root@redis-m ~]# tail /var/log/keepalived-redis-state.log Wed Jul 29 11:51:39 CST 2015 [master] Wed Jul 29 11:51:42 CST 2015 Being master.... Run SLAVEOF cmd ... OK Run SLAVEOF NO ONE cmd ... OK [fault] Wed Jul 29 11:56:12 CST 2015 #查看redis-s上的keepalived日志 [root@redis-s ~]# tail /var/log/keepalived-redis-state.log Being slave.... Run SLAVEOF cmd ... OK Already connected to specified master [master] Wed Jul 29 11:56:14 CST 2015 Being master.... Run SLAVEOF cmd ... OK Already connected to specified master Run SLAVEOF NO ONE cmd ... OK #查看redis状态信息 [root@redis-m ~]# redis-cli -h 192.168.3.63 info Replication # Replication role:master 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 [root@redis-m ~]# redis-cli -h 192.168.3.62 info Replication # Replication role:master #从这里我们能看到redis-s已经切换到master角色了 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 #查询之前key [root@redis-m ~]# redis-cli -h 192.168.3.63 get k3 "v3" [root@redis-m ~]# redis-cli -h 192.168.3.63 set k4 v4 OK #从这里我们能看出redis-s切换成master角色后是可以写数据的 [root@redis-m ~]# redis-cli -h 192.168.3.63 get k4 "v4" #连接到redis-s:192.168.3.62查看结果 [root@redis-m ~]# redis-cli -h 192.168.3.62 get k4 "v4"
恢复redis-m的redis服务器和keepalived服务
#先恢复redis服务 [root@redis-m ~]# service redis start start redis-server...runing [root@redis-m ~]# netstat -tunlp |grep redis tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 34382/redis-server tcp 0 0 :::6379 :::* LISTEN 34382/redis-server #再恢复keepalived服务 [root@redis-m ~]# /etc/init.d/keepalived start [root@redis-m ~]# ip a |grep eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 inet 192.168.3.61/24 brd 192.168.3.255 scope global eth0 inet 192.168.3.63/32 scope global eth0 #查看redis-m的redis状态信息 [root@redis-m ~]# redis-cli -h 192.168.3.61 info Replication # Replication role:master connected_slaves:1 #从这里我们能看到现在redis-s吃slave的状态 slave0:ip=192.168.3.62,port=6379,state=online,offset=170,lag=1 master_repl_offset:170 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:17 repl_backlog_histlen:154 #查看redis-s的redis状态信息 [root@redis-m ~]# redis-cli -h 192.168.3.62 info Replication # Replication role:slave master_host:192.168.3.61 master_port:6379 master_link_status:up master_last_io_seconds_ago:2 master_sync_in_progress:0 slave_repl_offset:254 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:28 #连接到vip上查看之前的数据k3,k4是否还存在 [root@redis-m ~]# redis-cli -h 192.168.3.63 get k3 "v3" [root@redis-m ~]# redis-cli -h 192.168.3.63 get k4 "v4" #添加一个新的数据k5:v5 [root@redis-m ~]# redis-cli -h 192.168.3.63 set k5 v5 OK [root@redis-m ~]# redis-cli -h 192.168.3.63 get k5 "v5" #连接到redis-m:192.168.3.61上查看结果 [root@redis-m ~]# redis-cli -h 192.168.3.61 get k5 "v5" #连接到redis-s:192.168.3.62上查看结果 [root@redis-m ~]# redis-cli -h 192.168.3.62 get k5 "v5"
到此redis+keepalived的高可用切换配置成功。
本文出自 “ly36843运维” 博客,请务必保留此出处http://ly36843.blog.51cto.com/3120113/1679522
原文地址:http://ly36843.blog.51cto.com/3120113/1679522