As the business continues to grow, our single server can not afford the demand, then we need to scale, there are two dimensions, one is vertical is to increase the hardware of the server, and the addition of the new server and the previous machine composition cluster to provide services, we know the former is a bottleneck, so, Cluster technology is extremely important to the Web architecture!
Cluster definition, I meng Baidu under can be: cluster is a group of independent, through high-speed network interconnection of computers, they constitute a group, and a single system to manage the Model. When a customer interacts with a cluster, the cluster acts as a standalone server
The role of clustering: increased availability and scalability
Types of clusters: high availability, load balancing, high performance
Scalability we can easily understand that high availability is not difficult, because I have a lot of machines for the same service, so one of the machines hangs up with the other Machines. It can be seen that the availability of the machine as a cluster does not need to be very high (is the server will have a guarantee, will not be bad on their own), so we can save some of the cost of hardware procurement, such as the purchase of Single-supply plug, network card does not need too much, double network card is good and so on, can save costs
Seemingly should not have the upper right corner of the cache, well, the picture is the book, I will not ps, first ignore it, we can see that the application server has become more, the front also added a load balancer scheduler, only painted one, in fact, this is a single point of failure, and its high availability is also required! so, we start from it, commonly used high-availability cluster software has heartbeat, keepalived, rhcs, The simplest commonly used by everyone like keepalived, its installation is very simple, direct Yum can, If the version has the need to compile or that three steps, This does not introduce, we look at the configuration file, the default is in the/etc/keepalived/ keepalived.conf, compiled by the nature of the directory you set up, if you want to see the meaning of parameters, please search the keepalived authoritative guide, the book is short, 10 minutes is enough, but is absolutely authoritative ~
1 host:2! Configuration File forkeepalived3 Global_defs {4 Notification_email {5 [email protected]6 }7 notification_email_from [email protected]8Smtp_server127.0.0.19Smtp_connect_timeout -Ten router_id Haproxy_ha one } a - vrrp_instance Haproxy_ha { - state MASTER the interface Eth0 -virtual_router_id $ -Priority max -Advert_int1 + Authentication { - Auth_type PASS +Auth_pass1111 a } at virtual_ipaddress { - 192.168.56.21 - } -}
1 Standby machine:2! Configuration File forkeepalived3 Global_defs {4 Notification_email {5 [email protected]6 }7 notification_email_from [email protected]8Smtp_server127.0.0.19Smtp_connect_timeout -Ten router_id Haproxy_ha one } a - vrrp_instance Haproxy_ha { - state BACKUP the interface Eth0 -virtual_router_id $ -Priority - -Advert_int1 + Authentication { - Auth_type PASS +Auth_pass1111 a } at virtual_ipaddress { - 192.168.56.21 - } -}
When we start, we will find that the host machine failed to succeed, wtf, what is the situation? That is because we need to change a kernel protection parameter,/proc/sys/net/ipv4/ip_nonlocal_bind, as the name implies, whether the need to bind non-local ip, because our virtual IP is not a local ip, the default is 0 we changed to 1, we can, At this point, the main standby between the establishment of a non-descriptive relationship ~ ~ ~, Stop the host, the opportunity to take over the virtual ip, very fast we do not feel, if you have been ping virtual ip, you may see a small number of milliseconds, our load balancer is highly available, what is the virtual ip? In fact, this virtual IP has two functions, for the load Balancing cluster preparation, and then it is your entire external ip, that is, users only need to access this IP can be, the latter he does not need to know
Again load-balanced cluster, attention is the cluster, the way to achieve load balancing, nic, dns, link calculation, But we are here to discuss the server cluster, set up a server cluster has hardware and software two ways, hardware use F5 and array, however are not cheap, especially F5, the price is really top small north years wages, so, we are the whole software bar ~ software is also divided into two
Four-layer load: forwarding, Maintaining a TCP connection
Seven-layer Load: agent, maintain two independent TCP connections
Four-layer load of LVS is very famous, it has four modes, more than 10 kinds of algorithms, almost support any scene, we enumerate four modes need to pay attention to the point is good
1 NAT: His bottleneck is that the backend host routing must be a scheduler, so as to ensure that the package back to the scheduler, so in and out are required to forward the scheduler, network card traffic can not carry. It is slower than dr, but this is not a fatal disadvantage, in fact, this so-called "slow" almost negligible 2TUN: almost no one to use, but also to play a line ... 3 DR: The backend server must bind the VIP and reject the VIP access package, which requires the scheduler and the real machine within a VLAN segment 4 full-nat: You need to combine with OSPF and compile Ali's kernel
10 of the algorithms I know
1the call scheduling (Round Robin) (RR) Scheduler distributes external requests sequentially to the real servers in the cluster by using the "round call" scheduling algorithm, which treats each server equally, regardless of the actual number of connections and system load on the Server. 2the. weighted wheel (Weighted Round Robin) (WRR) Scheduler Dispatches access requests based on the different processing capabilities of real servers through the "weighted round call" scheduling algorithm. This ensures that the processing capacity of the server can handle more Traffic. The scheduler can automatically inquire about the load of the real server and adjust its weights dynamically. 3the least-link (Least Connections) (LC) Scheduler dynamically dispatches network requests to servers with the fewest number of links established through the "least-connection" scheduling algorithm. If the real server of the cluster system has similar system performance, the "minimum connection" scheduling algorithm can be used to balance the load Well. 4. Weighted least-link (Weighted Least Connections) (WLC) The scheduler uses a "weighted least link" scheduling algorithm to optimize load-balancing performance in a cluster system where server performance is significantly different. Servers with higher weights will withstand a large percentage of the active connection Load. The scheduler can automatically inquire about the load of the real server and adjust its weights dynamically. 5Minimum links based on locality (locality-Based Least Connections) (lblc) "least link based on locality" scheduling algorithm is a load balancing target IP address, which is mainly used in cache cluster System. According to the target IP address of the request, the algorithm finds the most recently used server, if the server is available and not overloaded, sends the request to the server, if the server does not exist, or if the server is overloaded and has half of the workload of the server, the principle of "least link" is used to select an available server. , the request is sent to the Server. 6Minimum locality-based links with replication (locality-Based Least Connections with Replication) (lblcr) "local least-link with replication" scheduling algorithm is also based on the target IP address load balancing, is mainly used in the cache cluster System. It differs from the LBLC algorithm in that it maintains a mapping from a destination IP address to a set of servers, while the LBLC algorithm maintains a mapping from a destination IP address to a server. According to the target IP address of the request, the algorithm finds the corresponding server group of the target IP address, selects a server from the server group according to the principle of "minimum connection", if the server is not overloaded, sends the request to the server, and if the server is overloaded, Select a server from this cluster according to the "minimum connection" Principle. Join the server to the server group and send the request to the Server. also, when the server group has not been modified for some time, the busiest server is removed from the server group to reduce the degree of Replication. 7The destination address hash (Destination Hashing) (DH) "destination address hash" scheduling algorithm finds the corresponding server from a statically allocated hash table based on the requested destination IP address, as hash key (hash key), If the server is available and not overloaded, Send the request to the server, otherwise the return is Empty. 8The source address hash (source Hashing) (SH) "origin address hash" scheduling algorithm based on the requested source IP address, as a hash key (hash Key) from the static distribution of the hash list to find the corresponding server, if the server is available and not overloaded, send the request to the server, otherwise, NULL is Returned. 9the shortest expected delay (shortest expected delay scheduling Sed) (sed) is based on the WLC Algorithm. This must be an example of ABC three machines weight 123 respectively, The number of connections is 123 respectively. Then if a new request comes in using the WLC algorithm, it may be assigned to any of the ABC. The SED algorithm is used to perform such an operation a (1+1)/1B (1+2)/2C (1+3)/3according to the result of the operation, the connection is given CTenMinimum queue scheduling (never queue scheduling NQ) (NQ) does not require a Queue. If there is a realserver number of connections = 0 is directly allocated in the past, do not need to perform SED operations
Another seven-layer load haproxy, its configuration file in/etc/haproxy/haproxy.cfg
1 Host Standby Machine2 Global3Maxconn1000004 chroot/var/lib/Haproxy5 User Haproxy6 Group Haproxy7 Daemon8Nbproc19pidfile/var/run/Haproxy.pidTenStats Socket/var/lib/haproxy.sock Mode -level Admin oneLog127.0.0.1Local3Info a - Defaults -Option http-keep-Alive theMaxconn100000 - Mode HTTP - Timeout Connect 5000ms - Timeout Client 50000ms + Timeout Server 50000ms - + Listen stats a Mode HTTP atBind0.0.0.0:8888 - Stats Enable -Stats uri/haproxy-Status - Stats Auth HAPROXY:BFMQ - - frontend frontend_www_example_com inBind192.168.56.21: the - Mode HTTP to option Httplog + Log Global - default_backend backend_www_example_com the * Backend backend_www_example_com $Option Forwardfor Header x-real-IPPanax NotoginsengOption Httpchk head/http/1.0 - Balance Roundrobin theServer Web-node1192.168.56.11:8080Check Inter -Rise -Fall the +Server Web-node2192.168.56.12:8080Check Inter -Rise -Fall the
It is obvious that we are listening to just the virtual ip192.168.56.21 and forwarding the access to the 80 port to two web machines, the IP of the two machines is real, and it also provides the corresponding port service
Haproxy algorithm (baidu's ~):
①roundrobin, said the simple polling, this does not say, this is the load balancer basicallyhave, ②static-rr, said according to the weight, suggest concern, ③leastconn, indicating that the least connected to the first process, the proposed attention; ④source , according to the request source ip, this is similar to Nginx Ip_hash mechanism, we use it as a method to solve the session problem, it is recommended to pay attention to; ⑤ri, which represents the URL parameter according to the request ' balance Url_ according to the requested Uri;⑥rl_param Param ' requires an URL parameter Name;⑦hdr (name), which indicates that each HTTP request is locked according to the HTTP request header, ⑧RDP-cookie (name), which indicates that according to the cookie (name) To lock and hash each TCP Request.
About Haproxy How to enable disable configuration dynamically then we need to configure the/var/lib/haproxy.sock, we directly to sock communication need to use SOCAT this package
1 Echo " Disable Server Backend_www_example_com/web-node1 " | socat/usr/local/haproxy/haproxy.sock stdio2echo"enable server backend_www _example_com/web-node1" | Socat/usr/local/haproxy/haproxy.sock stdio
of course, He's got other features. echo "help" | Socat Stdio/usr/local/haproxy/haproxy.sock can be self-viewing, very practical!
well, Haproxy's Basic installation is so, we should optimize for him, before we still have to make a single machine on its own to do all the things, so in fact, has been optimized Well. But we remember that no matter how optimized the port range is Dead.
But,haproxy is a professional reverse proxy load Balancer Scheduler that can extend multiple IPs to take advantage of expanding our port range
1 Server web-node1 192.168. 56.11:8080192.168. 56.100:10000-650002 server Web-node2 192.168. 56.11:8080192.168. 56.101:10000-65000
This allows us to expand the range of ports directly, and of course we can continue to add IP extensions
The era of web clustering