Build a high-availability Load Balancing Cluster Based on Haproxy + Keepalived, haproxykeepalived

Source: Internet
Author: User

Build a high-availability Load Balancing Cluster Based on Haproxy + Keepalived, haproxykeepalived

Lab environment:

Host Name

IP address

VIP

192.168.200.254

Haproxy-1

192.168.200.101

Haproxy-2

192.168.200.102

Nginx1

192.168.200.103

Nginx2

192.168.200.104

 

 

1. Compile and install the nginx service on Nginx1/2
1.1 first install Nginx1

[root@Nginx-1 ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel[root@Nginx-1 ~]# useradd -M -s /sbin/nologin  nginx[root@Nginx-1 ~]# tar xf nginx-1.6.2.tar.gz -C /usr/src[root@Nginx-1 ~]# cd /usr/src/nginx-1.6.2[root@Nginx-1 nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install[root@Nginx-1 nginx-1.6.2]# cd /usr/local/nginx/html/[root@Nginx-1 html]# echo "server 192.168.200.103" > index.html[root@Nginx-1 html]# /usr/local/nginx/sbin/nginx [root@Nginx-1 html]# netstat -anpt |grep nginxtcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4503/nginx  

1.2 install Nginx2 in the same way as Nginx1.
The only difference from Nginx1 is:

[root@Nginx-2 html]# echo "server 192.168.200.104" > index.html

2, the installation of Haproxy-1 and Haproxy-2 two machine configuration consistent:

[root@Haproxy-1 ~]# yum -y install gcc gcc-c++ make pcre-devel bzip2-devel[root@Haproxy-1 ~]# tar xf haproxy-1.4.24.tar.gz -C /usr/src/[root@Haproxy-1 ~]# cd /usr/src/haproxy-1.4.24/[root@Haproxy-1 haproxy-1.4.24]# make TARGET=linux26 && make install

2.1 Haproxy Server Configuration
Create a configuration directory and file for haproxy

[root@Haproxy-1 haproxy-1.4.24]# mkdir /etc/haproxy[root@Haproxy-1 haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/

2.2 Introduction to haproxy configuration items
Haproxy configuration files are generally divided into three parts: global (global configuration) defaults (default configuration) listen (application components)

[root@Haproxy-1 ~]# vim /etc/haproxy/haproxy.cfg# this config needs haproxy-1.1.28 or haproxy-1.2.1globallog 127.0.0.1local0log 127.0.0.1local1 notice#log loghostlocal0 infomaxconn 4096#chroot /usr/share/haproxyuid 99gid 99daemon#debug#quietdefaultslogglobalmodehttpoptionhttplogoptiondontlognullretries3#redispatchmaxconn2000contimeout5000clitimeout50000srvtimeout50000listenweb-cluster 0.0.0.0:80option httpchk GET /index.htmlbalanceroundrobinserverinst1 192.168.200.103:80 check inter 2000 fall 3serverinst2 192.168.200.104:80 check inter 2000 fall 3

2.3 create a self-starting script

[Root @ Haproxy-1 ~] # Cp/usr/src/haproxy-1.4.24/examples/haproxy. init/etc/init. d/haproxy [root @ Haproxy-1 ~] # Ln-s/usr/local/sbin/haproxy/usr/sbin/haproxy [root @ Haproxy-1 ~] # Chmod + x/etc/init. d/haproxy [root @ Haproxy-1 ~] #/Etc/init. d/haproxy startStarting haproxy: [OK]

2.4 client access test:

Open http: // 192.168.200.101 in the browser and open a new browser to access http: // 192.168.200.101 again.

Open http: // 192.168.200.102 in the browser and open a new browser to access http: // 192.168.200.102 again.

The two access results can be verified as follows:

Server 192.168.200.103

Server 192.168.200.104

 

3. Compile and install the keepalived Service

[root@Haproxy-1 ~]# yum -y install kernel-devel openssl-devel popt-devel[root@Haproxy-1 ~]# tar xf keepalived-1.2.13.tar.gz [root@Haproxy-1 ~]# cd keepalived-1.2.13[root@Haproxy-1 keepalived-1.2.13]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.18-194.el5-i686 && make && make install

3.1 configure keepalibed boot script

[root@Haproxy-1 ~]# chkconfig --add keepalived[root@Haproxy-1 ~]# chkconfig keepalived on[root@Haproxy-1 ~]# chkconfig --list keepalived

3.2.1 configure the master configuration file of keepalibed

 

[root@Haproxy-1 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalivedvrrp_script chk_http_port {script "/etc/keepalived/check_haproxy.sh"interval 2global_defs {router_id LVS_DEVEL}vrrp_instance VI_1 {state MASTER interface eth0virtual_router_id 51 priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}track_script {chk_http_port}virtual_ipaddress {192.168.200.254 }}}

3.2.2 configure the keepalibed master configuration file for the second Haproxy Server

[root@Haproxy-2 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalivedvrrp_script chk_http_port {script "/etc/keepalived/check_haproxy.sh"interval 2weight 2global_defs {router_id LVS_DEVEL}vrrp_instance VI_1 {state BACKUP interface eth0virtual_router_id 51 priority 50advert_int 1authentication {auth_type PASSauth_pass 1111}track_script {chk_http_port}virtual_ipaddress {192.168.200.254 }}}

4. Configure the haproxy detection script on both machines.

[root@Haproxy-1 ~]# cat /etc/keepalived/check_haproxy.sh #!/bin/bashnum=`ps -C haproxy --no-header |wc -l`if [ $num -eq 0 ]then/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfgsleep 3if [ `ps -C haproxy --no-header |wc -l` -eq 0 ]then/etc/init.d/keepalived stopfifi[root@Haproxy-1 ~]# chmod +x /etc/keepalived/check_haproxy.sh[root@Haproxy-1 ~]# service keepalived start[root@Haproxy-2 ~]# service keepalived start

5.1 test the VIP address

[Root @ Haproxy-1 ~] # Ip addr show dev eth02: eth0: <BROADCAST, MULTICAST, UP, LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00: 0c: 29: cc: 18: a2 brd ff: ff inet 192.168.200.101/24 brd 192.168.200.255 scope global eth0 inet 192.168.200.254/32 scope global eth0 inet6 fe80: 20c: 29ff: fecc: 18a2/64 scope link valid_lft forever preferred_lft forever [root @ Haproxy-1 ~] #/Etc/init. d/keepalived stop keepalived: [OK] [root @ Haproxy-2 ~] # Ip addr show dev eth02: eth0: <BROADCAST, MULTICAST, UP, LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00: 0c: 29: fd: 8a: 4e brd ff: ff inet 192.168.200.102/24 brd 192.168.200.255 scope global eth0 inet 192.168.200.254/32 scope global eth0 inet6 fe80: 20c: 29ff: fefd: 8a4e/64 scope link valid_lft forever preferred_lft forever

5.2. Test Haproxy Health Check

[Root @ Haproxy-1 ~] # Service haproxy stopShutting down haproxy: [OK] [root @ Haproxy-1 ~] # Service haproxy statushaproxy (pid 59717) is running...

5.3 webpage test:
Open http: // 192.168.200.254 in a browser
Open a new browser and access http: // 192.168.200.254 again

The two access results can be verified as follows:
Server 192.168.200.103
Server 192.168.200.104

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.