3、花括号“{}”。用来分隔定义块,因此必须成对出现。如果写漏了,keepalived运行时,不会得到预期的结果。由于定义块内存在嵌套关系,因此很容易遗漏结尾处的花括号,这点要特别注意。
global_defs { notification_email { #指定keepalived在发生切换时需要发送email到的对象,一行一个 sysadmin@fire.loc } notification_email_from Alexandre.Cassen@firewall.loc #指定发件人 smtp_server localhost #指定smtp服务器地址 smtp_connect_timeout 30 #指定smtp连接超时时间 router_id LVS_DEVEL #运行keepalived机器的一个标识 }
第二部分:vrrp_sync_group作用:确定失败切换(FailOver)包含的路由实例个数。即在有2个负载均衡器的场景,一旦某个负载均衡器失效,需要自动切换到另外一个负载均衡器的实例是哪些? 实例组group{}至少包含一个vrrp实例
虚拟服务器virtual_server定义块 ,虚拟服务器定义是keepalived框架最重要的项目了,是keepalived.conf必不可少的部分。 该部分是用来管理LVS的,是实现keepalive和LVS相结合的模块。ipvsadm命令可以实现的管理在这里都可以通过参数配置实现,注意:real_server是被包含在viyual_server模块中的,是子模块。
virtual_server 192.168.202.200 23 { //VIP地址,要和vrrp_instance模块中的virtual_ipaddress地址一致
delay_loop 6 #健康检查时间间隔 lb_algo rr #lvs调度算法rr|wrr|lc|wlc|lblc|sh|dh lb_kind DR #负载均衡转发规则NAT|DR|RUN persistence_timeout 5 #会话保持时间 protocol TCP #使用的协议 persistence_granularity <NETMASK> #lvs会话保持粒度 virtualhost <string> #检查的web服务器的虚拟主机(host:头) sorry_server<IPADDR> <port> #备用机,所有realserver失效后启用 real_server 192.168.200.5 23 { //RS的真实IP地址 weight 1 #默认为1,0为失效 inhibit_on_failure #在服务器健康检查失效时,将其设为0,而不是直接从ipvs中删除 notify_up <string> | <quoted-string> #在检测到server up后执行脚本 notify_down <string> | <quoted-string> #在检测到server down后执行脚本 TCP_CHECK { //常用 connect_timeout 3 #连接超时时间 nb_get_retry 3 #重连次数 delay_before_retry 3 #重连间隔时间 connect_port 23 健康检查的端口的端口 bindto <ip> } HTTP_GET | SSL_GET{ //不常用 url{ #检查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> #默认检查25端口 bindto <IP ADDRESS> } connect_timeout 5 retry 3 delay_before_retry 2 helo_name <string> | <quoted-string> #smtp helo请求命令参数,可选 } MISC_CHECK{ //不常用 misc_path <string> | <quoted-string> #外部脚本路径 misc_timeout #脚本执行超时时间 misc_dynamic #如设置该项,则退出状态码会用来动态调整服务器的权重,返回0 正常,不修改;返回1, 检查失败,权重改为0;返回2-255,正常,权重设置为:返回状态码-2 } }
[root@LB2 ~]# 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 } } } [root@LB2 ~]#
默认keepalive的日志文件是
/var/log/messages
[root@LB2 ~]# tail -5 /var/log/messages Dec 7 22:40:10 LB2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE Dec 7 22:40:10 LB2 Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(10,11)] Dec 7 22:40:10 LB2 Keepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'. Dec 7 22:40:10 LB2 Keepalived_healthcheckers: Configuration is using : 8425 Bytes Dec 7 22:40:10 LB2 Keepalived_healthcheckers: Using LinkWatch kernel netlink reflector... [root@LB2 ~]#
修改配置
[root@LB2 ~]# vim /etc/sysconfig/keepalived ...... #KEEPALIVED_OPTIONS="-D" //注释掉该行 KEEPALIVED_OPTIONS="-D -d -S 0" //添加改行 ...... [root@LB2 ~]# vim /etc/rsyslog.conf ...... local0.* /var/log/keepalived.log //添加改行 [root@LB2 ~]# /etc/init.d/rsyslog restart //重启日志服务 Shutting down system logger: [ OK ] Starting system logger: [ OK ] [root@LB2 ~]# /etc/init.d/keepalived restart //重启keepalive服务 Stopping keepalived: [ OK ] Starting keepalived: [ OK ] [root@LB2 ~]# tail -5 /var/log/keepalived.log Dec 8 01:02:06 LB2 Keepalived_vrrp: Virtual IP = 1 Dec 8 01:02:06 LB2 Keepalived_vrrp: 192.168.220.110/24 brd 192.168.220.110 dev eth0 scope global Dec 8 01:02:06 LB2 Keepalived_vrrp: Using LinkWatch kernel netlink reflector... Dec 8 01:02:06 LB2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE Dec 8 01:02:06 LB2 Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(10,11)] [root@LB2 ~]#