轉載請註明出處:http://blog.csdn.net/l1028386804/article/details/52578080
之前,給大家介紹了一些關於Redis的文章,大家可以參見博文中有關Redis的文章。今天,我們就一起來學習如何搭建高可用及負載平衡的Redis,好了,不多說了,我們直接進入正題吧。 一、測試環境 1、機器
母機:centos6.5-64
虛擬機器:centos6.5-64 單核 1G 獨立ip 3個
虛擬機器使用VMWare,centos為64位6.5.具體資訊如下:
192.168.0.231 Redis-master192.168.0.231 Redis-slave192.168.0.239 Virtual IP
2、軟體
Reidis
Keepalived
二、安裝前配置 1、虛擬機器迷你安裝
虛擬機器迷你安裝,更新系統及安裝必須組件
$ yum install -y openssl openssl-devel kernel kernel-devel gcc wget tcl libnl-devel$ yum upgrade$ reboot //重啟系統
2、安裝Keepalived
安裝keepalived 下載地址:http://www.keepalived.org/software/keepalived-1.2.13.tar.gz 安裝
$ wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz$ tar -zxvf keepalived-1.2.13.tar.gz$ cd keepalived-1.2.13$ ln -s /usr/src/kernels/`uname -r`-`uname -m`/ /usr/src/linux$ ./configure --prefix=/usr/local/keepalived --sysconf=/etc$ chkconfig --add keepalived//查看是否成功$ service keepalived status
3、安裝redis
下載Redis,目前Redis的穩定版本是2.8.17,下載地址Redis-2.8.17
$ wget http://download.redis.io/releases/redis-2.8.17.tar.gz$ tar -zxvf redis-2.8.17.tar.gz$ cd redis-2.8.17$ make && make test$ mkdir -p /usr/local/redis/{bin,var,etc}$ cp ./src/{redis-benchmark,redis-cli,redis-server,redis-check-dump,redis-check-aof} /usr/local/redis/bin/$ cp /usr/local/redis/bin/* /usr/local/bin/
三、配置 1、Master配置
先配置好Redis,主從複製成功之後再行配置Keepalived。
Master-Redis配置
$ vim /usr/local/redis/etc/redis.conf
修改如下值
daemonize yespidfile /var/run/redis.pidport 6379tcp-backlog 511timeout 0tcp-keepalive 0loglevel noticelogfile "/usr/local/redis/log/redis.log"databases 16save 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbdir /usr/local/redis/dataslave-serve-stale-data yesslave-read-only norepl-disable-tcp-nodelay noslave-priority 100appendonly yesappendfilename "appendonly.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mblua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-entries 512list-max-ziplist-value 64set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10aof-rewrite-incremental-fsync yes
Master-Keepalived配置
global_defs { router_id redis}vrrp_script chk_redis { script "/etc/keepalived/scripts/redis_check.sh" ###監控指令碼 interval 2 ###監控時間 timeout 2 ###逾時時間 fall 3 ###重試次數}vrrp_instance redis { state MASTER ###設定為MASTER interface eth0 ###監控網卡 virtual_router_id 52 priority 101 ###權重值 authentication { auth_type PASS ###加密 auth_pass redis ###密碼 } track_script { chk_redis ###執行上面定義的chk_redis } virtual_ipaddress { 192.168.0.239 ###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/redsi_stop.sh}
也可在本地編輯好後使用 scp上傳到伺服器。 Master-keepalived指令碼redis_master.sh:
#!/bin/bash REDISCLI="/usr/local/redis/bin/redis-cli" LOGFILE="/usr/local/redis/var/keepalived-redis-state.log" echo "[master]" >> $LOGFILE date >> $LOGFILE echo "Being master...." >> $LOGFILE 2>&1 echo "Run SLAVEOF cmd ..." >> $LOGFILE $REDISCLI SLAVEOF 192.168.0.232 6379 >> $LOGFILE 2>&1 sleep 10 #延遲10秒以後待資料同步完成後再取消同步狀態 echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
Master-keepalived指令碼redis_backup.sh:
#!/bin/bash REDISCLI="/usr/local/redis/bin/redis-cli" LOGFILE="/usr/local/redis/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.0.232 6379 >> $LOGFILE 2>&1
2、Backup配置
同樣先配置好Redis,主從複製成功之後再行配置Keepalived。
Backup-Redis配置
$ vim /usr/local/redis/etc/redis.conf
修改如下值
daemonize yespidfile /var/run/redis.pidport 6379tcp-backlog 511timeout 0tcp-keepalive 0loglevel noticelogfile "/usr/local/redis/log/redis.log"databases 16save 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbdir /usr/local/redis/dataslave-serve-stale-data yesslave-read-only norepl-disable-tcp-nodelay noslave-priority 100appendonly yesappendfilename "appendonly.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mblua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-entries 512list-max-ziplist-value 64set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10aof-rewrite-incremental-fsync yesSLAVEOF 192.168.0.231 6379
Backup-Keepalived配置
global_defs { router_id redis}vrrp_script chk_redis { script "/etc/keepalived/scripts/redis_check.sh" ###監控指令碼 interval 2 ###監控時間 timeout 2 ###逾時時間 fall 3 ###重試次數}vrrp_instance redis { state BACKUP ###設定為MASTER interface eth0 ###監控網卡 virtual_router_id 51 priority 100 ###權重值 authentication { auth_type PASS ###加密 auth_pass redis ###密碼 } track_script { chk_redis ###執行上面定義的chk_redis } virtual_ipaddress { 192.168.0.239 ###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/redsi_stop.sh}
也可在本地編輯好後使用 scp上傳到伺服器。 Master-keepalived指令碼redis_master.sh:
#!/bin/bash REDISCLI="/usr/local/redis/bin/redis-cli" LOGFILE="/usr/local/redis/var/keepalived-redis-state.log" echo "[master]" >> $LOGFILE date >> $LOGFILE echo "Being master...." >> $LOGFILE 2>&1 echo "Run SLAVEOF cmd ..." >> $LOGFILE $REDISCLI SLAVEOF 192.168.0.231 6379 >> $LOGFILE 2>&1 sleep 10 #延遲10秒以後待資料同步完成後再取消同步狀態 echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
Master-keepalived指令碼redis_backup.sh:
#!/bin/bash REDISCLI="/usr/local/redis/bin/redis-cli" LOGFILE="/usr/local/redis/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.0.231 6379 >> $LOGFILE 2>&1
四、公用指令碼配置 1、redis_chekc.sh
可以使用netcat來檢測redis服務的狀態.
安裝nc,使用nc檢測redis的服務而不是ping
$ wget http://sourceforge.net/projects/netcat/files/netcat/0.7.1/netcat-0.7.1.tar.gz$ tar -zxvf netcat-0.7.1.tar.gz$ ./configure$ make && make install
建立指令碼:
$ vim /etc/keepalived/scripts/redis_check.sh
指令碼內容:
#!/bin/bashALIVE=`/usr/local/redis/bin/redis-cli PING`if ["$ALIVE" == "PONG"] ;then echo $ALIVE exit 0else echo $ALIVE exit 1fi
2、redis_fault.sh
建立指令碼:
$ vim /etc/keepalived/scripts/redis_fault.sh
指令碼內容:
# !/bin/bashLOGFILE=/usr/local/redis/var/keepalived-redis-state.log echo "[faule]" >> $LOGFILE date >> $LOGFILE
3、redis_stop.sh
建立指令碼:
$ vim /etc/keepalived/scripts/redis_stop.sh
指令碼內容:
# !/bin/bashLOGFILE=/usr/local/redis/var/keepalived-redis-state.log echo "[stop]" >> $LOGFILE date >> $LOGFILE
五、測試 1、主從複製測試
先開啟Master上的Redis,再開啟Backup上的Redis,分別查看info
$ redis-cli info
Master的rule顯示master為正常,slave會顯示為192.168.0.232
在Master上寫入資料,在Backup讀取
$ redis-cli set a hello //Master 寫入資料$ redis-cli get a //Backup 讀取資料"hello"
主從複製正常。
2、主從切換測試
先後啟動Master和Backup上的keepalived,在Master上查看vip
$ ip a
從圖中可以看出vip已經綁定在Master上,現在通過vip來進行資料測試
$ redis-cli -h 192.168.0.239 set a test$ redis-cli -h 192.168.0.239 get a //測試虛擬ip資料讀取//在主機和從機上分別進行資料讀取$ redis-cli get a
從圖中可以看出測試無問題。
現在關閉Master上的redis,來看VIP能否正常漂移,並進行資料測試。
$ killall -9 redis-server$ redis-cli -h 192.168.0.232 info
現在給redis寫入資料,測試看Master恢複服務後能否正常恢複資料
$ redis-cli -h 192.168.0.239 set a nihao
恢複Master上的Redis,查看ip a
$ service redis-server start$ ip a$ redis-cli get a
資料恢複,VIP切換回Master,Redis高可用環境搭建成功.
至此,已成功搭建高可用及負載平衡的Redis環境。