Linux cluster: LVS Build Load Balancer cluster (ii)

Source: Internet
Author: User

First, the DR Model LVs build 1, preparation work
  • Scheduler dir:192.168.242.128
  • Real server rs1:192.168.242.129, gateway reverts to previous original gateway
  • Real server rs2:192.168.242.130, gateway reverts to previous original gateway
  • vip:192.168.242.110
2. Writing scripts on Dir
[[email protected] ~]# vim /usr/local/sbin/lvs_dr.sh#! /bin/bashecho 1 > /proc/sys/net/ipv4/ip_forwardipv=/usr/sbin/ipvsadmvip=192.168.242.110rs1=192.168.242.129rs2=192.168.242.130#ifdown ifup 同等于重启网卡,对虚拟网卡进行重置#防止再次启动脚本的时候,再次设置虚拟网卡的IP地址ifdown ens33ifup ens33#注意这里的网卡名字ifconfig ens33:2 $vip broadcast $vip netmask 255.255.255.255 uproute add -host $vip dev ens33:2$ipv -C$ipv -A -t $vip:80 -s rr$ipv -a -t $vip:80 -r $rs1:80 -g -w 1$ipv -a -t $vip:80 -r $rs2:80 -g -w 1[[email protected] ~]# sh /usr/local/sbin/lvs_dr.sh 成功断开设备 ‘ens33‘。成功激活的连接(D-Bus 激活路径:/org/freedesktop/NetworkManager/ActiveConnection/2)
3. Write script on RS (two identical contents)
[[email protected] ~]# vim /usr/local/sbin/lvs_dr_rs.sh#!/bin/bashvip=192.168.242.110ifdown loifup lo#把vip绑定在lo上,是为了实现rs直接把结果返回给客户端ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 uproute add -host $vip lo:0#以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端#参考文档 www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.htmlecho "1" > /proc/sys/net/ipv4/conf/lo/arp_ignoreecho "2" > /proc/sys/net/ipv4/conf/lo/arp_announceecho "1" > /proc/sys/net/ipv4/conf/all/arp_ignoreecho "2" > /proc/sys/net/ipv4/conf/all/arp_announce[[email protected] ~]# sh /usr/local/sbin/lvs_dr_rs.sh
4. Testing

I am here to use the fourth virtual machine test, no words can be directly in the browser to enter the VIP address test.

[[email protected] ~]# curl 192.168.242.110LVS-zlinux03[[email protected] ~]# curl 192.168.242.110LVS-zlinux02[[email protected] ~]# curl 192.168.242.110LVS-zlinux03[[email protected] ~]# curl 192.168.242.110LVS-zlinux02[[email protected] ~]# curl 192.168.242.110LVS-zlinux03

Second, Keepalived+lvs

The full architecture requires two servers (dir) to install the Keepalived software respectively, the purpose is to achieve high availability, but the keepalived itself has the function of load balancing, so this experiment can only install a keepalived;
The keepalived has built-in IPVSADM functionality, so you don't need to install the IPVSADM package, or write and execute the Lvs_dir script.

1. Preparatory work

Main keepalived (Scheduler dir): 192.168.242.128
Real server rs1:192.168.242.129, gateway reverts to previous original gateway
Real server rs2:192.168.242.130, gateway reverts to previous original gateway
vip:192.168.242.110

2, modify the keepalived configuration Ask Price
[[email protected] ~]# Cp/etc/keepalived/keepalived.conf/etc/keepalived/keepalived.conf.bak//backup configuration file [email  protected] ~]# >/etc/keepalived/keepalived.conf//emptying [[email protected] ~]# Vim/etc/keepalived/keepali ved.conf//Add the following vrrp_instance vi_1 {#备用服务器上为 BACKUP State MASTER interface Ens33 Virtual_route    r_id #备用服务器上为90 priority Auth_pass Zlinux} virtual_ipaddress {192.168.242.110} } virtual_server 192.168.242.110 80 {# (query Realserver status every 10 seconds) Delay_loop # (LVS algorithm) Lb_algo WLC # (D      R mode) Lb_kind DR # (connection of the same IP is assigned to the same realserver within 60 seconds) Persistence_timeout 0 # (check realserver status with TCP protocol) protocol TCP Real_server 192.168.242.129 80 {# (weight) weight Tcp_check {# (10 seconds No response timeout) connect _timeout nb_get_retry 3 delay_before_retry 3 Connect_port}} real_server 192.1 68.242.130 {Weight Tcp_check {connect_timeout nb_get_retry 3 delay_before_retry 3 Connect_port 80   }     }  }
3. Start keepalived
[[email protected] ~]# ipvsadm -C           //清空之前的规则,因为之前执行过LVS脚本[[email protected] ~]# ipvsadm -lnIP Virtual Server version 1.2.1 (size=4096)Prot LocalAddress:Port Scheduler Flags  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn[[email protected] ~]# systemctl start keepalived     //启动[[email protected] ~]# ps aux | grep keeproot       3406  0.0  0.0 120720  1404 ?        Ss   18:21   0:00 /usr/sbin/keepalived -Droot       3407  0.0  0.1 127464  3324 ?        S    18:21   0:00 /usr/sbin/keepalived -Droot       3408  0.0  0.1 131656  2848 ?        S    18:21   0:00 /usr/sbin/keepalived -Droot       3414  0.0  0.0 112680   976 pts/0    R+   18:25   0:00 grep --color=auto keep

Also need to be performed on two RSsh /usr/local/sbin/lvs_dr_rs.sh

4. Testing

Test Method 1:
Enter the VIP in the browser 192.168.242.110 and then deliberately stop an RS nginx service, then refresh the browser to see the results.
Test Method 2:
In the scheduler, execute the relevant command to view the number of connections:

[[email protected] ~]# ipvsadm -lnIP Virtual Server version 1.2.1 (size=4096)Prot LocalAddress:Port Scheduler Flags  -> RemoteAddress:Port           Forward Weight ActiveConn InActConnTCP  192.168.242.110:80 wlc  -> 192.168.242.129:80           Route   100    0          0         [[email protected] ~]# ipvsadm -ln                 //打开另一台Nginx服务,刷新之后,两个连接IP Virtual Server version 1.2.1 (size=4096)Prot LocalAddress:Port Scheduler Flags  -> RemoteAddress:Port           Forward Weight ActiveConn InActConnTCP  192.168.242.110:80 wlc  -> 192.168.242.129:80           Route   100    0          0           -> 192.168.242.130:80           Route   100    1          

Linux cluster: LVS Build Load Balancer cluster (ii)

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.