haproxy+keepalived achieve high Availability load balancing (instance configuration) _linux

Source: Internet
Author: User
Tags auth haproxy

Author: split_two

Environment four machines are RedHat4.8 (64-bit) version:

IP address Use
192.168.5.55 MASTER
192.168.2.73 BACKUP
192.168.5.54 Load A
192.168.5.57 Load B
192.168.2.100 VIP

1. Install Haproxy on Master

Copy Code code as follows:

wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.20.tar.gz/root

Extract to current directory
Tar zxvf haproxy-1.3.20.tar.gz
Go to this directory
CD haproxy-1.3.20
Compile and install, install to/usr/local directory, but after installing this directory does not have Haproxy folder, I am also very strange!
Make target=linux26 prefix=/usr/local/haproxy Install
But the results will prompt the following information

Install-d/usr/local/sbin
Install Haproxy/usr/local/sbin
Install-d/usr/local/share/man/man1
Install-m 644 Doc/haproxy.1/usr/local/share/man/man1
Install-d/usr/local/doc/haproxy
For x in configuration architecture Haproxy-en haproxy-fr; Do \
Install-m 644 doc/$x. Txt/usr/local/doc/haproxy; \
Done

Prompts you to start Haproxy in the Sbin directory and the rest in the doc directory
Access to the Haproxy directory
Cd/usr/local/doc/haproxy
Create a new haproxy master configuration file

Copy Code code as follows:

VI haproxy.cfg
Global
Log 127.0.0.1 Local0
Maxconn 4096
Chroot/usr/local/doc/haproxy
UID 501
GID 501
Daemon
Nbproc 1
Pidfile/usr/local/doc/haproxy/haproxy.pid
Defaults
Log 127.0.0.1 Local3
Mode http
Option Httplog
Option Httpclose
Option Dontlognull
Option Forwardfor
Option Redispatch
Retries 2
Maxconn 2000
Balance Roundrobin
Stats Uri/haproxy-status View Status
Stats hide-version Hide Haproxy version
Stats Realm gemini\ haproxy View status requires password
Stats auth admin:admin username and password
Contimeout 5000
Clitimeout 50000
Srvtimeout 50000
Listen Web_proxy 192.168.2.100:80
Server Web1 192.168.5.54:8080 Cookie app1inst1 check Inter rise 2 Fall 5
Server web2 192.168.5.57:8080 Cookie app1inst2 check Inter rise 2 Fall 5

2, master installation keepalived
Tar zxvf keepalived-1.1.15.tar.gz
Vi/usr/src/kernels/2.6.9-89.el-smp-x86_64/include/linux/types.h
The following two lines will be commented out, otherwise the compilation will be wrong, with my version of the system has a relationship, you may not!

Copy Code code as follows:

/*
typedef __u16 __bitwise __SUM16;
typedef __u32 __bitwise __wsum;
*/
CD keepalived-1.1.15
./configure
Make
Make install

To start the keepalived as a system service

cp/usr/local/etc/rc.d/init.d/keepalived/etc/init.d/
cp/usr/local/etc/sysconfig/keepalived/etc/sysconfig/
mkdir/etc/keepalived/
cp/usr/local/etc/keepalived/keepalived.conf/etc/keepalived/
cp/usr/local/sbin/keepalived/usr/sbin/
Vi/etc/keepalived/keepalived.conf contents are as follows:

Copy Code code as follows:

! Configuration File for Keepalived
Global_defs {
router_id Lva_devel
}
Vrrp_script Chk_http_port {
Script "/root/check_haproxy.sh"
Interval 2
Weight 2
}
Vrrp_instance Vi_1 {
State MASTER
Interface eth0
VIRTUAL_ROUTER_ID 51
Priority 100
Advert_int 1
Authentication {
Auth_type Pass
Auth_pass 1111
}
Track_script {
Chk_http_port
}
virtual_ipaddress {
192.168.2.100
}
}

A script check_haproxy.sh is called above, and the contents are as follows:
Copy Code code as follows:

#!/bin/bash
A= ' Ps-c haproxy--no-header | Wc-l '
If [$A-eq 0];then
/usr/local/sbin/haproxy-f/usr/local/doc/haproxy/haproxy.cfg
echo "Haproxy start"
Sleep 3
If [' Ps-c haproxy--no-header | wc-l '-eq 0];then
/etc/init.d/keepalived stop
echo "Keepalived Stop"
Fi
Fi

3, backup on the installation of Haproxy, the steps are not detailed, and master above the same.
Haproxy.conf content is as follows
Copy Code code as follows:

Global
Log 127.0.0.1 Local0
Maxconn 4096
Chroot/usr/local/doc/haproxy
UID 501
GID 501
Daemon
Nbproc 1
Pidfile/usr/local/doc/haproxy/haproxy.pid
Defaults
Log 127.0.0.1 Local3
Mode http
Option Httplog
Option Httpclose
Option Dontlognull
Option Forwardfor
Option Redispatch
Retries 2
Maxconn 2000
Balance Roundrobin
Stats Uri/haproxy-status
Stats Hide-version
Stats Realm Gemini\ Haproxy
Stats Auth Admin:admin
Contimeout 5000
Clitimeout 50000
Srvtimeout 50000
Listen Web_proxy 192.168.2.100:80
Server Web1 192.168.5.54:8080 Cookie app1inst1 check Inter rise 2 Fall 5
Server web2 192.168.5.57:8080 Cookie app1inst2 check Inter rise 2 Fall 5

4, Backup installation keepalived, steps are not introduced, keepalived.conf file content on two changes, red font marked out
Copy Code code as follows:

! Configuration File for Keepalived
Global_defs {
router_id Lva_devel
}
Vrrp_script Chk_http_port {
Script "/root/check_haproxy.sh"
Interval 2
Weight 2
}
Vrrp_instance Vi_1 {
State BACKUP
Interface eth0
VIRTUAL_ROUTER_ID 51
Priority 99
Advert_int 1
Authentication {
Auth_type Pass
Auth_pass 1111
}
Track_script {
Chk_http_port
}
virtual_ipaddress {
192.168.2.100
}
}

Invoke Script check_haproxy.sh content:
Copy Code code as follows:

#!/bin/bash
A= ' IP A | grep 192.168.2.100 | Wc-l '
b= ' Ps-ef | grep Haproxy | Grep-v grep | awk ' {print $} '
If [$A-gt 0];then
/usr/local/sbin/haproxy-f/usr/local/doc/haproxy/haproxy.cfg
Else
Kill-9 $B
Fi

5, two load machines I will not introduce more, with the system with the Apache
Log on 192.168.5.54:
Echo ' This is 192.168.5.54! ' >/var/www/html/index.html
The listening port for modifying the/etc/httpd/conf/httpd.conf file is 8080
Sed-i ' S/listen 80/listen 8080/g '/etc/httpd/conf/httpd.conf
/ETC/INIT.D/HTTPD start
Log on 192.168.5.57:
Echo ' Hello,this is 192.168.5.57! ' >/var/www/html/index.html
The listening port for modifying the/etc/httpd/conf/httpd.conf file is 8080
Sed-i ' S/listen 80/listen 8080/g '/etc/httpd/conf/httpd.conf
/ETC/INIT.D/HTTPD start

6. Test steps

Start the keepalived service on master, and then start the keepalived service on backup.
Determine if there is a 192.168.2.100 address on Master, view it with IP a!
Then manually kill the Haproxy process on master to see if the process can be recovered immediately?
Stop the keepalived service on master to confirm that backup takes over the VIP address?
Finally, start the keepalived service on master and then confirm that Master will take over the VIP address again?
Summary: I have no problem with this test, if you have any questions, please give me a message!

Original: http://blog.chinaunix.net/uid-23916356-id-3387261.html

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.