Configure WEB Server Load balancer and dynamic/static separation with Haproxy + Keepalived

Source: Internet
Author: User

I. Introduction

II. Environment Introduction

Iii. install and configure backend servers

4. install and configure Haproxy

5. install and configure Keepalived

Vi. Verification Service

I. Introduction

HAProxy introduction:

HAProxy provides high availability, Server Load balancer, and TCP and HTTP application-based proxy. It supports Virtual Hosts and is a free, fast, and reliable solution. HAProxy is especially suitable for websites with extremely high loads, which usually require session persistence or layer-7 processing. HAProxy runs on current hardware and supports tens of thousands of concurrent connections. In addition, its running mode enables it to be easily and securely integrated into your current architecture, while protecting your web servers from being exposed to the network.

HAProxy implements an event-driven, single-process model that supports a large number of concurrent connections. Multi-process or multi-thread models are limited by memory, system schedulers, and ubiquitous lock restrictions, and are rarely able to process thousands of concurrent connections. Because the event-driven model implements all these tasks on a User-Space with better resource and time management, there is no such problem. The disadvantage of this model is that in multi-core systems, these programs are generally less scalable. This is why they must be optimized to make every CPU time slice (Cycle) do more work.

Keepalived introduction:

KeepAlived is a software similar to layer3, 4 & 5 switching mechanism. It is a high-availability solution that uses virtual IP addresses and heartbeat detection to achieve high availability. Keepalived is the Master and Backup roles specified by the two servers respectively, and the priority of the specified Master is higher than that of Backup. Therefore, the virtual IP address is bound to the Master server by default, provide external services. The Master and Backup servers send heartbeat messages to each other at certain intervals to detect the survival status of each other. The interval is generally 2 seconds. If the Backup server finds that the Master is down, then, the Backup sends an ARP packet to the gateway and binds the virtual IP address to its Nic. At this time, Backup provides external services and implements automatic failover, when the Master node recovers, it takes over all resources again.

II. Environment Introduction

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/161F64U4-0.gif "title =" keep_haproxy1.gif "alt =" 161758348.gif"/>

System Version: CentOS 6.4 _ x86_64

Keepalived version: keepalived-1.2.7 click here to download

Haproxy version: Haproxy-1.4.24 click here to download

Topology introduction:

The Keepalived + Haproxy software is installed on the front-end HA1 and HA2 servers. The two servers are both active and standby, and the initial state is that each server has a virtual IP address HA1 VIP: 172.16.14.10 HA2 VIP: 172.16.14.11]. Apache software is installed on backend WEB1 and WEB2 servers to achieve load balancing, while LAMP server is only responsible for parsing php programs, when a user accesses a static page, the browser scheduler distributes the page to the server that processes the static request. If the user accesses a dynamic page of php, will be distributed to the lamp dynamic server for processing, to achieve the dynamic and static separation effect,

Iii. Install backend servers

1. Install and test the LAMP Server

[Root @ lamp ~] # Yum-y install httpd php mysql-server ##### create a test page [root @ lamp ~] # Vim/var/www/html/index. php 

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/161FA236-1.gif "title =" 2013-09-29_132940.gif "alt =" 133004352.gif"/>

2. Install the HTTPD service of the WEB1 server and access the test

[root@WEB1 ~]# yum -y install httpd[root@WEB1 ~]# service httpd start[root@WEB1 ~]# chkconfig httpd on[root@WEB1 ~]# echo "

650) this. width = 650; "src =" http://img1.51cto.com/attachment/201309/133702690.gif "title =" 2013-09-29_133642.gif "alt =" 133702690.gif"/>

3. Install the HTTPD service of the WEB2 server and access the test

[root@WEB2 ~]# yum -y install httpd[root@WEB2 ~]# service httpd start[root@WEB2 ~]# chkconfig httpd on[root@WEB2 ~]# echo "

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/161F64X0-3.gif "title =" 2013-09-29_133920.gif "alt =" 133933830.gif"/>

4. install and configure Haproxy

1. Install Haproxy on the HA1 Server

###### Install the development environment [root @ HA1 ~] # Yum-y install gcc-c ++ [root @ HA1 ~] # Tar xf haproxy-1.4.24.tar.gz [root @ HA1 ~] # Cd haproxy-1.4.24 [root @ HA1 haproxy-1.4.24] # uname-r2.6.32-358. el6.x86 _ 64 [root @ HA1 haproxy-1.4.24] # make TARGET = linux26 ARCH = x86_64 [root @ HA1 haproxy-1.4.24] # make install ###### Note: TARGET specifies the kernel version, the uname-r command is used to view the kernel version. The ARCH command specifies the CPU architecture ------------------------------------------------------------ ##### provides the master configuration file and service script for Haproxy, and add it to the System Service [root @ HA1 haproxy-1.4.24] # mkdir/etc/haproxy [root @ HA1 haproxy-1.4.24] # cp examples/haproxy. cfg/etc/haproxy [root @ HA1 haproxy-1.4.24] # cp examples/haproxy. init/etc/init. d/haproxy [root @ HA1 haproxy-1.4.24] # ln-s/usr/local/sbin/haproxy/usr/sbin/haproxy [root @ HA1 haproxy-1.4.24] # chmod + x/etc/ init. d/haproxy [root @ HA1 haproxy-1.4.24] # chkconfig -- add haproxy

2. Modify the master configuration file

###### Create the Haproxy working directory [root @ HA1 ~] # Mkdir/usr/share/haproxy [root @ HA1 ~] # Vim/etc/haproxy. cfg # this config needs haproxy-1.1.28 or haproxy-1.2.1global log 127.0.0.1 local0 # log configuration, all logs are logged locally, output log 127.0.0.1 local1 notice maxconn 25600 # maximum number of connections chroot/usr/share/haproxy # change the working directory of Haproxy uid 99 # Your UID gid 99 # Your GID nbproc 1 # process Data (multiple processes can be set) daemon # Run Haproxy # debug # enable defaults log global mode http # default protocol, which can be {http | tcp | health} http: layer-7 protocol tcp: layer-4 health: only Return OK option httplog # detailed record http log option dontlognull # do not record Health Check log information retries 3 #3 connection failures, the service is considered unavailable option redispatch # ServerID corresponding server down after the machine, force redirect to maxconn 30000 # default maximum number of connections contimeout 5000 # connection timeout clitimeout 50000 # client timeout srvtimeout 50000 # server timeout check 1 s # Heartbeat detection timeout http -request 10 s # default http request timeout queue 1 m # default queue timeout connect 10 s # default connection timeout client 1 m # Default client timeout server 1 m # default server timeout http-keep-alive 10 s # default persistent connection timeout time listen stats mode http bind 0.0.0.0: 8090 # specify IP address and Port stats enable # enable Haproxy statistics stats refresh 3 s # statistics page automatic refresh Interval stats hide-version # status page does not show version stats uri/allen # statistics the uri on the page is "/allen" stats realm Haproxy \ allen # The content message stats auth admin is displayed during statistics page authentication: admin # username and password on the statistics page stats admin if TRUE # enable or disable status page frontend allen # define Front-End Server bind *: 80 mode http option httpclose # actively close the http connection after each request is completed. option forwardfor # the backend server obtains the IP address of the client. You can obtain the acl url_static path_end-I from the http header. html. jpg. gif # define ACL rules as shown in ". -I: Ignore the case-sensitive acl url_dynamic path_end-I. php default_backend webservers # the backend server address pool use_backend lamp if url_dynamic is called by default during client access # Call the backend server and check whether the ACL rules match backend webservers # define the backend server balance roundrobin # define the algorithm; round-Robin server web1 172.16.14.3: 80 check rise 2 fall 1 weight 2 server web2 172.16.14.4: 80 check rise 2 fall 1 weight 2 backend lamp balance source # defined algorithm; source Address hash operation; similar to Nginx ip_hash server lamp 172.16.14.5: 80 check rise 2 fall 1 secure ##### Note: check: Start health check on the backend server; rise: the number of times that the offline server has successfully switched to the normal state. fall: the number of times that the server needs to be checked to switch from the normal state to the unavailable state. weight: weight. The larger the number, the higher the overweight.

3. Start the Haproxy service and perform access tests.

[root@HA1 ~]# service haproxy start

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/161FA642-4.gif "title =" 2013-09-29_140636.gif "alt =" 140809101.gif"/>

4. Install Haproxy on the HA2 server. The installation and configuration methods are the same as those on the HA1 server.

5. install and configure Keepalived

1. Install Keepalived on the HA1 and HA2 servers.

[root@HA1 ~]# yum -y install keepalived[root@HA2 ~]# yum -y install keepalived

2. Modify the master configuration file of the HA1 Server

[root@HA1 ~]# vim /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs {   notification_email {    root@localhost   }   notification_email_from admin@allen.com   smtp_server 127.0.0.1   smtp_connect_timeout 30   router_id LVS_ALLEN}vrrp_script chk_proess {    script "killall -0 haproxy"    interval 1    weight -2}vrrp_instance ha_1 {    state MASTER    interface eth0    virtual_router_id 56    priority 100    advert_int 1    authentication {        auth_type PASS        auth_pass 1056    }    virtual_ipaddress {    172.16.14.10    }    track_script {        chk_proess    }}vrrp_instance ha_2 {    state BACKUP    interface eth0    virtual_router_id 58    priority 92    advert_int 1    authentication {        auth_type PASS        auth_pass 1058    }    virtual_ipaddress {    172.16.14.11    }}

3. Modify the master configuration file of the HA2 Server

[root@HA2 ~]# vim /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs {   notification_email {    root@localhost   }   notification_email_from admin@allen.com   smtp_server 127.0.0.1   smtp_connect_timeout 30   router_id LVS_ALLEN}vrrp_script chk_proess {    script "killall -0 haproxy"    interval 1    weight -2}vrrp_instance ha_1 {    state BACKUP    interface eth0    virtual_router_id 56    priority 99    advert_int 1    authentication {        auth_type PASS        auth_pass 1056    }    virtual_ipaddress {    172.16.14.10    }}vrrp_instance ha_2 {    state MASTER    interface eth0    virtual_router_id 58    priority 93    advert_int 1    authentication {        auth_type PASS        auth_pass 1058    }    virtual_ipaddress {    172.16.14.11    }    track_script {        chk_proess    }}

4. Start the Keepalived service and view the virtual IP addresses on the HA1 and HA2 servers.

[root@HA1 ~]# service keepalived start[root@HA1 ~]# ip addr show eth02: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000    link/ether 00:0c:29:2c:1a:24 brd ff:ff:ff:ff:ff:ff    inet 172.16.14.1/16 brd 172.16.255.255 scope global eth0    inet 172.16.14.10/32 scope global eth0    inet6 fe80::20c:29ff:fe2c:1a24/64 scope link       valid_lft forever preferred_lft forever------------------------------------------------------------------------[root@HA2 ~]# service keepalived start[root@HA2 ~]# ip addr show eth02: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000    link/ether 00:0c:29:ec:f6:3f brd ff:ff:ff:ff:ff:ff    inet 172.16.14.2/16 brd 172.16.255.255 scope global eth0    inet 172.16.14.11/32 scope global eth0    inet6 fe80::20c:29ff:feec:f63f/64 scope link       valid_lft forever preferred_lft forever

Vi. Verification Service

1. Access and verify the Server Load balancer Function

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/161F640U-5.gif "style =" float: none; "title =" 2013-09-30_12141611.gif "alt =" 121854558.gif"/>

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/161F61U1-6.gif "title =" 2013-09-30_12150722.gif "alt =" 122444937.gif"/>

2. Verify whether dynamic and static separation is achieved

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/161F61119-7.gif "title =" 2013-09-30_12171633.gif "alt =" 121819839.gif"/>

3. If one of the front-end servers fails, verify that the IP address can be automatically switched to the backup server.

###### Stop the HA1 server's haproxy service [root @ HA1 ~] # Service haproxy stop ------------------------------------------------------------------------ ##### view the IP address of the HA2 server [root @ HA2 ~] # Ip addr show eth02: eth0: <BROADCAST, MULTICAST, UP, LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00: 0c: 29: ec: f6: 3f brd ff: ff inet 172.16.14.2/16 brd scope global eth0 inet 172.16.14.11/32 scope global eth0 inet 172.16.14.10/32 scope global eth0 inet6 fe80 :: 20c: 29ff: feec: f63f/64 scope link valid_lft forever preferred_lft forever note: the virtual IP address has been successfully switched to the HA2 server.

4. If the server is repaired and re-launched, the IP address will be switched back to the original server, which will not be verified here; so will the haproxy or keepalived service on the HA2 server be disabled


The Keepalived + Haproxy Implementation of load balancing and dynamic/static separation has been completed. Other related content will be updated in the future blog, so stay tuned...


This article is from the ALLEN blog, please be sure to keep this source http://502245466.blog.51cto.com/7559397/1303506

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.