haproxy编译安装
CentOS7 之前版本自带的lua版本比较低并不符合HAProxy要求的lua最低版本(5.3)的要求,因此需 要编译安装较新版本的lua环境,然后才能编译安装HAProxy,过程如下:
#当前系统版本 [root@node1 ~]# lua -v Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio #安装基础命令及编译依赖环境 [root@node1 ~]# yum install gcc readline-devel [root@node1 ~]# wget http://www.lua.org/ftp/lua-5.3.5.tar.gz [root@node1 ~]# tar xf lua-5.3.5.tar.gz -C /usr/local/src [root@node1 ~]# cd /usr/local/src/lua-5.3.5 [root@node1 lua-5.3.5]# make linux test #查看编译安装的版本 [root@node1 lua-5.3.5]# src/lua -v Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio [root@localhost haproxy-2.5.10]# cp /usr/local/src/lua-5.3.5/src/lua /usr/bin/lua cp: overwrite ‘/usr/bin/lua’? y [root@localhost haproxy-2.5.10]# lua -v Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio
编译安装haproxy
[root@node1 ~]# yum -y install gcc openssl-devel pcre-devel systemd-devel [root@node1 ~]# tar xf haproxy-2.2.2.tar.gz -C /usr/local/src/ [root@node1 ~]# cd /usr/local/src/haproxy-2.2.2/ [root@node1 haproxy-2.2.9]# cat README [root@node1 haproxy-2.2.9]# cat INSTALL #参考INSTALL文件进行编译安装 [root@centos7 haproxy-2.2.9]# 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.3.5/src/ LUA_LIB=/usr/local/src/lua-5.3.5/src/ [root@centos7 haproxy-2.2.9]# make install PREFIX=/apps/haproxy [root@centos7 haproxy-2.2.9]# ln -s /apps/haproxy/sbin/haproxy /usr/sbin/ # 查看版本 [root@localhost haproxy-2.5.10]# haproxy -v HAProxy version 2.5.10-4b0891f 2022/12/05 - https://haproxy.org/ Status: stable branch - will stop receiving fixes around Q1 2023. Known bugs: http://www.haproxy.org/bugs/bugs-2.5.10.html Running on: Linux 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64
编写启动脚本
[root@node1 ~]# cat /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 [Install] WantedBy=multi-user.target
配置文件
[root@node1 ~]# mkdir /etc/haproxy [root@node1 ~]# 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 [root@node1 ~]# mkdir /var/lib/haproxy [root@node1 ~]# useradd -r -s /sbin/nologin -d /var/lib/haproxy haproxy [root@node1 ~]# systemctl enable --now haproxy
haproxy配置负载均衡
修改配置文件实现简单的负载均衡
# vim /etc/haproxy/conf.d/text.cfg listen WEB_PORT_80 bind 192.168.133.142:80 mode http balance roundrobin server web1 192.168.133.130:80 check inter 3000 fall 3 rise 5 server web2 192.168.133.137:80 check inter 3000 fall 3 rise 5
测试
[root@localhost ~]# for i in {1..10}; do curl 192.168.133.142 ; done rs1 192.168.133.130 rs2 192.168.133.137 rs1 192.168.133.130 rs2 192.168.133.137 rs1 192.168.133.130 rs2 192.168.133.137 rs1 192.168.133.130 rs2 192.168.133.137 rs1 192.168.133.130 rs2 192.168.133.137