1 haproxy概述
HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。根据官方数据,其最高极限支持10G的并发。
HAProxy特别适用于那些负载特大的web站点, 这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
其支持从4层至7层的网络交换,即覆盖所有的TCP协议。就是说,Haproxy 甚至还支持 Mysql的均衡负载。
- 相同点:在功能上,proxy通过反向代理方式实现 WEB均衡负载。和 Nginx,ApacheProxy,lighttpd,Cheroke 等一样。
- 不同点:Haproxy 并不是 web 服务器。以上提到所有带反向代理均衡负载的产品,都清一色是 WEB 服务器。简单说,就是他们能处理解析页面的。而Haproxy 仅仅是一款的用于均衡负载的应用代理。其自身并不能提供web服务。但其配置简单, 拥有非常不错的服务器健康检查功能还有专门的系统状态监控页面,当其代理的后端服务器出现故障, HAProxy会自动将该服务器摘除,故障恢复后再自动将该服务器加入。
环境
IP | 主机名 | |
---|---|---|
haproxy服务端 | 192.168.32.133 | asuna |
web服务端 | 192.168.32.128 | WJX |
web服务端 | 192.168.32.134 | WJX2 |
2 实战
2.1 安装依赖包
[root@asuna ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
2.2 安装haproxy
[root@asuna ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel vim wget
[root@asuna ~]# useradd -r -M -s /sbin/nologin haproxy
[root@asuna ~]# wget http://download.openpkg.org/components/cache/haproxy/haproxy-2.2.0.tar.gz
[root@asuna ~]# tar xf haproxy-2.2.0.tar.gz
[root@asuna ~]# cd haproxy-2.2.0
[root@asuna haproxy-2.2.0]# make -j `nproc` TARGET=linux-glibc USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1 USE_SYSTEMD=1
[root@asuna haproxy-2.2.0]# make install
#配置内核参数
[root@asuna ~]# echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf
[root@asuna ~]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
[root@asuna ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
2.3 haproxy配置文件
[root@asuna ~]# mkdir /etc/haproxy
[root@asuna ~]# cat > /etc/haproxy/haproxy.cfg <<EOF
#--------------全局配置----------------
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.32.128:80 weight 2 check inter 2000 fall 5
server web02 192.168.32.134:80 check inter 2000 fall 5
#server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
EOF
2.4 编写haproxy.service文件
[root@asuna ~]# cat > /usr/lib/systemd/system/haproxy.service <<EOF
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
[Service]
ExecStartPre=/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/local/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
EOF
2.5 启动haproxy和日志服务
[root@asuna ~]# systemctl daemon-reload
[root@asuna ~]# vim /etc/rsyslog.conf
...
# Save boot messages also to boot.log
local0.* /var/log/haproxy.log
local7.* /var/log/boot.log
...
[root@asuna ~]# systemctl restart rsyslog
[root@asuna ~]# systemctl enable --now haproxy
[root@asuna ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:8189 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
2.6 安装httpd 修改index.html
[root@WJX ~]# yum -y install httpd
[root@WJX ~]# systemctl enable --now httpd
Created symlink from /etc/systemd/system/multi-user.targetrvice to /usr/lib/systemd/system/httpd.service.
[root@WJX ~]# systemctl stop firewalld
[root@WJX ~]# cd /var/www/html/
[root@WJX html]# echo 'node01' > index.html
[root@WJX2 ~]# yum -y install httpd
[root@WJX2 ~]# systemctl enable --now httpd
Created symlink from /etc/systemd/system/multi-user.targetrvice to /usr/lib/systemd/system/httpd.service.
[root@WJX2 ~]# systemctl stop firewalld
[root@WJX2 ~]# cd /var/www/html/
[root@WJX2 html]# echo 'node02' > index.html