Lvs+keepalived for web load and high availability

Source: Internet
Author: User

Lab Environment:
Lvs-master 192.168.1.236
Lvs-backup 192.168.1.237
LVS-DR-VIP 192.168.1.238
Web1 192.168.1.239
WEB2 192.168.1.240

Note: Yum installs the same package version


1. Install LVS and keepalived on two servers respectively
[Email protected] ~]# yum-y install Ipvsadm
[Email protected] ~]# yum-y install keepalived

[Email protected] ~]# yum-y install Ipvsadm
[Email protected] ~]# yum-y install keepalived

2. Modify the Lvs-master configuration file
[Email protected] ~]# vi/etc/keepalived/keepalived.conf
! Configuration File for Keepalived

Global_defs {
Notification_email {
[email protected] #email Notice
}
Notification_email_from [email protected]
Smtp_server 127.0.0.1
Smtp_connect_timeout 30
router_id Lvs_devel
}

Vrrp_instance Vi_1 {
State Master #设置lvs的状态, error MASTER and backup two types, must be capitalized
Interface Eth1 #设置对外服务的接口
virtual_router_id Wuyi #设置虚拟路由表示
Priority #设置优先级, the greater the value, the higher the precedence
Advert_int 1 #设置同步时间间隔
Authentication {#设置验证类型和密码
Auth_type PASS
Auth_pass 1111
}
virtual_ipaddress {#设置lvs VIP
192.168.1.238
}
}

Virtual_server 192.168.1.238 80 {
Delay_loop 6 #健康检查时间间隔
Lb_algo WRR #负载均衡调度算法
Lb_kind DR #负载均衡转发规则
#persistence_timeout #设置会话保持时间, useful for BBS, etc.
Protocol TCP #协议
Real_server 192.168.1.239 80 {
Weight 3 #设置权重
Tcp_check {
Connect_timeout 3
Nb_get_retry 3
Delay_before_retry 3
Connect_port 80
}
}
Real_server 192.168.1.240 80 {
Weight 3
Tcp_check {
Connect_timeout 3
Nb_get_retry 3
Delay_before_retry 3
Connect_port 80
}
}
}
Modify the configuration of the Lvs-backup
! 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
Interface eth1
VIRTUAL_ROUTER_ID 51
Priority 90
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass 1111
}
virtual_ipaddress {
192.168.1.238
}
}

Virtual_server 192.168.1.238 80 {
Delay_loop 6
Lb_algo WRR
Lb_kind DR
#persistence_timeout 20
Protocol TCP

    real_server 192.168.1.239 {
        weight 3
        Tcp_check {
        connect_ Timeout 3
        nb_get_retry 3
         Delay_before_retry 3
        connect_port
        
   }
real_server 192.168.1.240 {
         weight 3
        Tcp_check {
         connect_timeout 3
        nb_get_retry 3
         delay_before_retry 3
        Connect_port 80
       }
   }
}

3. Add configuration on both realserver ends (i.e. web1 and WEB2, as configured)
Configure the Web service
[Email protected]~]# yum-y Install httpd
[Email protected] ~]# echo "WEB11" >/var/www/html/index.html
[Email protected]~]# yum-y Install httpd
[Email protected] ~]# echo "Web22" >/var/www/html/index.html

[[email protected] ~]#/etc/rc.d/init.d/realserver (WEB1 and WEB2 configuration)
#!/bin/bash
# Description:config Realserver lo and apply Noarp

sns_vip=192.168.1.238

. /etc/rc.d/init.d/functions

Case ' 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: $ {start|stop}"
Exit 1
Esac

Exit 0
[Email protected] ~]# chmod o+x/etc/rc.d/init.d/realserver
A brief description of what the above script does:

1) VIP (virtual IP). The VIP of the direct routing mode must be in the same network segment as the server's external service IP address, and the LVS load balancer and all other servers that provide the same functionality use this VIP;

2) The VIP is bound on the loopback interface lo0:0, its broadcast address is itself, the subnet mask is 255.255.255.255. This differs greatly from the standard network address settings. This variable length mask is used to divide the network segment into only one host address to avoid IP address collisions.

3) Echo This section is the function of suppressing ARP broadcasts. If you do not do ARP suppression, there will be numerous machines declaring to others: "Hey! I'm Obama, I'm here! "And that's a mess.

4. Testing
Start keepalived and Ipvsadm on the Lvs-master
[[email protected] ~]#/etc/init.d/ipvsadm start
[[email protected] ~]#/etc/init.d/keepalived start

650) this.width=650; "title=" 1.jpg "src=" Http://s3.51cto.com/wyfs02/M00/58/42/wKioL1StDXzgW9DvAAIGzvukwuQ859.jpg " alt= "Wkiol1stdxzgw9dvaaigzvukwuq859.jpg"/>

650) this.width=650; "title=" 2.jpg "src=" Http://s3.51cto.com/wyfs02/M00/58/45/wKiom1StDMqiscp9AAOzBLbsdkI991.jpg " alt= "Wkiom1stdmqiscp9aaozblbsdki991.jpg"/>

Start keepalived and Ipvsadm on the Lvs-backup
[[email protected] ~]#/etc/init.d/ipvsadm start
[[email protected] ~]#/etc/init.d/keepalived start

650) this.width=650; "title=" 3.jpg "src=" Http://s3.51cto.com/wyfs02/M01/58/42/wKioL1StDeTDUkOyAAGlio2CNGE117.jpg " alt= "Wkiol1stdetdukoyaaglio2cnge117.jpg"/>

650) this.width=650; "title=" 4.jpg "src=" Http://s3.51cto.com/wyfs02/M01/58/45/wKiom1StDTPi40kgAANEYes8Tn4316.jpg " alt= "Wkiom1stdtpi40kgaaneyes8tn4316.jpg"/>

Start Realserver on the Web1
[[email protected] ~]#/etc/init.d/realserver start

650) this.width=650; "title=" 5.jpg "src=" Http://s3.51cto.com/wyfs02/M02/58/42/wKioL1StDhWBilgwAAIPtbB_Gw0224.jpg " alt= "Wkiol1stdhwbilgwaaiptbb_gw0224.jpg"/>

Start Realserver on the WEB2
[[email protected] ~]#/etc/init.d/realserver start

650) this.width=650; "title=" 6.jpg "src=" Http://s3.51cto.com/wyfs02/M02/58/45/wKiom1StDWeTCmK6AAIJuYK4xBs746.jpg " alt= "Wkiom1stdwetcmk6aaijuyk4xbs746.jpg"/>

Test Web Access
[Email protected] ~]#/etc/init.d/httpd start #启动apache
[[email protected] ~]#/etc/init.d/httpd start

650) this.width=650; "title=" 7.jpg "src=" Http://s3.51cto.com/wyfs02/M00/58/45/wKiom1StDYrC8viTAAEi4E5pL78769.jpg " alt= "Wkiom1stdyrc8vitaaei4e5pl78769.jpg"/>

Close Web1
[[email protected] ~]#/etc/init.d/httpd stop

650) this.width=650; "title=" 8.jpg "src=" Http://s3.51cto.com/wyfs02/M02/58/42/wKioL1StDmPxtgaEAAEdvkAdpss880.jpg " alt= "Wkiol1stdmpxtgaeaaedvkadpss880.jpg"/>

Highly Available tests
Close Lvs-master's keepalived and Ipvsadm
[[email protected] ~]#/etc/init.d/keepalived stop
[[email protected] ~]#/etc/init.d/ipvsadm stop

650) this.width=650; "title=" 9.jpg "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M01/58/42/ Wkiol1stdooazlnzaahjmdhtzdw074.jpg "alt=" Wkiol1stdooazlnzaahjmdhtzdw074.jpg "/>

650) this.width=650; "title=" 10.jpg "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M01/58/45/ Wkiom1stdctzl2i0aamahb4andk621.jpg "alt=" Wkiom1stdctzl2i0aamahb4andk621.jpg "/>

See the LVS VIP on the Lvs-backup

650) this.width=650; "title=" 11.jpg "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M02/58/42/ Wkiol1stdqyd-qbvaahzpbupogk752.jpg "alt=" Wkiol1stdqyd-qbvaahzpbupogk752.jpg "/>

650) this.width=650; "title=" 12.jpg "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M02/58/45/ Wkiom1stde2xoksbaaojwou6zjk479.jpg "alt=" Wkiom1stde2xoksbaaojwou6zjk479.jpg "/>

Test if Web Access is still normal

650) this.width=650; "title=" 13.jpg "src=" http://s3.51cto.com/wyfs02/M00/58/42/wKioL1StDs6zrAGZAAEaaXz8HaQ183.jpg "alt=" Wkiol1stds6zragzaaeaaxz8haq183.jpg "/>

You can see that website access is normal

This article is from the "Linux art (q Group: 1991706)" blog, please be sure to keep this source http://304076020.blog.51cto.com/7503470/1600384

Lvs+keepalived for web load and high availability

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.