ubuntu20.04编译安装HAproxy

一 环境准备

1.1 设置时间同步

root@node-01:~# apt -y install chrony
root@node-01:~# systemctl enable chrony

1.2 创建haproxy用户

root@node-01:~# groupadd -g 2021 haproxy
root@node-01:~# useradd -g haproxy -s /bin/false -u 2021 haproxy
root@node-01:~# id haproxy
uid=2021(haproxy) gid=2021(haproxy) groups=2021(haproxy)

二 安装haproxy

社区官网:https://www.haproxy.org/

2.1 下载haproxy

root@node-01:/opt# wget https://www.haproxy.org/download/2.4/src/haproxy-2.4.8.tar.gz

2.2 查看haproxy依赖

root@node-01:/opt# tar xf haproxy-2.4.8.tar.gz 
root@node-01:/opt# cd haproxy-2.4.8/
root@node-01:/opt/haproxy-2.4.8# cat INSTALL

2.3 安装haproxy依赖

root@node-01:~# apt -y install libpcre3-dev zlib1g-dev libsystemd-dev make gcc libssl-dev 

2.4 安装lua

官网:http://www.lua.org/start.html

2.4.1 下载lua

root@node-01:/opt# curl -R -O http://www.lua.org/ftp/lua-5.4.3.tar.gz

2.4.2 安装lua

root@node-01:/opt# tar xf lua-5.4.3.tar.gz 
root@node-01:/opt# cd lua-5.4.3/
root@node-01:/opt/lua-5.4.3# make linux test

2.5 安装haproxy

root@node-01:/opt# cd haproxy-2.4.8/
root@node-01:/opt/haproxy-2.4.8# make -j $(nproc) ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 USE_LUA=1 LUA_INC=/opt/lua-5.4.3/src/ LUA_LIB=/opt/lua-5.4.3/src/ PREFIX=/usr/local/haproxy
root@node-01:/opt/haproxy-2.4.8# make install PREFIX=/usr/local/haproxy

 2.6 验证版本

root@node-01:~# /usr/local/haproxy/sbin/haproxy -v
HAProxy version 2.4.8-d1f8d41 2021/11/03 - https://haproxy.org/
Status: long-term supported branch - will stop receiving fixes around Q2 2026.
Known bugs: http://www.haproxy.org/bugs/bugs-2.4.8.html
Running on: Linux 5.4.0-89-generic #100-Ubuntu SMP Fri Sep 24 14:50:10 UTC 2021 x86_64

三 准备haproxy.cfg

点击查看代码
root@node-01:~# cat /usr/local/haproxy/haproxy.cfg 
global
    maxconn 100000
    chroot /usr/local/haproxy
    stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
    user  haproxy
    group haproxy
    daemon
    pidfile /var/lib/haproxy/haproxy.pid
    log 127.0.0.1 local2 info

defaults
    option http-keep-alive
    maxconn 100000
    mode http
    timeout connect 300000ms
    timeout client  300000ms
    timeout server  300000ms

listen stats
    mode http
    bind 0.0.0.0:9999
    stats enable
    log global
    stats uri     /haproxy-status
    stats auth    haadmin:123456

listen  k8s-6443
    bind 192.168.174.20:6443
    mode tcp
    log global
    server k8s-master-01  192.168.174.100:6443  check inter 3s fall 2 rise 5
    server k8s-master-02  192.168.174.101:6443  check inter 3s fall 2 rise 5
    server k8s-master-03  192.168.174.102:6443  check inter 3s fall 2 rise 5

四 准备haproxy.service

root@node-01:~# cat /lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

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

[Install]
WantedBy=multi-user.target

五 启动haproxy服务

root@node-01:~# mkdir -pv /var/lib/haproxy
root@node-01:~# chown -R haproxy.haproxy /var/lib/haproxy/
root@node-01:~# systemctl start haproxy
root@node-01:~# systemctl status haproxy
● haproxy.service - HAProxy Load Balancer
     Loaded: loaded (/lib/systemd/system/haproxy.service; disabled; vendor preset: enabled)
     Active: active (running) since Wed 2021-11-10 13:15:08 CST; 4s ago
    Process: 20635 ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg -c -q (code=exited, status=0/SUCCESS)
   Main PID: 20651 (haproxy)
      Tasks: 3 (limit: 2245)
     Memory: 27.9M
     CGroup: /system.slice/haproxy.service
             ├─20651 /usr/local/haproxy/sbin/haproxy -Ws -f /usr/local/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
             └─20653 /usr/local/haproxy/sbin/haproxy -Ws -f /usr/local/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid

Nov 10 13:15:08 node-01 systemd[1]: Starting HAProxy Load Balancer...
Nov 10 13:15:08 node-01 systemd[1]: Started HAProxy Load Balancer.
Nov 10 13:15:08 node-01 haproxy[20651]: [NOTICE]   (20651) : New worker #1 (20653) forked
root@node-01:~# systemctl enable haproxy
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /lib/systemd/system/haproxy.service.

六 验证haproxy端口

root@node-01:~# ss -tnlp
State            Recv-Q            Send-Q                       Local Address:Port                       Peer Address:Port           Process                                              
LISTEN           0                 4096                               0.0.0.0:9999                            0.0.0.0:*               users:(("haproxy",pid=20653,fd=6))   

七 查看haproxy状态页

八 添加内核参数

在 Linux 如果需要绑定本机不存在的 IP, 例如在 HAproxy 及 Nginx 可能会用到, 需要开启 Kernel 的参数 net.ipv4.ip_nonlocal_bind.

root@node-01:~# echo "net.ipv4.ip_nonlocal_bind = 1" >> /etc/sysctl.conf 
root@node-01:~# sysctl -p

九 参考文档

http://cbonte.github.io/haproxy-dconv/2.4/snapshot/configuration.html

posted @ 2021-11-10 13:21  小吉猫  阅读(631)  评论(0编辑  收藏  举报