环境:

haproxy192.168.76.4
web1192.168.76.6
web2192.168.76.7

一.安装haproxy

1.haproxy下载压缩包地址mirrors.huaweicloud.com/home

 

 下载tar包

wget -c https://repo.huaweicloud.com/haproxy/2.4/src/haproxy-2.4.8.tar.gz

下载lua

curl -R -O https://www.lua.org/ftp/lua-5.4.3.tar.gz

安装gcc gcc-c++ make

yum install gcc gcc-c++ make -y

解压lua

    tar xf lua-5.4.3.tar.gz -C /usr/local/src/
    cd /usr/local/src/lua-5.4.3/

make all test 并将lua复制到/usr/local/lua

make all test
cp src/lua /usr/bin/lua

查看lua版本  

先切换到lua文件夹中
cd /usr/local/src/lua-5.4.3/
src/lua -v

解压haproxy

tar xf haproxy-2.4.8.tar.gz -C /usr/local/src/

编译安装

yum -y install gcc openssl-devel pcre-devel systemd-devel

cd  /usr/local/src/haproxy-2.4.8

make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_LUA=1 LUA_INC=/usr/local/src/lua-5.4.3/src/ LUA_LIB=/usr/local/src/lua-5.4.3/src/


make install PREFIX=/usr/local/haproxy

用tree查看haproxy文件结构

 软连接

ln -sv /usr/local/haproxy/sbin/haproxy /usr/sbin/haproxy

查看haproxy版本

haproxy -v

编写启动脚本   /usr/lib/systemd/system/haproxy.service

[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
 
[Service]
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /usr/local/haproxy/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
 
[Install]
WantedBy=multi-user.target

重新加载systemctl daemon-reload

创建目录,编写配置文件

mkdir /etc/haproxy
cd /etc/haproxy

编写配置文件  vim  haproxy.cfg   (这个文件是在/etc/haproxy下)

global
        maxconn         10000
        stats socket    /var/run/haproxy.stat mode 600 level admin
        log             127.0.0.1 local0
       # uid             200
       # gid             200
        user             haproxy
        group           haproxy
        chroot          /usr/local/haproxy
        daemon
 
defaults
        mode http
        option httplog
        log global
        timeout client 1m
        timeout server 1m
        timeout connect 10s
        timeout http-keep-alive 2m
        timeout queue 15s
        timeout tunnel 4h  # for websocket
 
listen app1
   # this is the address and port we'll listen to, the ones to aim the
   # load generators at
   bind :80
 
   # create a certificate and uncomment this for SSL
   # bind :8443 ssl crt my-cert.pem alpn h2,http/1.1
 
   # Put the server's IP address and port below
   server web1 192.168.76.6:80    #真实web服务器
   server web2 192.168.76.7:80    #真实web服务器

创建用户haproxy设置未nologin

useradd -r -s /sbin/nologin -d /usr/local/haproxy/ haproxy

启动haproxy     systemctl start haproxy

二.配置web服务器

echo "web1:192.168.76.6" > /var/www/html/index.html

echo "web2:192.168.76.7" > /var/www/html/index.html

不能启动haproxy原因可能是:

1.配置文件有问题

2.启动文件有问题

3.防火墙没关,selinux没禁用

4.用户未创建

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐