0
点赞
收藏
分享

微信扫一扫

Centos7编译安装haproxy2.4.16

心存浪漫 2022-04-30 阅读 26
#当前系统版本
[root@centos7 ~]#lua -v
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio

#安装基础命令及编译依赖环境
[root@centos7 ~]#yum install gcc readline-devel -y
[root@centos7 ~]#tar xf lua-5.4.4.tar.gz -C /usr/local/src/
[root@centos7 ~]#cd /usr/local/src/lua-5.4.4/
[root@centos7 lua-5.4.4]#make linux test

#查看编译安装的版本
[root@centos7 lua-5.4.4]#src/lua -v
Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio

1编译安装HAProxy
#HAProxy 2.0以上版本编译参数:
[root@centos7 ~]#yum -y install gcc openssl-devel pcre-devel systemd-devel
[root@centos7 lua-5.4.4]# cd ~
[root@centos7 ~]#tar xf haproxy-2.4.16.tar.gz -C /usr/local/src、
[root@centos7 ~]#cd /usr/local/src/haproxy-2.4.16/

#查看安装方法
[root@centos7 haproxy-2.4.16]#less INSTALL
[root@centos7 haproxy-2.4.16]#less Makefile

#参考INSTALL文件进行编译安装
[root@centos7 haproxy-2.4.16]#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.4/src/ LUA_LIB=/usr/local/src/lua-5.4.4/src/
[root@centos7 haproxy-2.4.16]#make install PREFIX=/apps/haproxy
[root@centos7 haproxy-2.4.16]#ln -s /apps/haproxy/sbin/haproxy /usr/sbin/

#查看生成的文件
[root@centos7 haproxy-2.4.16]#tree /apps/haproxy/
/apps/haproxy/
├── doc
│ └── haproxy
│ ├── 51Degrees-device-detection.txt
│ ├── architecture.txt
│ ├── close-options.txt
│ ├── configuration.txt
│ ├── cookie-options.txt
│ ├── DeviceAtlas-device-detection.txt
│ ├── intro.txt
│ ├── linux-syn-cookies.txt
│ ├── lua.txt
│ ├── management.txt
│ ├── netscaler-client-ip-insertion-protocol.txt
│ ├── network-namespaces.txt
│ ├── peers.txt
│ ├── peers-v2.0.txt
│ ├── proxy-protocol.txt
│ ├── regression-testing.txt
│ ├── seamless_reload.txt
│ ├── SOCKS4.protocol.txt
│ ├── SPOE.txt
│ └── WURFL-device-detection.txt
├── sbin
│ └── haproxy
└── share
└── man
└── man1
└── haproxy.1

6 directories, 22 files

1.1验证HAProxy版本

#验证HAProxy版本:
[root@centos7 haproxy-2.4.16]#which haproxy
/usr/sbin/haproxy
[root@centos7 haproxy-2.4.16]#haproxy -v
HAProxy version 2.4.16-9d532c4 2022/04/29 - 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.16.html
Running on: Linux 3.10.0-1062.el7.x86_64 #1 SMP Wed Aug 7 18:08:02 UTC 2019 x86_64

#大写-V选项显示版本和帮助用法
[root@centos7 haproxy-2.4.16]# haproxy -v
HAProxy version 2.4.16-9d532c4 2022/04/29 - 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.16.html
Running on: Linux 3.10.0-1062.el7.x86_64 #1 SMP Wed Aug 7 18:08:02 UTC 2019 x86_64
[root@centos7 haproxy-2.4.16]# haproxy -V
HAProxy version 2.4.16-9d532c4 2022/04/29 - 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.16.html
Running on: Linux 3.10.0-1062.el7.x86_64 #1 SMP Wed Aug 7 18:08:02 UTC 2019 x86_64
Usage : haproxy [-f <cfgfile|cfgdir>]* [ -vdVD ] [ -n <maxconn> ] [ -N <maxpconn> ]
[ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ] [-- <cfgfile>*]
-v displays version ; -vv shows known build options.
-d enters debug mode ; -db only disables background mode.
-dM[<byte>] poisons memory with <byte> (defaults to 0x50)
-V enters verbose mode (disables quiet mode)
-D goes daemon ; -C changes to <dir> before loading files.
-W master-worker mode.
-Ws master-worker mode with systemd notify support.
-q quiet mode : don't display messages
-c check mode : only check config files and exit
-n sets the maximum total # of connections (uses ulimit -n)
-m limits the usable amount of memory (in MB)
-N sets the default, per-proxy maximum # of connections (0)
-L set local peer name (default to hostname)
-p writes pids of all children to this file
-de disables epoll() usage even when available
-dp disables poll() usage even when available
-dS disables splice usage (broken on old kernels)
-dG disables getaddrinfo() usage
-dR disables SO_REUSEPORT usage
-dL dumps loaded object files after config checks
-dr ignores server address resolution failures
-dV disables SSL verify on servers side
-dW fails if any warning is emitted
-dD diagnostic mode : warn about suspicious configuration statements
-sf/-st [pid ]* finishes/terminates old pids.
-x <unix_socket> get listening sockets from a unix socket
-S <bind>[,<bind options>...] new master CLI

1.2准备HAProxy启动文件

#创建service文件
[root@centos7 haproxy-2.4.15]#vim /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 /var/lib/haproxy/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
LimitNOFILE=100000

[Install]
WantedBy=multi-user.target

[root@centos7 haproxy-2.4.16]#systemctl daemon-reload

1.3配置文件

#创建自定义的配置文件
[root@centos7 haproxy-2.4.16]#mkdir /etc/haproxy
[root@centos7 haproxy-2.4.16]#vim /etc/haproxy/haproxy.cfg
global
maxconn 100000
chroot /apps/haproxy
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
#uid 99
#gid 99
user haproxy
group haproxy
daemon
#nbproc 4
#cpu-map 1 0
#cpu-map 2 1
#cpu-map 3 2
#cpu-map 4 3
pidfile /var/lib/haproxy/haproxy.pid
log 127.0.0.1 local2 info

defaults
option http-keep-alive
option forwardfor
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 web_port
bind 10.0.0.7:80
mode http
log global
server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5

1.4启动 haproxy

#准备socket文件目录
[root@centos7 haproxy-2.4.16]#mkdir /var/lib/haproxy

#设置用户和目录权限
[root@centos7 haproxy-2.4.16]#useradd -r -s /sbin/nologin -d /var/lib/haproxy haproxy
[root@centos7 haproxy-2.4.16]#systemctl enable --now haproxy

1.5验证 haproxy 状态

[root@centos7 haproxy-2.4.16]#systemctl status haproxy
● haproxy.service - HAProxy Load Balancer
Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: disabled)
Active: active (running) since 六 2022-04-30 19:07:40 CST; 9s ago
Process: 2481 ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q (code=exited, status=0/SUCCESS)
Main PID: 2484 (haproxy)
CGroup: /system.slice/haproxy.service
├─2484 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy...
└─2488 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy...

430 19:07:40 centos7.magedu.com systemd[1]: Starting HAProxy Load Balancer...
430 19:07:40 centos7.magedu.com systemd[1]: Started HAProxy Load Balancer.
430 19:07:40 centos7.magedu.com haproxy[2484]: [NOTICE] (2484) : New worker #1 (2488) forked
430 19:07:40 centos7.magedu.com haproxy[2484]: [WARNING] (2488) : Server web_port/web1 i...e.
430 19:07:40 centos7.magedu.com haproxy[2484]: [NOTICE] (2488) : haproxy version is 2.4...c4
430 19:07:40 centos7.magedu.com haproxy[2484]: [NOTICE] (2488) : path to executable is ...xy
430 19:07:40 centos7.magedu.com haproxy[2484]: [ALERT] (2488) : proxy 'web_port' has n...e!
Hint: Some lines were ellipsized, use -l to show in full.

[root@centos7 haproxy-2.4.16]#pstree -p |grep haproxy
|-haproxy(2484)---haproxy(2488)-+-{haproxy}(2489)
| |-{haproxy}(2490)
| `-{haproxy}(2491)

1.6查看haproxy的状态页面

 

#浏览器访问:http://10.0.0.7:9999/haproxy-status	用户名:haadmin 密码:123456
举报

相关推荐

0 条评论