ubuntu16.04 部署配置LVS主从

实验环境---ubuntu16.04

四台机器:10.211.55.13—55.16

  • 具体实验环境配置如下:

    10.211.55.102  LVS_VIP

    10.211.55.13  LVS_MASTER

    10.211.55.14  LVS_SALVE

    10.211.55.15  WEB1

    10.211.55.16  WEB2

安装LVS与Keepalived

55.13(lvs master)做如下操作:

sudo apt install ipvsadm

sudo ipvsadm
 
sudo apt install keepalived -y
 
注意:配置文件目录/etc/keepalived/keepalived.conf,默认并没有配置文件,需要手动创建。
parallels@parallels-vm:~$ cat /etc/keepalived/keepalived.conf 
 
global_defs { 
 
    notification_email {  
 
        test@sina.com  
 
    }  
 
    notification_email_from admin@test.com 
 
    smtp_server 127.0.0.1 
 
    smtp_connect_timeout 30 
 
    router_id proxy1
 
 
 
 
vrrp_instance VI_1 { 
 
    state MASTER 
 
    interface enp0s5
 
 
    virtual_router_id 70 
 
    priority 90
 
    advert_int 1 
 
    authentication { 
 
        auth_type PASS 
 
        auth_pass 1111 
 
    } 
 
    virtual_ipaddress { 
 
        10.211.55.102 
 
    } 
 
 
 
 
virtual_server 10.211.55.102 8080 { 
 
    delay_loop 6 
 
    lb_algo rr  
 
    lb_kind DR 
 
    nat_mask 255.255.255.0 
 
    persistence_timeout 50 
 
    protocol TCP 
 
 
 
    real_server 10.211.55.15 8080 { 
 
        weight 1 
 
        TCP_CHECK { 
 
            connect_timeout 3 
 
            nb_get_retry 3 
 
            delay_before_retry 3 
 
        } 
 
    } 
 
 
 
    real_server 10.211.55.16 8080 { 
 
        weight 1 
 
        TCP_CHECK { 
 
            connect_timeout 3 
 
            nb_get_retry 3 
 
            delay_before_retry 3 
 
        } 
 
    } 
 
}
 
致此,master keepalived配置完毕.

打开ip forward功能:文件/etc/sysctl.conf

打开后修改里面“net.ipv4.ip_forward = 1”

执行如下命令使设置生效:

sudo sysctl -p

 

 
 
55.14(lvs slave)做如下操作:

sudo apt install ipvsadm

sudo ipvsadm
 
sudo apt install keepalived -y
 
注意:配置文件目录/etc/keepalived/keepalived.conf,默认并没有配置文件,需要手动创建。
parallels@parallels-vm:~$ cat /etc/keepalived/keepalived.conf 
 
bal_defs { 
 
    notification_email {  
 
        test@sina.com  
 
    }  
 
    notification_email_from admin@test.com 
 
    smtp_server 127.0.0.1 
 
    smtp_connect_timeout 30 
 
    router_id proxy2
 
 
 
 
vrrp_instance VI_2 { 
 
    state SLAVE
 
    interface enp0s5 
 
    virtual_router_id 70 
 
    priority 50
 
    advert_int 1 
 
    authentication { 
 
        auth_type PASS 
 
        auth_pass 1111 
 
    } 
 
    virtual_ipaddress { 
 
        10.211.55.102 
 
    } 
 
 
 
 
virtual_server 10.211.55.102 8080 { 
 
    delay_loop 6 
 
    lb_algo rr  
 
    lb_kind DR 
 
    nat_mask 255.255.255.0 
 
    persistence_timeout 50 
 
    protocol TCP 
 
 
 
    real_server 10.211.55.15 8080 { 
 
        weight 1 
 
        TCP_CHECK { 
 
            connect_timeout 3 
 
            nb_get_retry 3 
 
            delay_before_retry 3 
 
        } 
 
    } 
 
 
 
    real_server 10.211.55.16 8080 { 
 
        weight 1 
 
        TCP_CHECK { 
 
            connect_timeout 3 
 
            nb_get_retry 3 
 
            delay_before_retry 3 
 
        } 
 
    } 
 
}

打开ip forward功能:文件/etc/sysctl.conf

打开后修改里面“net.ipv4.ip_forward = 1”

执行如下命令使设置生效:

sysctl -p

 

接下来开启keepalived服务LVS MASTER.    LVS  SLAVE都要操作如下:55.13  55.14

sudo systemctl start keepalived.service

查看keepalived进程:

[root@lvs keepalived-1.2.7]# sudo ps aux|grep keepalived

root     18485  0.0  0.0  42164   972 ?        Ss   11:43   0:00 keepalived -D

root     18486  0.0  0.2  46464  2376 ?        S    11:43   0:00 keepalived -D

root     18487  0.0  0.1  46340  1740 ?        S    11:43   0:00 keepalived -D

去master查看虚拟ip是否加上:注意如果虚拟ip没有自动加上,请重启下keepalived服务。虚拟ip :55.102(自定义)

parallels@parallels-vm:~$ ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host 

       valid_lft forever preferred_lft forever

2: enp0s5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

    link/ether 00:1c:42:41:65:f6 brd ff:ff:ff:ff:ff:ff

    inet 10.211.55.13/24 brd 10.211.55.255 scope global dynamic enp0s5

       valid_lft 1117sec preferred_lft 1117sec

    inet 10.211.55.102/32 scope global enp0s5

       valid_lft forever preferred_lft forever

    inet6 fdb2:2c26:f4e4:0:2d91:9984:fe45:63ca/64 scope global temporary dynamic 

       valid_lft 602038sec preferred_lft 83527sec

    inet6 fdb2:2c26:f4e4:0:e00e:d6:1c91:3bea/64 scope global mngtmpaddr noprefixroute dynamic 

       valid_lft 2591853sec preferred_lft 604653sec

    inet6 fe80::5806:a54:e63b:90c0/64 scope link 

       valid_lft forever preferred_lft forever

上述显示55.102虚拟ip加上了!

接下里配置部署两台web服务器:

   配置部署10.211.55.15 web1:

         配置虚拟IP启动脚本:

             parallels@parallels-vm:~$ pwd

/home/parallels

parallels@parallels-vm:~$ cat realserver.sh 

#Written by :NetSeek http://www.linuxtone.org

 

VIP=10.211.55.102

 

. /lib/init/vars.sh

. /lib/lsb/init-functions

 

case "$1" in

start)

    ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP

    /sbin/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 >/dev/null 2>&1

    echo "RealServer Start OK"

 

    ;;

stop)

    ifconfig lo:0 down

    route del $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

 

配置部署tomcat

 sudo apt-get install tomcat7 -y

启动tomcat服务:  

sudo systemctl start tomcat7.service

 

为了测试负载均衡,我们将这个页面改下,以更好的标识这个网页是本服务器的

Tomcat7安装目录位于/usr/share/tomcat7,所以我们要编辑tomcat下的webapps/ROOT/index.html这个文件。

parallels@parallels-vm:/usr/share/tomcat7-root/default_root$ pwd

/usr/share/tomcat7-root/default_root

 删掉此目录的index.html文件:sudo rm -f index.html

重新创建此目录的index.html文件,内容如下:

Sudo  touch /usr/share/tomcat7-root/default_root/index.html

parallels@parallels-vm:/usr/share/tomcat7-root/default_root$ cat index.html 

web1 10.211.55.15

启动虚拟IP的脚本:

sudo sh realserver.sh start

 

去LVS_MASTER服务器的终端查看下ipvsadm,查看WEB1(55.15)服务器加到集群里:

sudo ipvsadm -ln

 

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  10.211.55.102:8080 rr persistent 50

  -> 10.211.55.15:8080             Route   1      0          0 

上述可见:55.15已经加进来了!

此时我们访问网页http://10.211.55.15:8080,出现界面显示web1 10.211.55.15;或者使用命令行测试转发情况都可以查看。

配置部署10.211.55.16 web2:

         配置虚拟IP启动脚本:

             parallels@parallels-vm:~$ pwd

/home/parallels

parallels@parallels-vm:~$ cat realserver.sh 

#Written by :NetSeek http://www.linuxtone.org

 

VIP=10.211.55.102

 

. /lib/init/vars.sh

. /lib/lsb/init-functions

 

case "$1" in

start)

    ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP

    /sbin/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 >/dev/null 2>&1

    echo "RealServer Start OK"

 

    ;;

stop)

    ifconfig lo:0 down

    route del $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

 

配置部署tomcat

 sudo apt-get install tomcat7 -y

启动tomcat服务:  

sudo systemctl start tomcat7.service

 

为了测试负载均衡,我们将这个页面改下,以更好的标识这个网页是本服务器的

Tomcat7安装目录位于/usr/share/tomcat7,所以我们要编辑tomcat下的webapps/ROOT/index.html这个文件。

parallels@parallels-vm:/usr/share/tomcat7-root/default_root$ pwd

/usr/share/tomcat7-root/default_root

 删掉此目录的index.html文件:sudo rm -f index.html

重新创建此目录的index.html文件,内容如下:

Sudo  touch /usr/share/tomcat7-root/default_root/index.html

parallels@parallels-vm:/usr/share/tomcat7-root/default_root$ cat index.html 

Web2  10.211.55.16

启动虚拟IP的脚本:

sudo sh realserver.sh start

 

去LVS_MASTER服务器的终端查看下ipvsadm,查看WEB2服务器加到集群里:

sudo ipvsadm -ln

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  10.211.55.102:8080 rr persistent 50

  -> 10.211.55.15:8080            Route   1      0          0         

  -> 10.211.55.16:8080            Route   1      0          0

  

上述55.15 55.16两台web都已经加进来了

 

最后客户端测试

浏览器输入:vip地址即10.211.55.102:8080

停掉其中一台keepalived,服务正常访问!

posted on 2018-09-13 14:10  永远的大空翼  阅读(1649)  评论(0编辑  收藏  举报