haproxy配置

1.安装haproxy

安装依赖包

dnf -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel

创建用户组

useradd -r -M -s /sbin/nologin haproxy

解压

[root@localhost ~]# tar xf haproxy-v2.3.0.tar.gz 
[root@localhost ~]# cd haproxy-2.3.0/

快速安装

[root@localhost haproxy-2.3.0]# make clean
[root@localhost haproxy-2.3.0]#make -j $(grep 'processor' /proc/cpuinfo |wc -l)  \

TARGET=linux-glibc \

USE_OPENSSL=1 \
USE_ZLIB=1  \
USE_PCRE=1  \
USE_SYSTEMD=1

...过程略
[root@localhost haproxy-2.3.0]# make install PREFIX=/usr/local/haproxy
#环境

[root@localhost ~]# echo 'export PATH=/usr/local/haproxy/sbin:$PATH' > /etc/profile.d/haproxy.sh

[root@localhost ~]# source /etc/profile.d/haproxy.sh
[root@localhost ~]# which haproxy
/usr/local/haproxy/sbin/haproxy

 

配置各个内核的参数

mkdir /etc/haproxy

cat > /etc/haproxy/haproxy.cfg <<EOF
#--------------全局配置----------------
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 172.16.103.130:80 check inter 2000 fall 5
    #server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
EOF

haproxy.service文件编写

cat > /usr/lib/systemd/system/haproxy.service <<EOF
[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 $MAINPID

[Install]
WantedBy=multi-user.target
EOF

#重新加载
systemctl daemon-reload
#开机自启
systemctl eneble --now haproxy

启用日志

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

local0.*                        /var/log/haproxy.log

#重启服务
systemctl restart rsyslog
 

重启服务

systemctl restart haproxy
ss -antl

 

posted @ 2021-06-29 17:51  #44  阅读(71)  评论(0)    收藏  举报