The theory of cluster--lvs

Source: Internet
Author: User

Original: http://caduke.blog.51cto.com/3365689/1544229

When a single server performance does not meet increasing access traffic, the server's scaling policy:

Scale up: scales up to improve the performance of a single physical host, such as increasing CPU, memory, and so on.

Scale out: Scaling out, dividing the interdependent servers (lamp, etc.) into layers, and then installing the servers at each level at different levels of physical hosts. When the level of the server is unable to withstand the pressure, just increase its level of the host.

The process of dividing hierarchies is also called decoupling, which is the degree of coupling between the programs. Different levels of servers are called corridor.

At each level, a load balancer is required to distribute requests to multiple back-end real servers. To avoid a single point of failure of the load balancer, the load balancer needs to be made into a highly available cluster.

Working level of the load balancer:

TCP/UDP: Work in kernel space, request the kernel to determine the direct forwarding, so fast, good performance. The software has LVS.

Application-Level load balancing: Working in user space, you need to copy the request from the kernel space to the user space and then copy it to the kernel space, so the performance will be lower than the TCP/UDP layer. But because the application layer knows more about the application's higher-level, finer-grained distribution, it is better to use the application tier directly when the amount of user requests does not exceed the load on the application. The software has Nginx, Haproxy, Apache, lighttpd, varnish, squid.

Hardware load Balancing: BIGIP F5, can achieve the concurrency of a single 600-800w.

Citrix's NETSCALER,A10

Classification of clusters:

Lb:load balancing Cluster, Load Balancer cluster.

Ha:high availiablity, High availability cluster

Hp:high perfomance, high performance cluster. Like Apache's officially maintained Hadoop (at least 40 units)

LVS Zhangwensong

The LVS working model is the front-end load balancer load Balancer (also known as the dispatcher dispatcher, Director Scheduler) + backend Real server (Realserver), which is the request to the Director, The Director then distributes the request to different real realserver based on different algorithms.

Similar to Iptables/netfilter,lvs is Ipvsadm/ipvs. Because the iptables filtering, tracking function will greatly reduce the performance of LVS, so Ipvs is generally not used with these tables. However, the rules for the Iptables mangle table prerouting chain are used in some models of LVS.

Ipvs works on the input chain, and when the request content conforms to the rule in the input chain, the input chain reverses the request directly to the postrouting chain and sends it to the backend's real host.

LVs Scheduling Method:

Starting point fair static scheduling algorithm: (that is, the number of connections regardless of the backend realserver)

RR: Round-robin, polling, round robin, rotation, starting from the first of the list of servers, and then looping in turn.

When the performance of each server host is different, the poor performance of the main opportunity to carry, then this algorithm is not reliable.

WRR: Weight round-robin, weighted round called. Distribution according to the weight multiplier, that is, the performance of the host will get more requests. But it is only the beginning of fairness.

sh: Source hashing hash to achieve the connection persistence of LVS.

In general, the server will send a cookie tag to the client to indicate the client's actions, such as items added to the shopping cart, in order to maintain persistent HTTP connection with the client. But when the next link is assigned to another realserver by the director, the cookie is useless and the HTTP persistence is not maintained. However, the session can be synchronized between Realserver via broadcast and multicast. However, this is a very expensive network resource, and if you use memcached to keep the session memcached, then all sessions will be useless. But if you start with the SH algorithm on the director, then you will create a session table using the key value method, the client IP is the key, the internal RS is the value. This ensures that all sessions are sent to a single host. But the problem still remains: an RS is damaged, and a significant percentage of users are still compromised.

dh: Destination Hashing, target address hash.

Application of SH and DH: The director using SH/DH is followed by multiple firewalls (both routers), so that packets only go the same firewall that only allows established to pass.

Results Fair dynamic algorithm: (calculates the number of connections for backend realserver, dynamic distribution)

LC: Least Connection, minimum connection.

Algorithm: overhead=active*256+inactive

This value who is small, who is the next to accept the request of Rs.

Cons: Starting at 0, it may still start with the worst performance.

WLC: Weight LC, weighted minimum connection, default algorithm .

Algorithm: overhead= (active*256+inactive)/weight

Disadvantage: Poor performance may still be the first to accept the request.

sed: Shortest Expect delay, the minimum expected delay, the input to the WLC

Algorithm overhead= (active+1) *256/weight

Cons: Inactive links are no longer counted, but when there is an equivalent connection, you still need to consider the number of inactive connections. And only when a good server has a large heap of requests, poor performance still does not accept the request.

NQ: The never queue never queues up and assigns a first

LBLC: (DH+LC) Lo

cality-based Least Connection Local Minimum connection

LBLCR: Replicated and locality-based Least Connection

Local Minimum connection with replication

English abbreviation:

Director: Scheduler,

Vip:virtual IP, floating virtual IP on the scheduler

RIP: The backend provides the IP of the server's real host

DIP: The IP that the scheduler communicates with the RIP

Cip:client IP, client IP sent to the request

Type of work for LVS:

NAT, DR, TUN, Fnat

NAT Type:

1, RS should use private IP;

2, RS Gateway should point to dip;

3, RIP and dip should be within the same network segment;

4, incoming and outgoing messages have to pass the director, under high load situation, director will become a performance bottleneck.

5, support port mapping;

6, RS can use any OS;

7, because the intelligent Drive 10 in the RS, so the production environment is almost not used.

Dr Type:

1, RS can use private IP, also can use public network IP;

2, Realserver Gateway can not point to dip;

3, dip and rip to be in the same network segment, so RS and director in the same physical network, that is, the same computer room, so there is no disaster prevention function;

4, inbound message through director, the outbound message is directly from the RS response to the client;

5, can not do port mapping;

6, RS can be the most OS;

7, each RS can yo rip and VIP;

8, because the RS has a VIP, and the last to arrive at the RS data transmission is in the frame mode, so need to open the RS arptables, or other parameters to reject the switch to VIP broadcast request.

9, when VIP, DIP, rip in the same network segment, they can use the total gateway.

However, when the VIP, dip and rip are not in a network segment, RS needs to add a separate route and then go to the director's gateway by this separate route.

For example, if the cluster node is to return the reply message to the client itself, the cluster node itself needs a VIP, otherwise the client will not accept packets sent by other hosts for no reason. But in this way, the cluster node needs to deal with the ARP broadcast problem within the internal network, otherwise the router of the cluster will be out of the question when it broadcasts a VIP. The methods for handling ARP broadcasts are:

1, the VIP is directly bound to the director on the router of the cluster, but the IDC room is not generally allowed to be operated.

2, use Arptables on RS, reject ARP broadcast request.

3, use the 2 parameters introduced after the Linux kernel 2.6.4 to set the notification and response mode for ARP.

Explanation: For Linux systems, the IP address is the data host, not the individual network card.

When an ARP request arrives at a Linux host, by default, the computer has that IP address,

The host will answer regardless of which NIC the ARP request is coming in.

At this point, you need to prevent RS from responding to ARP requests coming in from any NIC.

For example, eth0 network card IP bit 172.16.100.100, but from the Eth0 network card in a request 192.168.100.101 ARP broadcast, the default as long as Linux has this IP, will respond to go out.

Once again, when a Linux host accesses a network, by default, all of its IP addresses and corresponding MAC addresses are advertised to all networks.

At this point, it is necessary to prohibit RS from any network to advertise their VIP and corresponding Mac.

Kernel parameter setting Method:

Arp_announce defines the ARP notification throttling level, which defaults to 0 and should be set to 2.

0, default, the Linux host advertises its own ip+mac of all interfaces to the network of all interfaces.

1, the Ip+mac of other interfaces is not advertised on an interface as much as possible, but may also be advertised.

2, it is absolutely forbidden to advertise the actual setting value of the ip+mac,rs of other interfaces on an interface

Echo 2 >/proc/sys/net/ipv4/conf/lo/arp_announce

Echo 2 >/proc/sys/net/ipv4/conf/all/arp_announce

Arp_ignore defines the ARP response level, which defaults to 0 and should be set to 1.

0, the default value, any interface incoming requests, Linux will give.

The IP of the 1,ARP request must be the IP of the incoming interface, otherwise it will not respond.

For example, the Eth0 interface is 192.168.100.101, and if a request comes in from eth1, it will not respond.

Echo 1 >/proc/sys/net/ipv4/conf/lo/arp_ignore

Echo 1 >/proc/sys/net/ipv4/conf/all/arp_ignore


Delivery of packets within the cluster's internal network:

When the packet enters the director, it will be removed from the source mac+ Target Mac, but when entering the input chain, the Ipvs rule is forced to twist to the postrouting chain, because the cluster hosts are connected to the switch, and the switch is only to identify the Mac, So the packet just changed the source mac+ Target Mac on the director, and the other levels didn't change.

When the packet arrives inside RS and sees its own Mac and has a VIP, it starts building the response message.

When RS sends out the paper, by default, the Linux default packet from which network card outgoing data, the IP of that network card as the source IP, it is necessary to develop a mandatory routing policy, the source address must be a VIP, regardless of which network card out, such as using the route add- Host $VIP Dev lo:0.

Tun Tunnel Model:

1, RIP, dip can not be a private IP;

2, RS Gateway can not point to dip;

3, inbound messages through the Director, outbound from the RS directly respond to the client;

4, port mapping is not supported;

5. The OS that supports IP Tunnelin can be used for Rs.

IP tunneling refers to the data on the IP layer plus a layer of source ip+ destination IP, so that after reaching the outermost IP address device, the device after unpacking the message found not to their own, and then forwarded.

Process:

When the client packet arrives at the VIP of the director with 2 public IP, the packet is encapsulated two times, the IP layer of the original packet is maintained on the network, and a layer of IP (dip+rip) +mac is added, then the forwarding of the routing device arrives at the RIP of Rs with the public IP. RS finds itself with a VIP and starts building the response.

Ipvs use of the User space tool Ipvsadm:

# grep-i Ipvs-c 20/boot/config-2.6.32-358.el6.x86_64

View kernel support does not support IPVS and more supported algorithms.

# yum Install ipvsadm-y this becomes a Director scheduler.

Add/Edit Cluster service:

ipvsadm-a| E-t|u|f Service-address [-S scheduler] [-P [Timeout]] [-O] [-M netmask]

-A: Adding a Cluster service

-E: Editing the Cluster service

-t:tcp

-u:udp

-f:firewall mark is used to build a persistent connection to RS and use Iptables to mark multiple ports

Service-address:vip:port

-S: Specify the Scheduling method

-P: Indicates persistent persistence, specifying timeout for persistent connections

Add a VIP for 172.16.100.10 httpd service

Ipvsadm-a-T 172.16.100.10:80-s WRR

To delete a Cluster service:

ipvsadm-d-t|u|f service-address

-D: Delete a Cluster service

Add/Edit rs:

Ipvsadm-a|e-t|u|f service-address-r server-address [-g|i|m] [-w weight] [-X upper] [-y lower]

-A: Add RS to the specified Cluster service

-E: Modify RS in the Cluster service

-R ip[:p ORT]: Specify the address of the RS

Specify the LVS type:

-G:DR model, default, gateway gateways

-i:tun model, Internet, expressed in IP tunnels

-m:nat model

-W Weight: Specifies a weight that takes effect when an algorithm with weights is used.

Add 2 rs to the 172.16.100.10:80 cluster and use the DR Model:

Ipvsadm-a-T 172.16.100.10:80-r 192.168.100.1-g

Ipvsadm-a-T 172.16.100.10:80-r 192.168.100.2-g

Remove RS from the Cluster service:

ipvsadm-d-t|u|f service-address-r server-address

-D: Remove RS

Save Ipvs Configuration:

Service Ipvsadm Save

Save to:/etc/sysconfig/ipvsadm

Ipvsadm-s >/path/to/ipvsadm.rules

Ipvsadm-r </path/to/ipvsadm.rules

To view IPVSADM connections:

Ipvsadm-l|l [-n] [-c] [--stats] [--rate] [--timeout]

-N: Digital display

-C: Statistical information

--stats: Display Status

--rate: Display Rate

--timeout: Display Timeout

Reset the IPVSADM counter:

Ipvsadm-z

Clear all rules for Ipvsadm:

Ipvsadm-c

Persistent connection of LVS:

The salutation of a persistent connection is inaccurate, because SH, DH is not a persistent connection mechanism for TCP.

KeepAlive refers to the TCP three handshake, will establish a channel, for a period of time thereafter can pass through this channel.

The SH dispatch method only sends the same CIP request to the same RS for a period of time, and only resolves the persistence of a single cluster service. This time depends on the persistent connection time of the TCP session, which is set by the TCP session itself.

When we want a client's httpd and SSH request to be sent to the same RS, then we need to use port affinity to solve: treat 2 or more cluster services as a service, that is, to bind multiple ports together, marking the last. Just let Ipvsadm know the mark.

LVS Persistent Template

A persistent connection template, which is a hash table that is stored in memory, that is, a key value list. The key is CIP and the value is Rs.

For a very busy scheduler, this table takes up a lot of space, so the system restricts it by default.

So some scenarios need to modify this limit value.

The persistence time of the template is also limited, and the default is 360 seconds on CentOScar6.4, which can be modified with-p.

Pcc:persistent Client Connection

Persistent client connections

All service requests that are made to the same client take effect and are sent to a server.

Demonstrate:

Ipvsadm-c Clear All Rules

Ipvsadm-a-T 192.168.1.10:0-S RR-P

Ipvsadm-a-T 192.168.1.10:0-R 192.168.1.210

Ipvsadm-a-T 192.168.1.10:0-R 192.168.1.212

The above is to direct all services to the same server. (0 = ALL ports)

Ppc:persistent Port Connection

Persistent Port connections

Valid only for one service.

You can write 0 on this port using the-T service:port to indicate that all

PFM: Persistent firewall tag Connection

When the packet has just reached the director's prerouting chain, a tag is played. For example, the VIP 80 and 443 ports are labeled bit 10, and then use-f to define the tag as a Cluster service, here only to mark the message, and did not change the message. And marking is implemented on the prerouting chain on the mangle table, so iptables rules are required. This tag is a number that is bounded from 0 to 99.

Iptables Marking Method:

Iptables-t mangle-a prerouting-d $VIP-P TCP--dport 80-j MARK--set-mark 10

Iptables-t mangle-a prerouting-d $VIP-P TCP--dport 443-j MARK--set-mark 10

Ipvsadm Cluster Definition Method:

Ipvsadm-a-F 10-s rr-p

Ipvadm-a-F 10-r 192.168.1.210-g

High Availability of LVS:

The director of LVS is able to use a script to monitor the back-end RS for health status, which can be deleted in a timely manner, but this state is not redundant because the user data is lost.

For specific LVS experiment, please see the following section

Cluster--lvs theory (RPM)

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.