环境
系统:CentOS 7.4
软件:haproxy-1.8.7.tar.gz
准备
- 安装依赖包
# yum install gcc
安装
下载源码包
地址:http://www.haproxy.org/#down-
编译安装
# tar -xzvf haproxy-1.8.7.tar.gz # cd haproxy-1.8.7 # make PREFIX=/usr/local/haproxy TARGET=linux2628 # make install PREFIX=/usr/local/haproxy
-
配置环境变量
# vim /etc/profile export PATH=/usr/local/haproxy/sbin:$PATH # source /etc/profile
配置
- 创建配置文件
# mkdir /usr/local/haproxy/conf
# vim /usr/local/haproxy/conf/haproxy.cfg
global
daemon
maxconn 4000
pidfile /usr/local/haproxy/haproxy.pid
defaults
timeout connect 10s
timeout client 1m
timeout server 1m
listen app
bind 0.0.0.0:8080
mode tcp
server app001 app001.domain.com:8080 check
server app002 app002.domain.com:8080 check
listen haproxy_statistics
bind 0.0.0.0:9000
mode http
stats enable
stats uri /haproxy_statistics
stats realm HAProxy\ Statistics
stats auth admin:Password
启动
-
添加启动服务
# vim /usr/lib/systemd/system/haproxy.service [Unit] Description=HAProxy After=network.target [Service] User=root Type=forking ExecStart=/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg ExecStop=/usr/bin/kill `/usr/bin/cat /usr/local/haproxy/haproxy.pid` [Install] WantedBy=multi-user.target
-
启动
# systemctl enable haproxy.service # systemctl start haproxy.service
其他
- 查看HAProxy状态
Web地址:http://ip:9000/haproxy_statistics