> CentOS > CentOS教程 >

CentOS 7 上配置LVS + Keepalived + ipvsadm

CentOS 7 上配置LVS + Keepalived + ipvsadm

一、部署环境
keepalived:10.10.10.30(CentOS 7)
lvs1:10.10.10.140(CentOS 6.4)
lvs2:10.10.10.150(CentOS 6.4)

二、在lvs1:10.10.10.140上配置
[root@LVS1 ~]# yum install -y httpd 
[root@LVS1 ~]# /etc/init.d/httpd start
[root@LVS1 ~]# cd /var/www/html/
[root@LVS1 html]# echo "ok" > keep.html 
[root@LVS1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 
DEVICE=eth0
HWADDR=00:0C:29:15:B7:DC
TYPE=Ethernet
UUID=93f4695a-8641-4360-9fce-5e3af0e32fc9
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=10.10.10.140
NETMASK=255.255.255.0
GATEWAY=10.10.10.30
[root@LVS1 ~]#/etc/init.d/network restart
[root@LVS1 ~]#/sbin/ifconfig eth0:0 10.10.10.140 broadcast 10.10.10.255 netmask 255.255.255.255 up

三、在lvs2:10.10.10.150上配置
[root@LVS2 ~]# yum install -y httpd 
[root@LVS2 ~]# /etc/init.d/httpd start
[root@LVS2 ~]# cd /var/www/html/
[root@LVS2 html]# echo "ok" > keep.html 
[root@LVS2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 
DEVICE=eth0
HWADDR=00:0C:29:0D:33:AC
TYPE=Ethernet
UUID=e04f57dc-ef9d-4563-bfa4-9c8c1e8fc870
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=static
IPADDR=10.10.10.150
NETMASK=255.255.255.0
GATEWAY=10.10.10.30
[root@LVS2 ~]#/etc/init.d/network restart
[root@LVS2 ~]#/sbin/ifconfig eth0:0 10.10.10.140 broadcast 10.10.10.255 netmask 255.255.255.255 up


四、在keepalived:10.10.10.30上配置
[root@KEEP ~]# vim /etc/sysconfig/network-scripts/ifcfg-eno33554984
HWADDR=00:0C:29:DC:FE:1B
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
#IPV6INIT=yes
#IPV6_AUTOCONF=yes
#IPV6_DEFROUTE=yes
#IPV6_PEERDNS=yes
#IPV6_PEERROUTES=yes
#IPV6_FAILURE_FATAL=no
NAME=eno33554984
#UUID=56cae8b8-235f-471d-9051-2508ee149e48
ONBOOT=yes
NM_CONTROLLED=no
IPADDR=10.10.10.30
NETMASK=255.255.255.0
[root@KEEP ~]# /etc/init.d/network restart

[root@KEEP ~]# cd /data/keepalived/
[root@KEEP keepalived]# tar zxvf keepalived-1.2.12.tar.gz 
[root@KEEP keepalived]# cd keepalived-1.2.12/
[root@KEEP keepalived-1.2.12]# ./configure --prefix=/usr/local/keepalived
[root@KEEP keepalived-1.2.12]# make 
[root@KEEP keepalived-1.2.12]# make install
[root@KEEP keepalived-1.2.2]# ln -s /usr/local/keepalived/etc/keepalived/ /etc/
[root@KEEP keepalived-1.2.2]# ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[root@KEEP keepalived-1.2.2]# ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@KEEP keepalived-1.2.2]# ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@KEEP keepalived-1.2.12]# cd

##keepalived健康检查 HTTP_GET
[root@KEEP ~]# /usr/local/keepalived/bin/genhash -s 10.10.10.140 -p 80 -u /keep.html
MD5SUM = eff5bc1ef8ec9d03e640fc4370f5eacd

[root@KEEP ~]# /usr/local/keepalived/bin/genhash -s 10.10.10.150 -p 80 -u /keep.html
MD5SUM = eff5bc1ef8ec9d03e640fc4370f5eacd

[root@KEEP ~]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived
global_defs {
  router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eno33554984
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.10.10.250
    }
}

virtual_server 10.10.10.250 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP

    real_server 10.10.10.140 80 {
        weight 1
        HTTP_GET {
            url {
              path /keep.html
              digest eff5bc1ef8ec9d03e640fc4370f5eacd
            }
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }

    real_server 10.10.10.150 80 {
        weight 1
        HTTP_GET {
            url {
              path /keep.html
              digest eff5bc1ef8ec9d03e640fc4370f5eacd
            }
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
}
[root@KEEP ~]# /etc/init.d/keepalived restart

五、在keepalived:10.10.10.30上用ipvsadm 命令测试
[root@KEEP ~]# yum install -y ipvsadm
[root@KEEP ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port          Forward Weight ActiveConn InActConn
TCP  10.10.10.250:80 wrr persistent 50
  -> 10.10.10.140:80              Route  1      0          0         
  -> 10.10.10.150:80              Route  1      0          0        

在10.10.10.140执行
[root@LVS1 html]# echo  "" > keep.html 
[root@KEEP ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port          Forward Weight ActiveConn InActConn
TCP  10.10.10.250:80 wrr persistent 50
  -> 10.10.10.150:80              Route  1      0          0    

再在10.10.10.140执行
[root@LVS1 html]# echo  "ok" > keep.html 
[root@KEEP ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port          Forward Weight ActiveConn InActConn
TCP  10.10.10.250:80 wrr persistent 50
  -> 10.10.10.140:80              Route  1      0          0         
  -> 10.10.10.150:80              Route  1      0          0 

(责任编辑:IT)