安装方式一
通过apt-get安装:
sudo apt-get update
sudo apt-get install haproxy
安装方式二
通过源码编译安装,源码下载地址:www.haproxy.org/
# 安装编译工具
sudo apt-get install libssl-dev make
# 解压
tar -xf haproxy-x.x.x.tar.gz
cd haproxy-x.x.x.tar
# 普通安装方式,haproxy不支持SSL
sudo make TARGET=linux2628 ARCH=x86_64 prefix=/usr/local/haproxy install
# 需要haproxy支持SSL时,使用如下编译方式
sudo make TARGET=linux2628 ARCH=x86_64 USE_OPENSSL=1 ADDLIB=-lz prefix=/usr/local/haproxy install
#将haproxy和openssl库连接
ldd haproxy | grep ssl
haproxy配置
运行命令查看用户的uid, gid:id <username>
支行命令:sudo vim /etc/haproxy/haproxy.cfg 修改配置文件
注意:这里的 pem 文件是下面两个文件合并而成:
cat servername.crt servername.key |tee servername.pem
# this config needs haproxy-1.1.28 or higher edition
global
log 127.0.0.1 local0 debug
#log 127.0.0.1 local1 notice
maxconn 4096
uid 1000
gid 1000
chroot /etc/haproxy
pidfile /var/run/haproxy.pid
daemon
nbproc 4
tune.ssl.default-dh-param 2048
#2 lines below modify the maxHttpHeaderSize
#tune.bufsize 65536
#tune.chksize 65536
#debug
#quiet
defaults
log global
mode http
option dontlognull
option redispatch
option abortonclose
retries 3
timeout connect 30s
timeout queue 5s
timeout client 1800s
timeout server 1800s
timeout check 30s
#timeout client-fin 30s
#timeout server-fin 30s
#timeout tunnel 1800s
listen stats
bind 0.0.0.0:8888
stats refresh 30s
stats uri /
stats realm baison-test-Haproxy
stats auth chianyu:cyadmin
bind-process 1
frontend https_frontend
bind *:80
#bind *:443 ssl crt /etc/ssl/xip.io/xip.io.pem
bind *:443 ssl crt /etc/haproxy/sslforfree/ssl.pem
#http-request set-header X-Forwarded-Proto https if { ssl_fc }
http-request set-header X-SSL %[ssl_fc]
redirect scheme https if !{ ssl_fc }
mode http
default_backend http_nodes
backend http_nodes
mode http
#option httpchk
option forwardfor
#option httpclose
#option http-keep-alive
#balance roundrobin
cookie svr_ckie insert nocache
server web1 192.168.1.10:8080 cookie web_1 check inter 2000 fall 3 weight 1
server web2 192.168.1.11:8080 cookie web_2 check inter 2000 fall 3 weight 2
#capture cookie vgnvisitor= len 32
timeout http request在客户端建立连接但不请求数据时,关闭客户端连接timeout http-keep-alive定义保持连接的超时时长timeout check健康状态监测时的超时时间,过短会误判,过长资源消耗timeout connect连接尝试成功连接到server的超时时间timeout queue在队列等待连接槽释放的超时时间timeout serverserver端非活动状态超时时间timeout client客户端非活动状态超时时间timeout server-fin半关闭状态连接,server端非活动超时时间timeout client-fin半关闭状态连接,client端非活动超时时间timeout tunnel客户端和服务器端通道非活动超时时间http-server-close在使用长连接时,为了避免客户端超时没有关闭长连接,此功能可以使服务器端关闭长连接redispatch在使用基于cookie定向时,一旦后端某一server宕机时,会将会话重新定向至某一上游服务器,必须使用的选项
让系统支持大量连接,文件/etc/security/limits.conf添加:
* soft nofile 150000
* hard nofile 180000
haproxy负载均衡算法
roundrobin表示简单的轮询;static-rr表示根据权重;leastconn表示最少连接者先处理;source表示根据请求源IP;uri表示根据请求的URI;url_param表示根据请求的URl参数;hdr(name)表示根据HTTP请求头来锁定每一次HTTP请求;rdp-cookie(name)表示根据据cookie(name)来锁定并哈希每一次TCP请求。
目前haproxy支持的负载均衡算法说明
1、roundrobin
表示简单的轮询,每个服务器根据权重轮流使用,在服务器的处理时间平均分配的情况下这是最流畅和公平的算法。该算法是动态的,对于实例启动慢的服务器权重会在运行中调整。
2、leastconn
连接数最少的服务器优先接收连接。leastconn建议用于长会话服务,例如LDAP、SQL、TSE等,而不适合短会话协议。如HTTP.该算法是动态的,对于实例启动慢的服务器权重会在运行中调整。
3、static-rr
每个服务器根据权重轮流使用,类似roundrobin,但它是静态的,意味着运行时修改权限是无效的。另外,它对服务器的数量没有限制。
该算法一般不用;
4、source
对请求源IP地址进行哈希,用可用服务器的权重总数除以哈希值,根据结果进行分配。只要服务器正常,同一个客户端IP地址总是访问同一个服务器。如果哈希的结果随可用服务器数量而变化,那么客户端会定向到不同的服务器;
该算法一般用于不能插入cookie的Tcp模式。它还可以用于广域网上为拒绝使用会话cookie的客户端提供最有效的粘连;
该算法默认是静态的,所以运行时修改服务器的权重是无效的,但是算法会根据“hash-type”的变化做调整。
5、uri
表示根据请求的URI左端(问号之前)进行哈希,用可用服务器的权重总数除以哈希值,根据结果进行分配。只要服务器正常,同一个URI地址总是访问同一个服务器。一般用于代理缓存和反病毒代理,以最大限度的提高缓存的命中率。该算法只能用于HTTP后端;
该算法一般用于后端是缓存服务器;
该算法默认是静态的,所以运行时修改服务器的权重是无效的,但是算法会根据“hash-type”的变化做调整。
6、url_param
在HTTP GET请求的查询串中查找中指定的URL参数,基本上可以锁定使用特制的URL到特定的负载均衡器节点的要求;
该算法一般用于将同一个用户的信息发送到同一个后端服务器;
该算法默认是静态的,所以运行时修改服务器的权重是无效的,但是算法会根据“hash-type”的变化做调整。
7、hdr(name)
在每个HTTP请求中查找HTTP头,HTTP头将被看作在每个HTTP请求,并针对特定的节点;
如果缺少头或者头没有任何值,则用roundrobin代替;
该算法默认是静态的,所以运行时修改服务器的权重是无效的,但是算法会根据“hash-type”的变化做调整。
8、rdp-cookie(name)
为每个进来的TCP请求查询并哈希RDP cookie;
该机制用于退化的持久模式,可以使同一个用户或者同一个会话ID总是发送给同一台服务器。如果没有cookie,则使用roundrobin算法代替;
该算法默认是静态的,所以运行时修改服务器的权重是无效的,但是算法会根据“hash-type”的变化做调整。
常用算法:roundrobin、source、lestconn
haproxy相关命令
手动启动haproxy命令:sudo haproxy -f /etc/haproxy/haproxy.cfg
手动关闭haproxy命令:sudo killall haproxy
重载haproxy配置:sudo haproxy -f /etc/haproxy/haproxy.cfg -sf
重载haproxy配置(不中断服务):sudo haproxy -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid)
haproxy开机启动:将启动命令添加到文件/etc/rc.local中
生成自签名证书
$ sudo mkdir /etc/ssl/xip.io
$ sudo openssl genrsa -out /etc/ssl/xip.io/xip.io.key 1024
$ sudo openssl req -new -key /etc/ssl/xip.io/xip.io.key -out /etc/ssl/xip.io/xip.io.csr
$ sudo openssl x509 -req -days 365 -in /etc/ssl/xip.io/xip.io.csr -signkey /etc/ssl/xip.io/xip.io.key -out /etc/ssl/xip.io/xip.io.crt
这就生成了axip.io.csr,xip.io.key和xip.io.crt文件了
接着,在创建了证书之后,我们需要创建 pem 文件。pem 文件本质上只是将证书、密钥及证书认证中心证书(可有可无)拼接成一个文件。我们只是简单地将证书及密钥文件并以这个顺序拼接在一样来创建 xip.io.pem 文件。这是 HAProxy 读取SSL证书首选的方式
$ sudo cat /etc/ssl/xip.io/xip.io.crt /etc/ssl/xip.io/xip.io.key | sudo tee /etc/ssl/xip.io/xip.io.pem
当购买真正的证书时,不一定会获取拼接后的文件,你可以要自己拼接它们。然而,很多机构也会提供一份拼接好的文件给你。如果你没有获取到拼接后的文件,则它可能不是一个 pem 文件,而是 bundle、cert、cert、key文件或一些相同概念但名称类似的文件。Stack Overflow: What is a pem file ... 答案对问题解释得很好。
参考
在 HAproxy 1.5 中使用 SSL 证书 景安网络:申请免费HTTPS证书 景安SSL Configure HAProxy to Load Balance Sites With SSL Haproxy Configuration Manual v1.8 Accelerating SSL Load Balancers with Intel® Xeon® v3 Processors Let's Encrypt 给网站加 HTTPS 完全指南 SSL For Free 真正零停机 HAProxy 重载 Configuring HAProxy and Jetty Aidaho12/haproxy-wi 负载均衡技术之3负载分类