2019.10.9 build high-availability load balancing cluster based Haproxy + Keepalived

Haproxy + keepalived used to install on the virtual machine 111,112,113,114,111,112, 113, 114 installed on nginx

1, 113 is first mounted on Nginx

[root@localhost ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel

[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# rz
z waiting to receive.**B0100000023be50
[root@localhost ~]# tar xf nginx-1.15.9.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.15.9/
[root@localhost nginx-1.15.9]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install

[root@localhost nginx-1.15.9]# cd /usr/local/nginx/html/
[root@localhost html]# echo "server 192.168.200.113" > index.html
[root@localhost html]# /usr/local/nginx/sbin/nginx
[root@localhost html]# netstat -anpt |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7681/nginx: master

Installation Nginx2, with Nginx1 build is the same way. And Nginx1 only difference is:

[@ localhost the root HTML] # echo "Server 192.168.200.114"> index.html
2, mounted on Haproxy 111, configured with the same two machines:

[root@localhost ~]#  yum -y install gcc gcc-c++ make pcre-devel bzip2-devel

[root@localhost ~]# rz
z waiting to receive.**B0100000023be50
[root@localhost ~]# tar xf haproxy-1.4.24.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/haproxy-1.4.24/
[root@localhost haproxy-1.4.24]# make TARGET=linux26 && make install

Haproxy server configuration
build configuration directory and file of haproxy

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

 Haproxy modify configuration files

[root@localhost haproxy-1.4.24]# vim /etc/haproxy/haproxy.cfg

# this config needs haproxy-1.1.28 or haproxy-1.2.1
 
global
    log 127.0.0.1   local0
    log 127.0.0.1   local1 notice
    #log loghost    local0 info
    maxconn 4096
    #chroot /usr/share/haproxy
    uid 99
    gid 99
    daemon
    #debug
    #quiet
 
defaults
    log global
    mode    http
    option  httplog
    option  dontlognull
    retries 3
    #redispatch
    maxconn 2000
    contimeout  5000
    clitimeout  50000
    srvtimeout  50000
 
listen  web-cluster 0.0.0.0:80
    option httpchk GET /index.html
    balance roundrobin
    server  inst1 192.168.200.113:80 check inter 2000 fall 3
    server  inst2 192.168.200.114:80 check inter 2000 fall 3

Create a custom startup script

[root@localhost haproxy-1.4.24]# cp /usr/src/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy-1.4.24]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@localhost haproxy-1.4.24]# chmod +x /etc/init.d/haproxy
[root@localhost haproxy-1.4.24]# /etc/init.d/haproxy start
Starting haproxy (via systemctl): [ 确定 ]

Client Access test:

 

 

 

 

 

 

 

3, compile and install keepalived Service

[root@localhost haproxy-1.4.24]# cd
[root@localhost ~]# yum -y install keepalived

Configuration keepalibed main configuration file

[root@localhost ~]# vim /etc/keepalived/keepalived.conf 

! Configuration File for keepalived
 
vrrp_script chk_http_port {
script "/etc/keepalived/check_haproxy.sh"
interval 2
 
global_defs {
router_id LVS_DEVEL
}
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.200.254
}
}
}

 4, are configured on both machines haproxy detection script

[root@localhost ~]# vim /etc/keepalived/check_haproxy.sh

#!/bin/bash
num=`ps -C haproxy --no-header |wc -l`
if [ $num -eq 0 ]
then
    /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
    sleep 3
    if [ `ps -C haproxy --no-header |wc -l` -eq 0 ]
    then
        /etc/init.d/keepalived stop
    fi
fi

[root@localhost ~]# chmod +x /etc/keepalived/check_haproxy.sh
[root@localhost ~]# service keepalived start

Test VIP address

[root@localhost ~]# ip a

[root@localhost ~]# /etc/init.d/keepalived stop                          //停止 keepalived

Test Haproxy health check

[root@localhost ~]# service haproxy stop

[root@localhost ~]# service haproxy status

Go to web pages for testing:

 

Guess you like

Origin www.cnblogs.com/990624lty-jhc/p/11644377.html