CentOS6.5 LVS + KeepAlived搭建步骤

测试环境架构:

CentOS6.5 LVS + KeepAlived搭建步骤_第1张图片

  废话不说,开始搭建:
      
     1.创建内核文件的软链接,如果不创建此链接编译ipvsadm将报错
            ln -s /usr/src/kernels/2.6.18-308.el5-x86_64/ /usr/src/linux   (2.6.18-308.el5-x86_64:内核版本

      2.下载ipvsadm
            wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
  
     3.安装ipvsadm            
            # tar xvf ipvsadm-1.24.tar.gz -C /usr/local/src/
            # cd /usr/local/src/ipvsadm-1.24/
            # make && make install
            开启: /etc/init.d/ipvsadm start
            如果报错:ApplyingIPVSconfiguration:/etc/init.d/ipvsadm:line62:/etc/sysconfig/ipvsadm:没有那个文件或目录
           遇到这个错误。运行service ipvsadm save。之后再执行service ipvsadm start

     4.
安装keepalived的依赖包
            # yum -y install openssl-devel popt-devel libnl-devel
            # ldconfig

      5.下载keepalived
            # wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz

      6.
安装keepalived
            # tar xvf keepalived-1.2.7.tar.gz -C /usr/local/src/
            # cd /usr/local/src/keepalived-1.2.7/
            # ./configure && make && make install

       7.
拷贝配置文件、启动脚本等
            # cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
            # cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
            # mkdir /etc/keepalived
            # cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
            # cp /usr/local/sbin/keepalived /usr/sbin/

      8.配置keepalived主节点
            这里使用DR模式、rr调度算法。
            
            # vim /etc/keepalived/keepalived.conf
            ! Configuration File for keepalived
 
            #全局定义:设置keepalived的通知机制和标识
            global_defs {
               notification_email {  #定义接收邮件的地址,一行对应一个
               [email protected]
           }
           notification_email_from [email protected]
           smtp_server 127.0.0.1 #指定smtp服务器地址
           smtp_connect_timeout 30
           router_id LVS_DEVEL  #定义运行keepalived机器的标识
            }
 
        #VRRP实例组定义:VRRP实例就表示在上面开启了VRRP协议,这个实例说明了VRRP的一些特性,如主从、VRID等,可以在每个接口上开启一个实例。
        vrrp_instance VI_1 {
                state MASTER #指定实例的初始状态,高优先级的会竞选为master,
                interface eth0 #实例绑定的网卡
                virtual_router_id 51 #VRID标记(0...255),MASTER和BACKUP要一致
                priority 100 #数字越高级别越高,master要高于backup至少50
                advert_int 1 #检查间隔,默认1s
                authentication {  #设置认证
                auth_type PASS  #认证方式,支持PASS和AH
                auth_pass 1111  #认证的密码
        }
            virtual_ipaddress {  #里面指定漂移地址(VIP)
             192.168.0.249
        }
    }
 
    #LVS配置
    virtual_server 192.168.0.249 80 {
    delay_loop 6 #服务轮询的延时时间
    lb_algo rr #lvs的调度算法
    lb_kind DR #lvs集群模式
    nat_mask 255.255.255.0
    #persistence_timeout 50 #会话保持时间(秒),测试方便取消设置
    protocol TCP #使用的协议是TCP
 
    real_server 192.168.0.235 80 {
        weight 1  #默认为1,0为失效
        TCP_CHECK { #后端服务器健康检查
            connect_timeout 3 #连接超时时间
            nb_get_retry 3 #重连次数
            delay_before_retry 3 #重连间隔时间(秒)
            connect_port 80
        }
    }
 
    real_server 192.168.0.244 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
      }
    }

      9.配置keepalived备节点
            
! Configuration File for keepalived
 
            global_defs {
               notification_email {
                 [email protected]
            }
           notification_email_from [email protected]
           smtp_server 127.0.0.1
           smtp_connect_timeout 30
           router_id LVS_DEVEL
        }
 
        vrrp_instance VI_1 {
            state BACKUP  #备机使用BACKUP
            interface eth0
            virtual_router_id 51
            priority 50  #备机的优先级要低于MASTER
            advert_int 1
            authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
             192.168.0.249
        }
    }
 
    virtual_server 192.168.0.249 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.255.0
    #persistence_timeout 50
    protocol TCP
 
    real_server 192.168.0.235 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
 
    real_server 192.168.0.244 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
      }
  }

    10.重要设置
          
MASTER DR和BACKUP DR都开启路由转发:
             echo "1" > /proc/sys/net/ipv4/ip_forward
             固定到配置文件:
            # vim /etc/sysctl.conf
             net.ipv4.ip_forward = 1

     11.
启动keepalived服务(Master DR和Backup DR都启动)
            # /etc/init.d/keepalived start
            查看绑定的ip地址:
            # ip add show eth0
           CentOS6.5 LVS + KeepAlived搭建步骤_第2张图片
                  

       12.web服务器配置
             配置虚拟ip启动脚本:
         #vim /etc/init.d/realserver.sh
       在文件中输入以下脚本:
       #!/bin/bash
        SNS_VIP=192.168.10.100
        . /etc/rc.d/init.d/functions
        case "$1" in
        start)
         ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
         /sbin/route add -host $SNS_VIP dev lo:0
         echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
         echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
         echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
         echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
         sysctl -p >/dev/null 2>&1
         echo "RealServer Start OK"
         ;;
        stop)
         ifconfig lo:0 down
         route del $SNS_VIP >/dev/null 2>&1
         echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
         echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
         echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
         echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
         echo "RealServer Stoped"
         ;;
         *)
         echo "Usage: $0 {start|stop}"
         exit 1
        esac
        exit 0

        关闭防火墙:
        service iptables stop
        启动脚本:
       sh /etc/init.d/realserver.sh start

 

    13.查看lvs状态:
            #ipvsadm -Ln
            CentOS6.5 LVS + KeepAlived搭建步骤_第3张图片


    至此安装完毕,访问192.168.0.249如期望一样,手动停掉lvs服务 /etc/init.d/keepalived stop 测试也如期望一样。



来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29754888/viewspace-1442730/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29754888/viewspace-1442730/

你可能感兴趣的:(CentOS6.5 LVS + KeepAlived搭建步骤)