一、配置文件说明
第一部分:全局定义块
1、email通知。作用:有故障,发邮件报警。
2、Lvs负载均衡器标识(lvs_id)。在一个网络内,它应该是唯一的。
3、花括号“{}”。用来分隔定义块,因此必须成对出现。如果写漏了,keepalived运行时,不会得到预期的结果。由于定义块内存在嵌套关系,因此很容易遗漏结尾处的花括号,这点要特别注意。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| global_defs {#global_defs区域,主要是配置故障发生时的通知对象以及机器标识 notification_email { # 故障发生时给谁发邮件通知。 acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc # 指定发件人 smtp_server 192.168.200.1 #通知邮件的smtp地址。 smtp_connect_timeout 30 #连接smtp服务器的超时时间。 router_id LVS_DEVEL #运行keepalived机器的一个标识 vrrp_skip_check_adv_addr # vrrp_strict #意为严格遵循 vrrp 协议,我们平时使用需要将其注释。 vrrp_garp_interval 0 vrrp_gna_interval 0 }
|
vrrp_strict如果不注释需要开启防火墙
第二部分:vrrp_sync_group
确定失败切换(FailOver)包含的路由实例个数。即在有2个负载均衡器的场景,一旦某个负载均衡器失效,需要自动切换到另外一个负载均衡器的实例是哪些? 实例组group{}至少包含一个vrrp实例
1 2 3 4 5 6 7 8 9 10 11 12
| vrrp_sync_group VG_1{ group { VI_1 VI_2 ...... } notify_master /path/xx.sh netify_backup /path/xx.sh notify_fault "path/xx.sh VG_1" notify /path/xx.sh smtp_alert }
|
第三部分:vrrp_instance
虚拟路由实例配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| vrrp_instance VI_1 { state BACKUP interface eth0 dont_track_primary track_interface{ eth0 eth1 } mcast_src_ip garp_master_delay virtual_router_id 50 priority 99 advert_int 1 nopreempt preempt_delay debug authentication { auth_type PASS auth_pass 111111 } virtual_ipaddress { 192.168.202.200 } }
|
第四部分:virtual_server
虚拟服务器virtual_server定义块 ,虚拟服务器定义是keepalived框架最重要的项目了,是keepalived.conf必不可少的部分。 该部分是用来管理LVS的,是实现keepalive和LVS相结合的模块。ipvsadm命令可以实现的管理在这里都可以通过参数配置实现,注意:real_server是被包含在viyual_server模块中的,是子模块。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| virtual_server 192.168.202.200 23 { //VIP地址,要和vrrp_instance模块中的 virtual_ipaddress地址一致 delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 5 protocol TCP persistence_granularity <NETMASK> virtualhost <string> sorry_server<IPADDR> <port>
real_server 192.168.200.5 23 { //RS的真实IP地址 weight 1 inhibit_on_failure notify_up <string> | <quoted-string> notify_down <string> | <quoted-string> TCP_CHECK { //常用 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 23 健康检查的端口的端口 bindto <ip> }
HTTP_GET | SSL_GET{ //不常用 url{ path / digest <string> status_code 200 } connect_port <port> bindto <IPADD> connect_timeout 5 nb_get_retry 3 delay_before_retry 2 }
SMTP_CHECK{ //不常用 host{ connect_ip <IP ADDRESS> connect_port <port> bindto <IP ADDRESS> } connect_timeout 5 retry 3 delay_before_retry 2 helo_name <string> | <quoted-string> } MISC_CHECK{ //不常用 misc_path <string> | <quoted-string> misc_timeout misc_dynamic
检查失败,权重改为0;返回2-255,正常,权重设置为:返回状态码-2 } } }
|
第五部分:vrrp_script
服务监控脚本。keepalived只能做到对网络故障和keepalived本身的监控,即当出现网络故障或者keepalived本身出现问题时,进行切换。但是这些还不够,我们还需要监控keepalived所在服务器上的其他业务进程,比如说nginx,keepalived+nginx实现nginx的负载均衡高可用。
1 2 3 4 5 6 7 8 9 10
| vrrp_script check_haproxy{
script "/home/check.sh" interval 3 weight -20
}
|
生产环境配置文件实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| # cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived
global_defs { notification_email { 49000448@qq.com } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 10.0.0.1 smtp_connect_timeout 30 router_id LVS_2 }
vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 55 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.220.110/24 }
# 这东西还没发现实用,有时有效,有时就连不上了 virtual_server 192.168.220.110 80 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 300 protocol TCP
real_server 192.168.220.129 80 { weight 1 TCP_CHECK { connect_timeout 8 nb_get_retry 3 delay_before_retry 3 connect_port 80 } }
real_server 192.168.220.138 80 { weight 1 TCP_CHECK { connect_timeout 8 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
|