haproxy+keepalived implements the highly available load balancing of instance configuration
- 2020-05-06 12:09:53
- OfStack
Author: split_two
Environment all four machines are RedHat 4.8 (64-bit) version:
IP address
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
wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.20.tar.gz /root
Unzip to the 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 there is no haproxy folder under this directory, I am also surprised!
make TARGET=linux26 prefix=/usr/local/haproxy install
But the result will prompt the following message,
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
Prompt you to launch haproxy in the sbin directory and the rest in the doc directory
Go to haproxy directory
cd /usr/local/doc/haproxy
Create a new haproxy master profile,
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 Check the status
stats hide-version hidden haproxy version
stats realm Gemini\ Haproxy A password is required to view the status
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 2000 rise 2 fall 5
server web2 192.168.5.57:8080 cookie app1inst2 check inter 2000 rise 2 fall 5
2. MASTER install 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
Comment out the following two lines, otherwise the compilation will be wrong, with my version of the system, you may not!
/*
typedef __u16 __bitwise __sum16;
typedef __u32 __bitwise __wsum;
*/
cd keepalived-1.1.15
./configure
make
make install
Start 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 content is 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, which reads:
#!/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. Install haproxy on BACKUP, the steps will not be described in detail, which is the same as MASTER above.
haproxy.conf reads
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 2000 rise 2 fall 5
server web2 192.168.5.57:8080 cookie app1inst2 check inter 2000 rise 2 fall 5
4. Install keepalived on BACKUP, and the steps are not introduced much. keepalived
! 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
}
}
Call script check_haproxy.sh content:
#!/bin/bash
A=`ip a | grep 192.168.2.100 | wc -l`
B=`ps -ef | grep haproxy | grep -v grep | awk '{print $2}'`
if [ $A -gt 0 ];then
/usr/local/sbin/haproxy -f /usr/local/doc/haproxy/haproxy.cfg
else
kill -9 $B
fi
5. I won't introduce the two load machines, but apache
comes with the system Login 192.168.5.54 operation:
echo 'this is 192.168.5.54!' > /var/www/html/index.html
Modify/etc/httpd/conf/httpd. Listen on port 8080
conf file sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
/etc/init.d/httpd start
Login on 192.168.5.57 operation:
echo 'Hello,This is 192.168.5.57!' > /var/www/html/index.html
Modify/etc/httpd/conf/httpd. Listen on port 8080
conf file sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
/etc/init.d/httpd start
6. Test procedure
Start the keepalived service on MASTER, and then the keepalived service on BACKUP.
Make sure there is 192.168.2.100 address on MASTER, check with ip a!
Then manually kill the haproxy process on MASTER to see if you can immediately restore the process?
Discontinue the keepalived service on MASTER and verify that BACKUP has taken over the VIP address?
Finally, start the keepalived service on MASTER and confirm whether MASTER will take over the VIP address again.
Summary: I have no problem with this test, if there is a problem please give me a message! Original text: http: / / blog. chinaunix. net/uid - 23916356 - id - 3387261. html