2019.10.9 基于Haproxy+Keepalived构建高可用负载均衡集群

用到虚拟机111、112、113、114,111、112上安装Haproxy+keepalived,113、114上安装nginx

1、 首先在113上安装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

安装Nginx2, 同Nginx1搭建方式是一样的。与Nginx1唯一不同的是:

[root@localhost html]# echo "server 192.168.200.114" > index.html
2、在111、112上安装Haproxy, 两台机器配置一致:

[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服务器配置
建立haproxy的配置目录及文件

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

 haproxy的配置文件的修改

[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

创建自启动脚本

[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): [ 确定 ]

客户端访问测试:

 

 

3、编译安装keepalived服务

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

配置keepalibed 主配置文件

[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、两台机器上都配置haproxy检测脚本

[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

测试VIP地址

[root@localhost ~]# ip a

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

测试Haproxy健康检查

[root@localhost ~]# service haproxy stop

[root@localhost ~]# service haproxy status

去网页进行测试:

猜你喜欢

转载自www.cnblogs.com/990624lty-jhc/p/11644377.html