zoukankan      html  css  js  c++  java
  • Keepalived 安装与配置

    下载:http://www.keepalived.org/

    what is keepalived?

    Keepalived is a routing software written in C. The main goal of this project is to provide simple and robust facilities for loadbalancing and high-availability to Linux system and Linux based infrastructures. Loadbalancing framework relies on well-known and widely used Linux Virtual Server (IPVS) kernel module providing Layer4 loadbalancing. Keepalived implements a set of checkers to dynamically and adaptively maintain and manage loadbalanced server pool according their health. On the other hand high-availability is achieved by VRRP protocol. VRRP is a fundamental brick for router failover. In addition, Keepalived implements a set of hooks to the VRRP finite state machine providing low-level and high-speed protocol interactions. Keepalived frameworks can be used independently or all together to provide resilient infrastructures.

    下载源代码

    ./configure --prefix=/opt/keepalived

    报错

    configure: error: Popt libraries is required

    安装popt开发包

    yum install popt-devel

    安装后configure输出信息

    Keepalived configuration
    ------------------------
    Keepalived version : 1.1.10
    Compiler : gcc
    Compiler flags : -g -O2
    Extra Lib : -lpopt -lssl -lcrypto
    Use IPVS Framework : No
    IPVS sync daemon support : No
    Use VRRP Framework : Yes
    Use LinkWatch : No
    Use Debug flags : No

    我上面的IPVS都是no,为no不支持LVS.

    看网上的说法

    1. Use IPVS Framework       : Yes  #使用ipvs框架,如果是做keepalive+ha可不要
    2. IPVS sync daemon support : Yes  #对ipvs同步的支持
    3. Use VRRP Framework       : Yes  #keepalive的核心,必须有!

    看网上说法要想IPVS为yes,要带上

    --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686/  

    1. #ln -s /usr/src/kernels/2.6.18-53.el5PAE-i686/  /usr/src/linux //生成一个链接文件  
    2.   
    3. 如果/usr/src/kernels没有2.6.18-194.el5,则需要编译  
    4.   
    5. #yum -y install kernel-devel  
    6. #yum -y update kernel  
    7. #cp /usr/src/kernels/2.6.18-274.el5/include/net/ip_vs.h        /usr/inclide/net/  //这个很重要,否则在安装ipvsadm的时候会提示缺少*.h文件

    我暂时没有支持IPVS.

    1. 安装完成之后,方了方便起见,请执行以下操作:
    2. 1、将安装目录里的/usr/local/keepalive/etc/rc.d/init.d/keepalived 复制到/etc/init.d (这里的安装目录改成自己的)
    3. 2、将安装目录里的配置文件/usr/local/keepalive/etc/keepalived/keepalived.conf 复制到/etc/keepalived/里。

     注意一点:启动时不能文件用绝对路径,不能用相对路径

    keepalived默认配置文件

    ! Configuration File for keepalived
    
    global_defs {
       notification_email { 通知email 
         acassen@firewall.loc
         failover@firewall.loc
         sysadmin@firewall.loc
       }
       notification_email_from Alexandre.Cassen@firewall.loc
       smtp_server 192.168.200.1
       smtp_connect_timeout 30
       router_id LVS_DEVEL   #节点名标识,主要用于通知中
    }
    
    vrrp_instance VI_1 {
      state MASTER          #配置为主服务器
    
    
      interface eth0        #通讯网卡
    
    
      virtual_router_id 100 #路由标识
    
    
      priority 200          #优先级,0-254
    
    
      advert_int 5          #通知间隔,实际部署时可以设置小一点,减少延时
    
    
       
    
    
      authentication {
    
    
        auth_type PASS
    
    
        auth_pass 123456    #验证密码,用于通讯主机间验证
    
    
      }
    
        virtual_ipaddress {
            192.168.200.16
            192.168.200.17
            192.168.200.18
        }
    }
    
    virtual_server 192.168.200.100 443 {
        delay_loop 6
        lb_algo rr
        lb_kind NAT
        nat_mask 255.255.255.0
        persistence_timeout 50
        protocol TCP
    
        real_server 192.168.201.100 443 {
            weight 1
            SSL_GET {
                url {
                  path /
                  digest ff20ad2481f97b1754ef3e12ecd3a9cc
                }
                url {
                  path /mrtg/
                  digest 9b3a0c85a887a256d6939da88aabd8cd
                }
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }
        }
    }
    
    virtual_server 10.10.10.2 1358 {
        delay_loop 6
        lb_algo rr 
        lb_kind NAT
        persistence_timeout 50
        protocol TCP
    
        sorry_server 192.168.200.200 1358
    
        real_server 192.168.200.2 1358 {
            weight 1
            HTTP_GET {
                url { 
                  path /testurl/test.jsp
                  digest 640205b7b0fc66c1ea91c463fac6334d
                }
                url { 
                  path /testurl2/test.jsp
                  digest 640205b7b0fc66c1ea91c463fac6334d
                }
                url { 
                  path /testurl3/test.jsp
                  digest 640205b7b0fc66c1ea91c463fac6334d
                }
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }
        }
    
        real_server 192.168.200.3 1358 {
            weight 1
            HTTP_GET {
                url { 
                  path /testurl/test.jsp
                  digest 640205b7b0fc66c1ea91c463fac6334c
                }
                url { 
                  path /testurl2/test.jsp
                  digest 640205b7b0fc66c1ea91c463fac6334c
                }
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }
        }
    }
    
    virtual_server 10.10.10.3 1358 {
        delay_loop 3
        lb_algo rr 
        lb_kind NAT
        nat_mask 255.255.255.0
        persistence_timeout 50
        protocol TCP
    
        real_server 192.168.200.4 1358 {
            weight 1
            HTTP_GET {
                url { 
                  path /testurl/test.jsp
                  digest 640205b7b0fc66c1ea91c463fac6334d
                }
                url { 
                  path /testurl2/test.jsp
                  digest 640205b7b0fc66c1ea91c463fac6334d
                }
                url { 
                  path /testurl3/test.jsp
                  digest 640205b7b0fc66c1ea91c463fac6334d
                }
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }
        }
    
        real_server 192.168.200.5 1358 {
            weight 1
            HTTP_GET {
                url { 
                  path /testurl/test.jsp
                  digest 640205b7b0fc66c1ea91c463fac6334d
                }
                url { 
                  path /testurl2/test.jsp
                  digest 640205b7b0fc66c1ea91c463fac6334d
                }
                url { 
                  path /testurl3/test.jsp
                  digest 640205b7b0fc66c1ea91c463fac6334d
                }
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }
        }
    }

    3. 配置Keepalived

    接下来就是配置了,很简单,直接上配置文件

    先是主服务器:

    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
    global_defs
    {
    notification_email    #通知email,根据实际情况配置
    {
    admin@example.com
    }
    notification_email_from admin@example.com
    smtp_server 127.0.0.1
    stmp_connect_timeout 30
    router_id node1         #节点名标识,主要用于通知中
    }
     
    vrrp_instance VI_NODE {
      state MASTER          #配置为主服务器
      interface eth0        #通讯网卡
      virtual_router_id 100 #路由标识
      priority 200          #优先级,0-254
      advert_int 5          #通知间隔,实际部署时可以设置小一点,减少延时
       
      authentication {
        auth_type PASS
        auth_pass 123456    #验证密码,用于通讯主机间验证
      }
     
      virtual_ipaddress {
        192.168.1.206       #虚拟ip,可以定义多个
      }
    }

    (我的启动master的日志

    keepalived  -f /opt/keepalived/etc/keepalived1.conf 切记,配置文件要用绝对路径,不能用相对路径,否则不正常。

    Keepalived_vrrp: Registering gratutious ARP shared channel Keepalived_vrrp: Configuration is using : 61723 Bytes Keepalived_vrrp: VRRP_Instance(VI_NODE) Transition to MASTER STATE

    我最开始用的是相对路径,用ip a没看到设置的虚拟ip.

    用来绝对路径后,启动正常了,ip a输出

    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
    valid_lft forever preferred_lft forever
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 1c:87:2c:60:14:26 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.50/24 brd 192.168.1.255 scope global eth0
    inet 192.168.1.206/32 scope global eth0 (我们配置的vip,我把配置文件中的virtual_address改成了:192.168.1.206     dev eth0 scope global)
    inet6 fe80::1e87:2cff:fe60:1426/64 scope link
    valid_lft forever preferred_lft forever

    可以看到,我们设定的虚拟 IP   192.168.1.206 已经挂接在网卡 eth0上。

    这个时候我们在局域网内ping可以ping通。

    ping  192.168.1.206

    我们在nginx中增加一个server

    server{
    listen 9889;
    server_name 192.168.1.206;

    location / {
    root /usr/local/www;

    }

    }

    这时候在我们电脑上访问http://192.168.1.206:9889/是可以访问的。

    )

    接下是从服务器设置:

    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
    global_defs {
      notification_email {
        admin@example.com
      }
      notification_email_from admin@example.com
      smtp_server 127.0.0.1
      stmp_connect_timeout 30
      router_id node2
    }
     
    vrrp_instance VI_NODE {
      state BACKUP           #与主服务器对应
      interface eth0         #从服务器的通信网卡
      virtual_router_id 100  #路由标识,和主服务器相同
      priority 100           #优先级,小于主服务器即可
      advert_int 5           #这里是接受通知间隔,与主服务器要设置相同
       
      authentication {
       auth_type PASS
        auth_pass 123456     #验证密码,与主服务器相同
      }
       
      virtual_ipaddress {
        192.168.1.206        #虚拟IP,也要和主服务器相同
      }
    }

    上面的设置是最基础的设置,实现的功能是如果主服务器的Keepalived停止服务(一般情况下服务器宕机),则将虚拟IP切换至从服务器,主服务器恢复后从新切换回主服务器。

    启动从服务器后,我们用ip a输出

    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:d8:ef:7e brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.156/24 brd 192.168.1.255 scope global eth0
    inet6 fe80::20c:29ff:fed8:ef7e/64 scope link
    valid_lft forever preferred_lft forever

    可以看到,虚拟 IP没有挂接在网卡 eth0上。

    我们测试功能,

    止主服务器keepalive,我们在客户端ping  192.168.1.206 依然能够ping通。网址能访问。

    这时我们在从服务器上用ip a看到

    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:d8:ef:7e brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.156/24 brd 192.168.1.255 scope global eth0
    inet 192.168.1.206/32 scope global eth0
    inet6 fe80::20c:29ff:fed8:ef7e/64 scope link
    valid_lft forever preferred_lft forever

    然后我们又开启主服务,这时在从服务器上面没有看到    inet 192.168.1.206/32 scope global eth0这行

    说明从服务器又没有绑定虚拟ip  192.168.1.206了。

    但是很多情况下我们面临的处境是nginx挂掉了,而这个时候Keepalived就不能发挥作用,这时候就需要我们来改良下Keepalived了。通过向Keepalived添加一个自定义脚本来监控neginx的运行状态,如果nginx进程结束,则kill Keepalived进程,以此来达到主从服务器的切换功能。

    我们在修改上面配置的主服务器的配置文件,在中间添加脚本实现

    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
    global_defs {
       notification_email {
         admin@example.com
       }
       notification_email_from admin@example.com
       smtp_server 127.0.0.1
       smtp_connect_timeout 30
       router_id nginx_master
    }
    vrrp_script chk_http_port {
       script "/usr/local/keepalived/nginx.sh"  #在这里添加脚本链接
       interval 3       #脚本执行间隔
       weight 2         #脚本结果导致的优先级变更
    }
    vrrp_instance VI_NODE {
        state MASTER
        interface eth0
        virtual_router_id 100
        priority 200
        advert_int 5
        authentication {
            auth_type PASS
            auth_pass 123456
        }
        track_script {
            chk_http_port     #添加脚本执行
        }
        virtual_ipaddress {
            192.168.1.206
        }
    }

    具体的配置可以参考另一篇文章Keepalived配置详解

    如果我们使用了LVS+Keepalived集成,那么keepalived可以代替ipvsadm来配置LVS,可以方便的通过配置就可以搞定,这在另一篇文章Keepalived+LVS配置详解

    修改完配置文件我们写我们的上面配置的nginx.sh,当然我们假定Nginx已经安装完成

    1
    2
    3
    4
    5
    #!/bin/bash
    A=`ps -C nginx --no-header |wc -l`
    if [ $A -eq 0 ];then
       killall keepalived
    fi

    上面的脚本简单的查看nginx进程是否存在,不存在就kill keepalived进程。

    接下来我们对上面的哦脚本修改一下,当脚本检测到nginx没有运行的时候会尝试去启动nginx以此,如果失败则停掉keepalived进程

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #!/bin/bash
    A=`ps -C nginx –no-header |wc -l`
    if [ $A -eq 0 ];then
      /usr/local/nginx/sbin/nginx #nginx命令的路径
      sleep 3
      if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
        killall keepalived
      fi
    fi

    把脚本保存到上面配置的具体路径(我这里是/usr/local/keepalived),然后很重要的一步是修改脚本的执行权限

    chmod +x nginx.sh

    4. 运行Keepalived

    配置完成后就可以运行看下效果了,分别在主从服务器上启动nginx和keepalived

    service keepalived start

    启动之后通过·ip a·命令查看主服务器的网络信息,可以看到在eth0网卡下生成了192.168.1.206这个虚拟ip,并可通过这个ip访问到nginx

    然后我们关闭nginx的进程(如果配置了一次尝试重启那要注意下),然后我们可以通过ps -e查看keepalived进程是否关闭,正常情况下查看网络信息中,可以看到eth0网卡下的虚拟ip已经解除,然后在从服务器的网络信息中可以看到从服务器的eth0网卡绑定了虚拟ip,通过这个ip就访问到了从服务器的nginx去了,这是我们重新启动主服务器的nginx和keepalieved,我们可以发现虚拟ip就绑回到了主服务器。

    这样就实现了基本双击主从热备功能了。

    这里注意下防火墙的问题,就是这问题困扰了我很久。找了一些资料才将问题解决

    因为Keepalived之间是通过组播来通知对方的是否存活,以及发送优先级的,并且通过组播来选举MASTER的,而224.0.0.18就是常用的组播地址,防火墙开启允许这个组播地址通信就可以了:

    1.如果用的是默认防火墙,只需要添加:

    iptables -I RH-Firewall-1-INPUT -d 224.0.0.18 -j ACCEPT

    2.如果是自己用脚本设置的防火墙,需要添加如下规则

    iptables -A OUTPUT -o eth0 -d 224.0.0.18 -j ACCEPT iptables -A OUTPUT -o eth0 -s 224.0.0.18 -j ACCEPT iptables -A INPUT -i eth0 -d 224.0.0.18 -j ACCEPT iptables -A INPUT -i eth0 -s 224.0.0.18 -j ACCEPT

    5. 总结

    • keepalived通过虚拟路由实现双机热备,相比其他方案具有一定的优越性
    • 因为是固定主从热备,该方案比较适合两个互备服务器性能有差异的情况
    • Keepalived同样可以实现双主互备,通过设置互为主备,然后通过DNS负载均衡到不同vip就可以实现

    原文链接(支持下楼主自己的博客^_^):http://thinkjet.me/linux-ha-keepalived-nginx.html

    充:
        重新找了下资料,nginx程序出现问题的时候对keepalive的切换可以这么做:
    修改keepalive的配置文件,添加红字部分:
    vim /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived
    global_defs {
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id LVS_DEVEL
    }
    vrrp_script check_nginx {
         script "/etc/keepalived/check_nginx.sh"
         interval 2
         weight 2
         }
    vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    #backup ipaddress
    mcast_src_ip 10.1.1.191
    priority 100
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass ufsoft
    }
    track_script {
         check_nginx
         }
    virtual_ipaddress {
    10.1.1.190
    }
    }
    在配置文件中增加的
    /etc/keepalived/check_nginx.sh内容如下:
    #!/bin/bash
    if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
     then
      killall -9  keepalived
    fi

    判断nginx进程是否存在,如果不存在则kill掉keepalived进程,这样实现了nginx程序出现异常时的keepalive切换,不用单独再跑一个脚本来做nginx验证操作了。
     
    经过测试,killall掉nginx后,keepalive也会退出。另外验证80服务也能正常切换。
     
     

    怎么查看keepalvied的日志?

      tail /var/log/messages

    配置中

     virtual_ipaddress {
            192.168.1.206
        }
    这个是什么意思?
    看看keepalive的原理:原理是VRRP(VRRP Virtual Router Redundancy Protocol,虚拟路由冗余协议)
    立一个VIP(virtual ip)和VIP对应的mac地址,独立于所有RIP(real ip)之外,主路由器在接管自己的RIP同时,接管这个VIP,当局域网中有报文传输时,主路由器收到报文打开后,发现目标IP就是自己接管的VIP,就会处理这个报文。根据VRRP应用的配置决定下一跳的地址,如果没有专用路由器组,可能自己承担路由器工作同时也是会话服务器,如果下一跳就是自己,就交由自己的会话层处理;如果处于路由器组中,会根据负载均衡策略选取出一台realserver(真实服务器),然后重新组装报文,将ip地址和mac地址替换成realserver的,重新发到局域网中,让realserver处理
     
     

    keepalived运行后,vip不生效的排查  

    参考:

    http://my.oschina.net/zyc1016/blog/138574

     http://www.linuxidc.com/Linux/2015-03/114981.htm

    http://blog.chinaunix.net/uid-20749043-id-4034770.html

  • 相关阅读:
    C++实现希尔排序和快排
    操作系统重点知识汇总
    结构体(对齐规则及举例)
    指针和引用(传指针和传引用)
    数组和指针
    判断一个字符是否为数字的两种方法(C/C++)
    浅谈操作系统栈和堆(区别与联系)
    浅谈malloc/free和new/delete 的区别
    操作符和表达式
    windows重装系统后grub引导菜单修复方法(亲自实验过)
  • 原文地址:https://www.cnblogs.com/youxin/p/5157079.html
Copyright © 2011-2022 走看看