Centos7 安装配置haproxy

记录一下,全程照着神仙教程做的,感谢CSDN的大佬带路!

附上来源链接以示感谢:https://blog.csdn.net/mushuangpanny/article/details/126354803

首先部署了NTP,bbr,yum update一下,接下来就开始做下面的步骤了

wget https://github.com/haproxy/haproxy/archive/refs/tags/v2.7-dev0.tar.gz  先下载
yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel  安装依赖
tar xf v2.7-dev0.tar.gz 解压缩
useradd -r -M -s /sbin/nologin haproxy  创建用户
cd haproxy-2.7-dev0/  进入目录
make clean
make -j $(grep 'processor' /proc/cpuinfo |wc -l)  \
> TARGET=linux-glibc  \
> USE_OPENSSL=1  \
> USE_ZLIB=1  \
> USE_PCRE=1  \
> USE_SYSTEMD=1
make install PREFIX=/usr/local/haproxy  编译、安装软件完成

下面开始验证安装
cd /usr/local/  进入目录
ll  看是否有haproxy
cd haproxy/ 
ll  看是否有sbin
cd sbin/
ll  看是否有haproxy

vi /etc/sysctl.conf 配置系统参数
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
sysctl -p

mkdir -p /etc/haproxy  创建配置目录
cd /etc/haproxy  进入目录
vi haproxy.cfg  创建并配置配置文件
#--------------全局配置----------------
global
    log 127.0.0.1 local0  info
    #log loghost local0 info
    maxconn 20480
#chroot /usr/local/haproxy
    pidfile /var/run/haproxy.pid
    #maxconn 4000
    user haproxy
    group haproxy
    daemon
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode http
    log global
    option dontlognull
    option httpclose
    option httplog
    #option forwardfor
    option redispatch
    balance roundrobin
    timeout connect 10s
    timeout client 10s
    timeout server 10s
    timeout check 10s
    maxconn 60000
    retries 3
#--------------统计页面配置------------------
listen admin_stats
    bind 0.0.0.0:8189
    stats enable
    mode http
    log global
    stats uri /haproxy_stats
    stats realm Haproxy\ Statistics
    stats auth admin:admin
    #stats hide-version
    stats admin if TRUE
    stats refresh 30s
#---------------web设置-----------------------
listen webcluster
    bind 0.0.0.0:80
    mode http
    #option httpchk GET /index.html
    log global
    maxconn 3000
    balance roundrobin
    cookie SESSION_COOKIE insert indirect nocache
    server web01 192.168.232.134:80 check inter 2000 fall 5
    server web02 192.168.232.128:80 check inter 2000 fall 5
    #server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5

vi /etc/rsyslog.conf  配置日志
local0.*       /var/log/haproxy.log  加入这条配置
systemctl enable --now rsyslog  开启日志
systemctl restart rsyslog  重启日志


vi /usr/lib/systemd/system/haproxy.service  配置编写
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg   -c -q
ExecStart=/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg  -p /var/run/haproxy.pid
ExecReload=/bin/kill -USR2 

[Install]
WantedBy=multi-user.target

systemctl daemon-reload  重新载入

systemctl enable --now  haproxy  启动haproxy
ss -antl  查看端口状态

Leave a comment

Your email address will not be published. Required fields are marked *