haproxy
1.haproxy简介
HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。
HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。
包括 GitHub、Bitbucket、Stack Overflow、Reddit、Tumblr、Twitter和 Tuenti在内的知名网站,及亚马逊网络服务系统都使用了HAProxy。
2.负载均衡
二层负载均衡(mac)
用于虚拟mac地址方式,外部对虚拟mac地址请求,负载均衡接收后分配给后端实际的mac地址响应。
三层负载均衡(ip)
一般用于虚拟ip地址的方式,外部对虚拟ip地址请求,负载均衡接收后分配给后端实际的ip地址响应。
四层负载均衡(tcp)
在三层负载均衡的基础上,用ip+port接收请求,在转发到对应的机器上。
产品大概有:F5,lvs,nginx,haproxy…
七层负载均衡(http)
根据虚拟的url或者ip,主机名接收请求,在转发到相应的处理服务器上。
产品大概有:haproxy,nginx,apache,mysql proxy…
3.haproxy安装
//下载haproxy安装包
[root@huangweifeng ~]# ls //我是将事先下载好的压缩包拖进来的
anaconda-ks.cfg haproxy-2.6.6.tar.gz rpmbuild scripts
//安装编译环境
[root@huangweifeng ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
//创建haproxy用户
[root@huangweifeng ~]# useradd -rMs /sbin/nologin haproxy
//解压和安装
[root@huangweifeng ~]# tar -xf haproxy-2.6.6.tar.gz -C /usr/local/
[root@huangweifeng ~]# ls /usr/local/
bin games include lib64 sbin src
etc haproxy-2.6.6 lib libexec share
[root@huangweifeng ~]# cd /usr/local/haproxy-2.6.6/
[root@huangweifeng haproxy-2.6.6]# make clean
[root@huangweifeng haproxy-2.6.6]# make -j $(grep 'processor' /proc/cpuinfo |wc -l) \
> TARGET=linux-glibc \
> USE_OPENSSL=1 \
> USE_ZLIB=1 \
> USE_PCRE=1 \
> USE_SYSTEMD=1
[root@huangweifeng haproxy-2.6.6]# make install PREFIX=/usr/local/haproxy
[root@huangweifeng haproxy-2.6.6]# ls
addons CONTRIBUTING haproxy MAINTAINERS scripts VERDATE
admin dev include Makefile src VERSION
BRANCHES doc INSTALL README SUBVERS
CHANGELOG examples LICENSE reg-tests tests
[root@huangweifeng haproxy-2.6.6]# cp haproxy /usr/sbin/
//设置Linux内核参数
[root@huangweifeng haproxy-2.6.6]# vim /etc/sysctl.conf
[root@huangweifeng haproxy-2.6.6]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
//配置haproxy服务
[root@huangweifeng haproxy-2.6.6]# mkdir /etc/haproxy
[root@huangweifeng haproxy-2.6.6]# vim /etc/haproxy/haproxy.cfg
[root@huangweifeng haproxy-2.6.6]# cat /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local0 info //定义haproxy日志输出设置
#log loghost local0 info //定义haproxy日志级别
maxconn 20480 //定义最大连接数
#chroot /usr/local/haproxy //chroot运行路径
pidfile /var/run/haproxy.pid //haproxy进程PID文件
#maxconn 4000
user haproxy //运行haproxy用户,可用uid代替
group haproxy //运行haproxy用户组,可用gid代替
daemon //以后台形式运行haproxy
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http //所处理的类别(7层代理http,4层代理tcp)
log global //引入global定义的日志格式
option dontlognull //不记录健康检查日志信息
option httpclose //每次请求完毕后主动关闭http通道,haproxy不支持keep-alive模式
option httplog //日志类别为http日志格式
#option forwardfor //如果后端服务器需要获取客户端的真是ip,需要配置的参数,可以从http header中获取客户端的ip
option redispatch
balance roundrobin //设置默认负载均衡方式,轮询方式
timeout connect 10s //默认连接超时时间
timeout client 10s //默认客户端超时时间
timeout server 10s //默认服务器超时时间
timeout check 10s //设置超时检查超时时间
maxconn 60000 //最大连接数
retries 3 //3次连接失败就认为服务器不可用,也可以通过后面设置
#--------------统计页面配置------------------
listen admin_stats //frontend和backend的组合体,监控组的名称,按需自定义名称
bind 0.0.0.0:8189 //侦听端口
stats enable //开启监控
mode http
log global
stats uri /haproxy_stats //监控页面的url访问路径
stats realm Haproxy\ Statistics //监控页面的提示信息
stats auth admin:admin //监控页面的用户和密码
#stats hide-version //隐藏统计页面上的haproxy版本信息
stats admin if TRUE //手工启用/禁用,后端服务器haproxy
stats refresh 30s //每个30秒自动刷新监控页面
#---------------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.159.146:80 check inter 2000 fall 5
#server web01 192.168.80.147:80 cookie web01 check inter 2000 fall 5
//启动haproxy,配置haproxy.service服务单元文件
[root@huangweifeng haproxy-2.6.6]# vim /usr/lib/systemd/system/haproxy.service
[root@huangweifeng haproxy-2.6.6]# cat /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@huangweifeng haproxy-2.6.6]# systemctl daemon-reload
[root@huangweifeng haproxy-2.6.6]# systemctl restart haproxy.service
[root@huangweifeng haproxy-2.6.6]# systemctl enable --now haproxy.service
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.
//配置日志信息
[root@huangweifeng haproxy-2.6.6]# vim /etc/rsyslog.conf
local0.haproxy /var/local/haproxy.log
[root@huangweifeng haproxy-2.6.6]# systemctl restart rsyslog.service
[root@huangweifeng haproxy-2.6.6]# systemctl restart haproxy.service
4.Haproxy搭建http负载均衡
*主机名称* | *IP地址* | *需要安装的应用* |
---|---|---|
client | 192.168.159.101 | 无 |
LB | 192.168.159.100 | haproxy |
RS1 | 192.168.159.146 | httpd |
RS2 | 192.168.159.147 | httpd |
//LB、RS1、RS2都关闭防火墙和selinux
[root@hwf ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vend>
Active: inactive (dead)
Docs: man:firewalld(1)
[root@hwf ~]# getenforce
Disabled
[root@RS1 ~]# systemctl stop firewalld
[root@RS1 ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS1 ~]# getenforce
Disabled
[root@RS2 ~]# systemctl stop firewalld
[root@RS2 ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS2 ~]# getenforce
Disabled
//RS1和RS2部署httpd
[root@RS1 ~]# yum -y install httpd
[root@RS1 ~]# echo RS1 > /var/www/html/index.html
[root@RS2 ~]# yum -y install httpd
[root@RS2 ~]# echo RS2 > /var/www/html/index.html
//修改LB的内核参数
[root@hwf haproxy-2.6.6]# vim /etc/sysctl.conf
[root@hwf haproxy-2.6.6]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
//修改haproxy配置文件
[root@hwf haproxy-2.6.6]# 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.159.146:80
server web02 192.168.159.147:80
//启动haproxy服务
[root@hwf haproxy-2.6.6]# systemctl restart haproxy.service
//客户端验证
[root@client ~]# curl http://192.168.159.100
RS2
[root@client ~]# curl http://192.168.159.100
RS1
[root@client ~]# curl http://192.168.159.100
RS2
[root@client ~]# curl http://192.168.159.100
RS1
//使用WEB网页访问测试
[root@hwf haproxy-2.6.6]# cat /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.159.146:80 check inter 2000 fall 5
server web02 192.168.159.147:80 check inter 2000 fall 5
[root@hwf haproxy-2.6.6]# systemctl restart haproxy.service
[root@hwf haproxy-2.6.6]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:8189 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
//网页访问测试