概述
1. 下载Haproxy源码包:
wget -c https://repo.huaweicloud.com/haproxy/2.4/src/haproxy-2.4.8.tar.gz
2. 下载源码编译工具:
yum install -y gcc gcc-c++ make
3. 下载Haproxy依赖包lua并编译安装:
curl -R -O http://www.lua.org/ftp/lua-5.4.4.tar.gz
tar zxf lua-5.4.4.tar.gz -C /usr/local/src/
cd /usr/local/src/lua-5.4.4
make linux test
4. 查看lua版本信息:
方法1:/usr/local/src/lua-5.4.4/src/lua -v
Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
方法2:cp /usr/local/src/lua-5.4.4/src/lua /usr/bin/lua
lua -v
Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
5. 解压Haproxy源码包:
tar xf haproxy-2.4.8.tar.gz -C /usr/local/src/
cd /usr/local/src/haproxy-2.4.8
6. 安装编译时需要的依赖包
yum install openssl-devel pcre-devel systemd-devel -y
7. 编译安装Haproxy
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/
make install PREFIX=/usr/local/haproxy
8. 查看haproxy的目录结构
tree /usr/local/haproxy/
/usr/local/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
9. 对/usr/local/haproxy/sbin/haproxy做软连接:
ln -sv /usr/local/haproxy/sbin/haproxy /usr/sbin/haproxy
10. 查看haproxy版本信息:
[root@centosnode1 conf.d]# 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 3.10.0-1160.71.1.el7.x86_64 #1 SMP Tue Jun 28 15:37:28 UTC 2022 x86_64
11. 为haproxy服务配置启动脚本:
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 /usr/local/haproxy/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
12. 创建haproxy服务的配置文件:可以配置文件示例中复制,配置文件示例位置:/usr/local/src/haproxy-2.4.8/examples/
vim /etc/haproxy/haproxy.cfg
global ##全局配置
maxconn 10000
stats socket /var/run/haproxy.stat mode 600 level admin
log 127.0.0.1 local2 info
user haproxy ##指定用户
group haproxy ##指定组
chroot /usr/local/haproxy ##服务工作目录
daemon ##开启保护进程
defaults ##默认配置
mode http
option httplog
log global
timeout client 1m
timeout server 1m
timeout connect 10s
timeout http-keep-alive 2m
timeout queue 15s
timeout tunnel 4h # for websocket
default-server inter 1000 weight 3
listen app1 ##此部分是前端部分和后端部分的结合
bind :80 ##监听的端口
log global
server web1 192.168.188.226:80 check ##后端的真实服务器地址
server web2 192.168.188.227:80 check ##后端的真实服务器地址
listen stats #配置监听页面
mode http
bind :9999 ##使用9999端口
stats enable
log global
stats uri /haproxy-status
stats auth haadmin:123456 ##指定登录监听页面的用户是haadmin,密码是123456
13. 创建用户和组:
useradd -r -s /sbin/nologin -d /usr/local/haproxy/ haproxy
14. 启动haproxy服务:
systemctl start haproxy.service
15. 在后端两台真实服务器上创建web页面,并开启apache服务:
RS1:echo "this is page ip : 192.168.188.226" > /var/www/html/index.html
RS2: echo "this is page ip : 192.168.188.227" > /var/www/html/index.html
systemctl start httpd
16. 测试haproxy是否实验负载均衡:
[root@centosnode4 html]# for ((i=1;i<=8;i++))
> do
> curl 192.168.188.223
> done
this is page ip : 192.168.188.227
this is page ip : 192.168.188.227
this is page ip : 192.168.188.226
this is page ip : 192.168.188.227
this is page ip : 192.168.188.226
this is page ip : 192.168.188.227
this is page ip : 192.168.188.227
this is page ip : 192.168.188.227
最后
以上就是勤劳大米为你收集整理的源码安装并运行Haproxy的全部内容,希望文章能够帮你解决源码安装并运行Haproxy所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复