最近业余尝试着去配置了下keepalived+LVS,跟着网上的配置,发现最终能运行,但是访问不到后边的服务,查看了很多的资料,现在把具体的配置贴出来,供大家参考。

LVS-DR配置

  1. 环境:

4linux虚拟机(内核支持LVS

VIP:192.168.1.100()对应机器ip:192.168.1.16

VIP:192.168.1.100()对应机器ip:192.168.1.11

RIP:192.168.1.8(tomcat服务)

RIP:192.168.1.9(tomcat服务)

 

  1. 安装相关的软件:

Ipvsadm(192.168.1.16192.168.1.11)

Keepalived(192.168.1.16192.168.1.11)

Tomcat(192.168.1.8)

Tomcat(192.168.1.9)

 

  1. 具体配置脚本:

VIP配置:

! Configuration File for  keepalived

 

global_defs {

    notification_email {

     [email protected]

    }

    notification_email_from [email protected]

    smtp_server smtp.exmail.qq.com

    smtp_connect_timeout 30

    router_id LVS_DEVEL

}

 

vrrp_instance VI_1 {

     state MASTER

     interface eth0

     virtual_router_id 51

     priority 100

     advert_int 1

     authentication {

        auth_type PASS

        auth_pass 123456

     }

     virtual_ipaddress {

        192.168.1.100

     }

}

 

virtual_server 192.168.1.100 8080  {

     delay_loop 6

     lb_algo rr

     lb_kind DR

     nat_mask 255.255.255.255

     persistence_timeout 1

     protocol TCP

 

     real_server 192.168.1.8 8080 {

        weight 1

        HTTP_GET {

            url {

              path /test/jsp/2.jsp

              digest  c2b63159e701960a9eef44a302b56074

            }

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

     }

        

         real_server  192.168.1.9 8080 {

        weight 1

        HTTP_GET {

            url {

              path /test/jsp/2.jsp

              digest  c2b63159e701960a9eef44a302b56074

            }

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

         }

     }

}

注意:此处的digest的算法,最好使用genhash工具去进行计算,具体用法可以去网上查。

 

VIP配置

! Configuration File for  keepalived

 

global_defs {

    notification_email {

     [email protected]

    }

    notification_email_from [email protected]

    smtp_server smtp.exmail.qq.com

    smtp_connect_timeout 30

    router_id LVS_DEVEL

}

 

vrrp_instance VI_1 {

     state BACKUP

     interface eth0

     virtual_router_id 51

     priority 40

     advert_int 1

     authentication {

        auth_type PASS

        auth_pass 123456

     }

     virtual_ipaddress {

        192.168.1.100

     }

}

 

virtual_server 192.168.1.100 8080  {

     delay_loop 6

     lb_algo rr

     lb_kind DR

     nat_mask 255.255.255.255

     persistence_timeout 1

     protocol TCP

 

     real_server 192.168.1.8 8080 {

        weight 1

        HTTP_GET {

            url {

              path /test/jsp/2.jsp

              digest  c2b63159e701960a9eef44a302b56074

            }

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

     }

        

         real_server  192.168.1.9 8080 {

        weight 1

        HTTP_GET {

            url {

              path /test/jsp/2.jsp

              digest  c2b63159e701960a9eef44a302b56074

            }

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

     }

}

 

当配置好VIP以后,为了在真实的服务器上支持对应VIP的路由,需要在真实服务器(此处指192.168.1.8192.168.1.9)上运行一些内容:

#!/bin/bash

 

VIP=192.168.1.100

 

ifconfig lo:0 $VIP broadcast $VIP  netmask 255.255.255.255 up

 

route add –host $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

 

  1. 为了区别keepalived,所以最好简单的写个网页,放在tomcat下以供访问。

  2. 启动所有的服务。现在试一下是否可以访问^_^

  3. 为了了解keepalived的功能,可以进行如下场景的测试:

停掉其中一台真实的服务,看看lvs能够正常工作

停掉主VIP,看看从VIP是否可以接管达到高可用

启动主VIP,看看主会不会跟从VIP进行抢占

运行结果:

keepalive+LVS-DR配置_第1张图片

keepalive+LVS-DR配置_第2张图片