zoukankan      html  css  js  c++  java
  • keepalive配置文件详解

      

    第一部分:全局定义块
      1、email通知。作用:有故障,发邮件报警。
      2、Lvs负载均衡器标识(lvs_id)。在一个网络内,它应该是唯一的。
      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实例

    vrrp_sync_group VG_1{ #监控多个网段的实例
    group {
        VI_1 #实例名
        VI_2
        ...... } notify_master /path/xx.sh #指定当切换到master时,执行的脚本 netify_backup /path/xx.sh #指定当切换到backup时,执行的脚本 notify_fault "path/xx.sh VG_1" #故障时执行的脚本 notify /path/xx.sh smtp_alert #使用global_defs中提供的邮件地址和smtp服务器发送邮件通知 }


    第三部分:vrrp_instance,实例名出自实例组group所包含的那些名字。  

    vrrp_instance VI_1 { state BACKUP #指定那个为master,那个为backup,如果设置了nopreempt这个值不起作用,主备考priority决 定 interface eth0 #设置实例绑定的网卡 dont_track_primary #忽略vrrp的interface错误(默认不设置) track_interface{ #设置额外的监控,里面那个网卡出现问题都会切换 eth0 eth1 } mcast_src_ip #发送多播包的地址,如果不设置默认使用绑定网卡的primary ip garp_master_delay #在切换到master状态后,延迟进行gratuitous ARP请求 virtual_router_id 50 #VPID标记 priority 99 #优先级,高优先级竞选为master advert_int 1 #检查间隔,默认1秒 nopreempt #设置为不抢占 注:这个配置只能设置在backup主机上,而且这个主机优先级要比另外一台高 preempt_delay #抢占延时,默认5分钟 debug #debug级别 authentication { #设置认证 auth_type PASS #认证方式 auth_pass 111111 #认证密码 } virtual_ipaddress { #设置vip 192.168.202.200 } }

    第四部分:

    虚拟服务器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指定日志文件

    默认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 ~]#
  • 相关阅读:
    Leetcode 92. Reverse Linked List II
    Leetcode 206. Reverse Linked List
    Leetcode 763. Partition Labels
    Leetcode 746. Min Cost Climbing Stairs
    Leetcode 759. Employee Free Time
    Leetcode 763. Partition Labels
    搭建数据仓库第09篇:物理建模
    Python进阶篇:Socket多线程
    Python进阶篇:文件系统的操作
    搭建数据仓库第08篇:逻辑建模–5–维度建模核心之一致性维度2
  • 原文地址:https://www.cnblogs.com/hftian/p/9453536.html
Copyright © 2011-2022 走看看