HAProxy配置
HAProxy简介
HAProxy 是一款基于 TCP(第四层)和 HTTP(第七层)应用的代理软件,它也可作为负载均衡器,而且完全免费。 借助 HAProxy,可以快速并且可靠地提供基于 TCP 和 HTTP 应用的代理解决方案。 HAProxy 最主要的优点是性能突出,它特别适合那些负载特大的 Web 站点,这些站点通常需要具备会话保持或七层处理功能。 HAProxy 完全可以支持数以万计的并发连接,而且它的运行模式可以让你简单而安全地将它整合到当前的架构中,同时可以保护你的 Web 服务器不暴露到网络上(通过防火墙 80 端口映射的方法)。
配置要求
用HAProxy搭建http与https的负载集群
环境介绍
| 主机 | IP地址 | 服务 |
|---|---|---|
| cst | 192.168.209.128 | 客户机 |
| LB | 192.168.209.129 | HAProxy |
| RS1 | 192.168.209.132 | httpd |
| RS2 | 192.168.209.133 | httpd |
环境准备
//LB操作
[root@LB ~]# systemctl disable --now firewalld
[root@LB ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
[root@LB ~]# setenforce 0
//RS1操作
[root@RS1 ~]# systemctl disable firewalld.service --now
[root@RS1 ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
[root@RS1 ~]# setenforce 0
[root@RS1 ~]# yum -y install httpd
[root@RS1 ~]# systemctl enable httpd --now
[root@RS2 ~]# echo HAProxy-web1 > /var/www/html/index.html
//RS2操作
[root@RS2 ~]# systemctl disable firewalld.service --now
[root@RS2 ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
[root@RS2 ~]# setenforce 0
[root@RS2 ~]# yum -y install httpd
[root@RS2 ~]# systemctl enable httpd --now
[root@RS2 ~]# echo HAProxy-web2 > /var/www/html/index.html
编译安装HAProxy
//安装依赖包
[root@LB ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
//创建HAProxy系统用户
[root@LB ~]# useradd -r -M -s /sbin/nologin haproxy
//编译安装HAProxy
此处已准备好安装包
[root@LB ~]# ls
haproxy-2.3.10.tar.gz
[root@LB ~]# tar xf haproxy-2.3.10.tar.gz
[root@LB ~]# cd haproxy-2.3.10/
[root@LB haproxy-2.3.10]# make -j $(grep 'processor' /proc/cpuinfo |wc -l) \
TARGET=linux-glibc \
USE_OPENSSL=1 \
USE_ZLIB=1 \
USE_PCRE=1 \
USE_SYSTEMD=1
[root@LB haproxy-2.3.10]# make install PREFIX=/usr/local/haprox
[root@LB haproxy-2.3.10]# cp haproxy /usr/sbin/
搭建HTTP负载集群
配置各个负载的内核参数
[root@LB ~]# echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf
[root@LB ~]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
[root@LB ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
提供配置文件
[root@LB ~]# mkdir /etc/haproxy
[root@LB ~]# vim /etc/haproxy/haproxy.cfg
global
daemon
maxconn 256
defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend http-in
bind *:80
default_backend servers
backend servers
server web01 192.168.209.132:80
server web02 192.168.209.133:80
[root@LB ~]# haproxy -f /etc/haproxy/haproxy.cfg -c
Configuration file is valid
启动
[root@LB ~]# haproxy -f /etc/haproxy/haproxy.cfg
[root@LB ~]# vim /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
[Service]
ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
[root@LB ~]# systemctl daemon-reload
[root@LB ~]# systemctl enable haproxy.service --now
测试
[root@cst ~]# curl http://192.168.209.129
HAProxy-web2
[root@cst ~]# curl http://192.168.209.129
HAProxy-web1
[root@cst ~]# curl http://192.168.209.129
HAProxy-web1
[root@cst ~]# curl http://192.168.209.129
HAProxy-web2
搭建HTTPS负载集群
以上配置操作成功后,进行如下配置
//在RS服务器上安装mod_ssl
[root@RS1 ~]# yum -y install mod_ssl
[root@RS2 ~]# yum -y install mod_ssl
[root@RS1 ~]# systemctl restart httpd
[root@RS1 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 *:443 *:*
[root@RS2 ~]# systemctl restart httpd
[root@RS2 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 *:443 *:*
LISTEN 0 128 *:80 *:*
修改配置文件
[root@LB ~]# vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2 info
maxconn 20480
chroot /usr/local/haproxy
pidfile /var/run/haproxy.pid
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
user haproxy
group haproxy
daemon
nbproc 1
nbthread 4
spread-checks 5
defaults
mode http
log global
option dontlognull
option httpclose
option http-keep-alive
option redispatch
balance roundrobin
timeout connect 60s
timeout client 30s
timeout server 30s
timeout check 10s
maxconn 60000
retries 3
listen https
bind 0.0.0.0:443
log global
mode tcp
balance roundrobin
server web01 192.168.209.132:443 check inter 2s fall 3 rise 5
server web02 192.168.209.133:443 check inter 2s fall 3 rise 5
[root@LB ~]# mkdir /var/lib/haproxy
[root@LB ~]# systemctl restart haproxy
测试
[root@cst ~]# curl -k https://192.168.209.129
HAProxy-web1
[root@cst ~]# curl -k https://192.168.209.129
HAProxy-web2
[root@cst ~]# curl -k https://192.168.209.129
HAProxy-web1
[root@cst ~]# curl -k https://192.168.209.129
HAProxy-web2
HAProxy网页界面配置
修改配置文件
[root@LB ~]# vim /etc/haproxy/haproxy.cfg
#--------------全局配置----------------
global
log 127.0.0.1 local0 info
#log loghost local0 info
maxconn 20480
#chroot /usr/local/haproxy
pidfile /var/run/haproxy.pid
#maxconn 4000
user haproxy
group haproxy
daemon
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option dontlognull
option httpclose
option httplog
#option forwardfor
option redispatch
balance roundrobin
timeout connect 10s
timeout client 10s
timeout server 10s
timeout check 10s
maxconn 60000
retries 3
#--------------统计页面配置------------------
listen admin_stats
bind 0.0.0.0:8189
stats enable
mode http
log global
stats uri /haproxy_stats
stats realm Haproxy\ Statistics
stats auth admin:admin
#stats hide-version
stats admin if TRUE
stats refresh 30s
#---------------web设置-----------------------
listen webcluster
bind 0.0.0.0:80
mode http
#option httpchk GET /index.html
log global
maxconn 3000
balance roundrobin
cookie SESSION_COOKIE insert indirect nocache
server web01 192.168.209.132:80 check inter 2000 fall 5
server web02 192.168.209.133:80 check inter 2000 fall 5
#server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
[root@LB ~]# systemctl restart haproxy
//启用日志
[root@LB ~]# vim /etc/rsyslog.conf
……
# Save boot messages also to boot.log
local0.info /var/log/haproxy.log //插入这一行
local7.* /var/log/boot.log
[root@LB ~]# systemctl restart rsyslog
访问测试
访问http://192.168.209.129:8189/haproxy_stats

[root@LB ~]# cat /etc/haproxy/haproxy.cfg
······
#--------------统计页面配置------------------
stats uri /haproxy_stats //访问方式
stats realm Haproxy\ Statistics
stats auth admin:admin //用户名和密码均为admin

成功进入网页界面

浙公网安备 33010602011771号