HAProxy + Keepalived dual-host high availability solution in Linux
Keepalived is used to detect the status of a web server. If a web server crashes or fails to work, Keepalived will detect it and remove the faulty web server from the system, when the web server is working normally, Keepalived automatically adds the web server to the server group. All these tasks are completed automatically without manual interference. All you need to do is to manually repair the faulty web server.
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 the 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.
System Environment: CenOS 6.5x86 _ 64 Desktop install set selinux and iptables to disabled
Mainly used software:
Haproxy-1.5.14.tar.gz
Keepalived-1.1.17.tar.gz
Nginx-1.7.8.tar.gz
Figure 1 shows the basic architecture:
Figure 2 allocates IP addresses.
1. The installation process is keepalived on the two HA machines:
# Tar-zxvf keepalived-1.2.17.tar.gz
# Ln-s/usr/src/kernels/2.6.18-128. el5-i686 // usr/src/linux
# Cd keepalived-1.2.17
[Root @ test25 keepalived-1.2.17] #./configure -- with-kernel-dir =/usr/src/kernels/2.6.32-279. el6.x86 _ 64/
# Make
[Root@test25keepalived-1.2.17] # make install
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. conf/etc/keepalived/
Cp/usr/local/sbin/keepalived/usr/sbin/
Modify the configuration file:
Master:
# Vim/etc/keepalived. conf
# Catkeepalived. conf
! ConfigurationFile for keepalived
Vrrp_scriptcheck_haproxy {
Script "/etc/keepalived/check_haproxy.sh"
Interval 2
Weight 2
}
Global_defs {
Notification_email {
Root @ localhost
}
Notification_email_fromkeepalived@server62.example.com
Smtp_server 127.0.0.1
Smtp_connect_timeout 30
Router_id LVS_DEVEL
}
Vrrp_instanceVI_1 {
State MASTER
Interface eth0
Virtual_router_id 151
Priority100
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass 1111
}
Virtual_ipaddress {
192.168.1.201
}
Track_script {
Check_haproxy
}
}
BACKUP:
# Catkeepalived. conf
! ConfigurationFile for keepalived
Vrrp_scriptcheck_haproxy {
Script "/etc/keepalived/check_haproxy.sh"
Interval 2
Weight 2
}
Global_defs {
Notification_email {
Root @ localhost
}
Notification_email_fromkeepalived@server63.example.com
Smtp_server 127.0.0.1
Smtp_connect_timeout 30
Router_id LVS_DEVEL
}
Vrrp_instanceVI_1 {
State BACKUP
Interface eth0
Virtual_router_id 151
Priority 50
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass 1111
}
Virtual_ipaddress {
192.168.1.201
}
Track_script {
Check_haproxy
}
}
Host:
# Vi/etc/keepalived/check_haproxy.sh
#! /Bin/bash
A = 'ps-C haproxy -- no-header | wc-l'
If [$ A-eq 0]; then
/Usr/local/haproxy/sbin/haproxy-f/usr/local/haproxy/conf/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
Backup Machine:
#! /Bin/bash
A = 'IP a | grep 10.2.32.201 | wc-l'
B = 'ps-ef | grep haproxy | grep-v grep | awk '{print $2 }''
If [$ A-gt 0]; then
/Usr/local/haproxy/sbin/haproxy-f/usr/local/haproxy/conf/haproxy. cfg
Else
Kill-9 $ B
Fi
# Run chmod 755/etc/keepalived/check_haproxy.sh on the two machines respectively.
Haproxy installation (both master and backup are the same ):
# Tar-zxvf haproxy-1.5.14.tar.gz
# Cd haproxy-1.5.14
# Make TARGET = linux26 PREFIX =/usr/local/haproxy
# Makeinstall PREFIX =/usr/local/haproxy
# Cd/usr/local/haproxy/
# Mkdir conf
# Mkdir logs
# Vi haproxy. cfg
Global
Log127.0.0.1 local0
Log127.0.0.1 local1 notice
Maxconn 4096
Chroot/usr/local/haproxy
Uid 99
Gid 99
Daemon
# Debug
# Quiet
Nbproc 1
Pidfile/usr/local/haproxy/logs/haproxy. pid
Ults
Log global
Mode http
Option httplog
Option dontlognull
Retries 3
# Redispatch
Maxconn 65535
Optionredispatch
Timeout connect5000
Timeout client50000
Timeout server50000
Stats uri/haproxy
Stats auth admin: admin
Listen www.bkjia.com *: 80
Cookie SERVERID rewrite
Mode http
Balance roundrobin
Option httpchkHEAD/index.html HTTP/1.0
Server web1 10.2.32.99: 80 cookie app1inst1 weight 5 check inter 2000 rise 2 fall 5
Server web2 10.2.32.110: 80 cookie app1inst2 weight 3 check inter 2000 rise 2 fall 5
2. Start the master node and slave node on both servers:
/Etc/init. d/keepalivedstart (if haproxy is not started before, this command will automatically start haproxy)
[Root @ test100 haproxy] # ps-ef | grephaprox
Root 30344 1 0 11: 43? At 00:00:00/bin/bash/etc/keepalived/check_haproxy.sh
Nobody 30349 1 0 11: 43? 00:00:00/usr/local/haproxy/sbin/haproxy-f/usr/local/haproxy/conf/haproxy. cfg
[Root @ test100 haproxy] # ps-ef | grepkeepali
Root 30337 1 0 11: 43? 00:00:00 keepalived-D
Root 30339 30337 1? 00:00:00 keepalived-D
Root 30340 30337 1? 00:00:00 keepalived-D
Root 30372 2876 0 00:00:00 pts/0 grep keepali
Iii. test:
1. ip address ad sh | grep 10.2.32
MASTER:
Inet10.2.32.100/24 brd 10.2.32.255 scope global eth0
Inet10.2.32.201/32 scope global eth0
Backup:
Inet10.2.32.101/24 brd 10.2.32.255 scope global eth0
2. Stop haproxy on the master. keepalived will automatically start it again after 3 seconds.
3. Stop the master keepalived and the slave will take over the service immediately.
Standby: ip ad sh | grep 10.2.32
Inet10.2.32.101/24 brd 10.2.32.255 scope global eth0
Inet10.2.32.201/32 scope global eth0
4. Test through a browser
Build a WEB server using Nginx
Build a Web server based on Linux6.3 + Nginx1.2 + PHP5 + MySQL5.5
Performance Tuning for Nginx in CentOS 6.3
Configure Nginx to load the ngx_pagespeed module in CentOS 6.3
Install and configure Nginx + Pcre + php-fpm in CentOS 6.4
Nginx installation and configuration instructions
Haproxy + Keepalived build Weblogic high-availability server Load balancer Cluster
Keepalived + HAProxy configure high-availability Load Balancing
Haproxy + Keepalived + Apache configuration notes in CentOS 6.3
Haproxy + KeepAlived WEB Cluster on CentOS 6
Haproxy + Keepalived build high-availability Load Balancing
Configure an HTTP Load balancer using HAProxy
For details about HAproxy, click here
HAproxy: click here
This article permanently updates the link address: