资源描述
基于keepalived的redis系统master双机热备,读数据负载均衡设置方案
==========================================================================================
硬件:
机器 ip 作用
master 192.168.0.2 redis系统的master主机
slave1 192.168.0.3 redis系统的slave机器,和master组成双机热备
slave2 192.168.0.4
redis系统的slave机器,和slave1构成读数据的负载均衡系统
软件:
keepalived,下载地址:www.keepalived.org
lvs,下载地址:http://www.linuxvirtualserver.org
redis,下载地址:www.redis.io
centos 6.4版本
安装:
不采用编译安装的方式,使用yum安装
1.安装kernel-devel:yum install kernel-devel.这是ipvsadm需要的
2.安装lvs:yum install ipvsadm
3.安装keepalived:yum install keepalived
如果无法联网,请去这里考古寻找自己需要的rpm文件
base:
update:
下面是需要的rpm文件列表截图
kernel-devel需要的文件
以及需要的证书
lvs需要的文件
keepalived需要的文件
注意,三台机器都需要安装keepalived
配置keepalived:
1.centos必要配置:
/etc/sysctl.conf文件
net.ipv4.ip_forward=1#转发开启
2.keepalived必要配置:
master修改/etc/keepalived/keepalived.conf为如下
[plain] view plaincopy
1. ! Configuration File for keepalived
2.
3. global_defs {
4. notification_email {
5. 邮箱
6. }
7. notification_email_from 邮箱
8. smtp_server 邮箱服务器地址
9. smtp_connect_timeout 30
10. router_id LVS_DEVEL
11. }
12. vrrp_instance VI_1 {
13. state MASTER
14. interface eth0 #eth0是要绑定的网卡
15. virtual_router_id 100 #同一个vrrp_instance中的值必须一样
16. priority 160 #master的值要高于backup的
17. advert_int 1
18. authentication {
19. auth_type PASS
20. auth_pass 1111
21. }
22. virtual_ipaddress {
23. 192.168.0.5 #用于双机热备的虚拟ip
24. }
25.
26. }
27. virtual_server 192.168.0.5 6379 {
28. delay_loop 3
29. lb_algo wrr
30. lb_kind DR
31. persistence_timeout 30
32. protocol TCP
33. real_server 192.168.0.2 6379 {
34. weight 8
35. notify_down redis服务失败后要执行的脚本的路径/脚本名 #服务失败后要执行的脚本
36. TCP_CHECK {
37. connect_timeout 1
38. nb_get_retry 3
39. delay_before_retry 3
40. connect_port 6379
41. }
42. }
43. }
slave1修改/etc/keepalived/keepalived.conf为如下
[plain] view plaincopy
1. ! Configuration File for keepalived
2.
3. global_defs {
4. notification_email {
5. 邮箱
6. }
7. notification_email_from 邮箱
8. smtp_server 邮箱服务器地址
9. smtp_connect_timeout 30
10. router_id LVS_DEVEL
11. }
12.
13. vrrp_instance VI_1 {
14. state MASTER
15. interface eth0 #eth0是要绑定的网卡
16. virtual_router_id 100 #同一个vrrp_instance中的值必须一样
17. priority 160 #master的值要高于backup的
18. advert_int 1
19. authentication {
20. auth_type PASS
21. auth_pass 1111
22. }
23. virtual_ipaddress {
24. 192.168.0.5 #用于双机热备的虚拟ip
25. }
26. notify_master 将slave重新转换为slave的脚本
27. }
28. vrrp_instance VI_2 {
29. state MASTER #将此slave作为读数据的master
30. interface eth0
31. virtual_router_id 101
32. priority 151
33. advert_int 1
34. authentication {
35. auth_type PASS
36. auth_pass 1111
37. }
38. virtual_ipaddress {
39. 192.168.0.6 #用于读取数据的负载均衡的虚拟ip
40. }
41. }
42. virtual_server 192.168.0.5 6379 {
43. delay_loop 3
44. lb_algo wrr
45. lb_kind DR
46. persistence_timeout 30
47. protocol TCP
48. real_server 192.168.0.2 6379 {
49. weight 1
50. notify_down redis服务失败后要执行的脚本的路径/脚本名 #服务失败后要执行的脚本
51. TCP_CHECK {
52. connect_timeout 1
53. nb_get_retry 2
54. delay_before_retry 1
55. connect_port 6379
56. }
57. }
58. real_server 192.168.0.3 6379 {
59. weight 8
60. TCP_CHECK {
61. connect_timeout 10
62. nb_get_retry 3
63. delay_before_retry 3
64. connect_port 6379
65. }
66. }
67. }
68. virtual_server 192.168.0.6 6379 {
69. delay_loop 3
70. lb_algo wrr
71. lb_kind DR
72. persistence_timeout 30
73. protocol TCP
74. real_server 192.168.0.2 6379 {
75. weight 8
76. TCP_CHECK {
77. connect_timeout 10
78. nb_get_retry 3
79. delay_before_retry 3
80. connect_port 6379
81. }
82. }
83. real_server 192.168.0.3 6379 {
84. weight 7
85. TCP_CHECK {
86. connect_timeout 10
87. nb_get_retry 3
88. delay_before_retry 3
89. connect_port 6379
90. }
91. }
92. }
slave2修改/etc/keepalived/keepalived.conf为如下
[plain] view plaincopy
1. ! Configuration File for keepalived
2.
3. global_defs {
4. notification_email {
5. 邮箱
6. }
7. notification_email_from 邮箱
8. smtp_server 邮箱服务器地址
9. smtp_connect_timeout 30
10. router_id LVS_DEVEL
11. }
12. vrrp_instance VI_2 {
13. state BACKUP
14. interface eth0
15. virtual_router_id 101
16. priority 149
17. advert_int 1
18. authentication {
19. auth_type PASS
20. auth_pass 1111
21. }
22. virtual_ipaddress {
23. 192.168.0.6
24. }
25. }
26. virtual_server 192.168.0.6 6379 {
27. delay_loop 3
28. lb_algo wrr
29. lb_kind DR
30. persistence_timeout 30
31. protocol TCP
32. real_server 192.168.0.2 6379 {
33. weight 8
34. TCP_CHECK {
35. connect_timeout 10
36. nb_get_retry 3
37. delay_before_retry 3
38. connect_port 6379
39. }
40. }
41. real_server 192.168.0.3 6379 {
42. weight 7
43. TCP_CHECK {
44. connect_timeout 10
45. nb_get_retry 3
46. delay_before_retry 3
47. connect_port 6379
48. }
49. }
50. }
配置redis:
master无需特殊配置 slave1则设置为master的从机
slave2则需要设置为192.168.0.5的从机,否则在master失效后slave2会无法继续读取数据
需要的脚本:
在master执行的脚本:
[plain] view plaincopy
1. #!/usr/bin/env bash
2. ervice keepalived stop #需要用户具有权限,不中断keepalived服务虚拟ip无法转移
在slave1执行的脚本:
[plain] view plaincopy
1. #!/usr/bin/env bash
2.
3. /usr/local/bin/redis-cli -h 127.0.0.1 -p 6379 slaveof NO ONE #将slave1转换为redis的
slave1第二个脚本,在master服务重启后将slave1重新转换为slave状态
最终效果: 192.168.0.5 提供了redis的双机热备服务,192.168.0.6则提供了数据读取的负载均衡
[plain] view plaincopy
1. #!/usr/bin/env bash
2.
3. /usr/local/bin/redis-cli slaveof 192.168.0.2 6379 #将slave1重新转换为redis的slave
需要注意,master每次需要先启动redis服务然后再启动keepalived
==========================================================================================
基于keepalived、redis sentinel的高可用redis集群【修改版】
2013年12月12日 ⁄ 综合 ⁄ 共 5289字 ⁄ 字号 小 中 大 ⁄ 评论关闭
原方案地址原方案
硬件
机器名
IP
作用
master
192.168.0.2
redis的master服务器
slave1
192.168.0.3
redis的slave服务器
slave2
192.168.0.4
redis的slave服务器
route1
192.168.0.5【虚拟IP:192.168.0.7】
keepalived和redis sentinel服务器,承载写redis的VIP【虚拟ip】,做写的双机热备的主master指定
route2
192.168.0.6【虚拟IP:192.168.0.8】
keepalived和redis sentinel服务器,承载读redis的VIP,做读的负载均衡和写的双机热备的master备份路由指定
详细的keepalived配置,route1
! Configuration File for keepalived
global_defs {
notification_email {
邮箱
}
notification_email_from 邮箱@
smtp_server 邮箱服务器地址
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 100
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.7
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth1
virtual_router_id 101
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.8
}
}
virtual_server 192.168.0.7 6379 {
delay_loop 3
lb_algo rr
lb_kind DR
persistence_timeout 15
protocol TCP
real_server 192.168.0.4 6379 {
weight 4
notify_up /home/wind/redis_up.sh
MISC_CHECK {
misc_path "/home/wind/redischeck.py 192.168.0.4 6379"
misc_timeout 5
}
}
real_server 192.168.0.2 6379 {
weight 3
notify_up /home/wind/redis_up.sh
MISC_CHECK {
misc_path "/home/wind/redischeck.py 192.168.0.2 6379"
misc_timeout 5
}
}
real_server 192.168.0.3 6379 {
weight 3
notify_up /home/wind/redis_up.sh
MISC_CHECK {
misc_path "/home/wind/redischeck.py 192.168.0.2 6379"
misc_timeout 5
}
}
}
virtual_server 192.168.0.8 6379 {
delay_loop 3
lb_algo wrr
lb_kind DR
persistence_timeout 30
protocol TCP
real_server 192.168.0.2 6379 {
weight 8
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 6379
}
}
real_server 192.168.0.3 6379 {
weight 2
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 6379
}
}
real_server 192.168.0.4 6379 {
weight 2
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 6379
}
}
}
route2的配置文件
! Configuration File for keepalived
global_defs {
notification_email {
# xieqj@
#shanghq@
}
notification_email_from xieqj@
smtp_server
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 100
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.87.89
}
#notify_master "/home/wind/redis.sh"
}
vrrp_instance VI_2 {
state MASTER
interface eth1
virtual_router_id 101
priority 151
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.87.90
}
}
virtual_server 192.168.87.89 6379 {
delay_loop 3
lb_algo rr
lb_kind DR
persistence_timeout 15
protocol TCP
real_server 192.168.87.103 6379 {
weight 4
notify_up /home/wind/redis_up.sh
MISC_CHECK {
misc_path "/home/wind/redischeck.py 192.168.87.103 6379"
misc_timeout 5
}
}
real_server 192.168.87.104 6379 {
weight 4
notify_up /home/wind/redis_up.sh
MISC_CHECK {
misc_path "/home/wind/redischeck.py 192.168.87.103 6379"
misc_timeout 5
}
}
real_server 192.168.87.105 6379 {
weight 4
notify_up /home/wind/redis_up.sh
MISC_CHECK {
misc_path "/home/wind/redischeck.py 192.168.87.103 6379"
misc_timeout 5
}
}
}
virtual_server 192.168.87.90 6379 {
delay_loop 3
lb_algo wrr
lb_kind DR
persistence_timeout 30
protocol TCP
real_server 192.168.87.104 6379 {
weight 8
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 6379
}
}
real_server 192.168.87.105 6379 {
weight 2
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 6379
}
}
}
keepalived的master的MISCH_CHECK监测脚本
#!/usr/bin/python
import sys,commands
cmd="/usr/local/bin/redis-cli -h "+sys.argv[1]+" -p "+sys.argv[2]+" info" #sys.argv是输入的参数,sys.argv[0]是需要执行的命令,以后才是参数。1是ip,2是端口
str=commands.getoutput(cmd)
ismaster=-100
ismaster=str.count("role:master")#原来是使用index方法,但是找不到字符串时会报错
zero=0
if ismaster>zero:
sys.exit(0) #返回0在keepalived表示健康
else:
sys.exit(1) #返回1表示keepalived检测端口不健康
更多MISC_CHECK参考见
redis维护脚本redis_up.sh用在检测到服务启动时
#!/usr/bin/env bash
/usr/local/bin/redis-cli -h $argv[1] -p 6379 config set appendonly no
redis设置与sentinel设置无变化
需要特别注意的配置,如果没有这个配置就会发生keepalived不转发的的问题,而且是如果redis和keepalive是同一台机器,会转发,但是如果相互间独立,则realserver收不到转发包
配置master
vim /etc/sysctl.conf,添加内容如下
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
执行命令
sysctl -p
ip addr add 192.168.0.7/32 dev lo
ip addr add 192.168.0.8/32 dev lo
ip add list
lo环上出现了指定的ip即可
配置slave1
vim /etc/sysctl.conf,添加内容如下
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
执行命令
sysctl -p
ip addr add 192.168.0.7/32 dev lo
ip addr add 192.168.0.8/32 dev lo
ip add list
配置slave2
vim /etc/sysctl.conf,添加内容如下
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
执行命令
sysctl -p
ip addr add 192.168.0.7/32 dev lo
ip addr add 192.168.0.8/32 dev lo
ip add list
==========================================================================================
硬件
机器名
IP
作用
master
192.168.0.2
redis的master服务器
slave1
192.168.0.3
redis的slave服务器
slave2
192.168.0.4
redis的slave服务器
route1
192.168.0.5【虚拟IP:192.168.0.7】
keepalived和redis sentinel服务器,承载写redis的VIP【虚拟ip】,做写的双机热备的主master指定
route2
192.168.0.6【虚拟IP:192.168.0.8】
keepalived和redis sentinel服务器,承载读redis的VIP,做读的负载均衡和写的双机热备的master备份路由指定
安装与配置见此文安装与配置
详细的keepalived配置,route1
[plain] view plaincopy
1. ! Configuration File for keepalived
2.
3. global_defs {
4. notification_email {
5. 邮箱
6. }
7. notification_email_from 邮箱
8. smtp_server 邮箱服务器地址
9. smtp_connect_timeout 30
10. router_id LVS_DEVEL
11. }
12. vrrp_instance VI_1 {
13. state MASTER
14. interface eth0 #eth0是要绑定的网卡
15. virtual_router_id 100 #同一个vrrp_instance中的值必须一样
16. priority 160 #master的值要高于backup的
17. advert_int 1
18. authentication {
19. auth_type PASS
20. auth_pass 1111
21. }
22. virtual_ipaddress {
23. 192.168.0.7 #用于双机热备的虚拟ip
24. }
25.
26. }
27. virtual_server 192.168.0.7 6379 {
28. delay_loop 3
29. lb_algo wrr
30. lb_kind DR
31. persistence_timeout 30
32. protocol TCP
33. real_server 192.168.0.2 6379 {
34. weight 8
35. notify_down redis服务失败后要执行的脚本的路径/脚本名 #服务失败后要执行的脚本
36. TCP_CHECK {
37.
展开阅读全文