yum安装haproxy
haproxy 是一款开源的负载均衡软件,他提供 L4 和 L7 层负载功能,全称为 high availability proxy。
 我们准备一台纯新的 CentOS7.6 服务器,关闭 selinux ,清空防火墙规则,使用 yum 安装 haproxy[root@haproxy ~]# getenforce
 Disabled[root@haproxy ~]# ip a
 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
 inet 127.0.0.1/8 scope host lo
 valid_lft forever preferred_lft forever
 inet6 ::1/128 scope host
 valid_lft forever preferred_lft forever
 2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
 link/ether 00:0c:29:49:94:f4 brd ff:ff:ff:ff:ff:ff
 inet 10.20.0.10/8 brd 10.255.255.255 scope global noprefixroute ens32
 valid_lft forever preferred_lft forever
 inet6 fe80::20c:29ff:fe49:94f4/64 scope link
 valid_lft forever preferred_lft forever[root@haproxy ~]# yum install haproxy -y
[root@haproxy ~]# rpm -ql haproxy
 /etc/haproxy
 /etc/haproxy/haproxy.cfg
 /etc/logrotate.d/haproxy
 /etc/sysconfig/haproxy
 /usr/bin/halog
 /usr/bin/iprange
 /usr/lib/systemd/system/haproxy.service
 /usr/sbin/haproxy
 /usr/sbin/haproxy-systemd-wrapper
 /usr/share/doc/haproxy-1.5.18
 .
 .省略部分输出信息…
 .
 /usr/share/doc/haproxy-1.5.18/proxy-protocol.txt
 /usr/share/doc/haproxy-1.5.18/queuing.fig
 /usr/share/haproxy
 /usr/share/haproxy/400.http
 /usr/share/haproxy/403.http
 /usr/share/haproxy/408.http
 /usr/share/haproxy/500.http
 /usr/share/haproxy/502.http
 /usr/share/haproxy/503.http
 /usr/share/haproxy/504.http
 /usr/share/haproxy/README
 /usr/share/man/man1/halog.1.gz
 /usr/share/man/man1/haproxy.1.gz
 /var/lib/haproxy查看 haproxy 版本信息
 [root@haproxy ~]# haproxy -v
 HA-Proxy version 1.5.18 2016/05/10
 Copyright 2000-2016 Willy Tarreau willy@haproxy.org修改 haproxy 配置文件,对本机工作在 88 号端口的 httpd 进行负载
 [root@haproxy ~]# vi /etc/haproxy/haproxy.cfg[root@haproxy ~]# cat /etc/haproxy/haproxy.cfg
 #---------------------------------------------------------------------
 global
 log 127.0.0.1 local2
 chroot /var/lib/haproxy
 pidfile /var/run/haproxy.pid
 maxconn 4000
 user haproxy
 group haproxy
 daemon
 stats socket /var/lib/haproxy/stats#---------------------------------------------------------------------
 defaults
 mode http
 log global
 option httplog
 option dontlognull
 option http-server-close
 option forwardfor except 127.0.0.0/8
 option redispatch
 retries 3
 timeout http-request 10s
 timeout queue 1m
 timeout connect 10s
 timeout client 1m
 timeout server 1m
 timeout http-keep-alive 10s
 timeout check 10s
 maxconn 3000#---------------------------------------------------------------------
 listen webapps
 bind 10.20.0.10:80
 server web1 127.0.0.1:88 check inter 3000 fall 2 rise 5[root@haproxy ~]# yum install httpd -y
[root@haproxy ~]# echo “web server index page” > /var/www/html/index.html
[root@haproxy ~]# vi /etc/httpd/conf/httpd.conf
[root@haproxy ~]# cat /etc/httpd/conf/httpd.conf | grep “Listen 88”
 Listen 88[root@haproxy ~]# systemctl start httpd
[root@haproxy ~]# ss -ntl
 State Recv-Q Send-Q Local Address:Port Peer Address:Port
 LISTEN 0 128 :22 :
 LISTEN 0 100 127.0.0.1:25 :
 LISTEN 0 128 :::22 :::
 LISTEN 0 128 :::88 ::😗
 LISTEN 0 100 ::1:25 ::😗[root@haproxy ~]# curl 127.0.0.1:88
 web server index page启动 haproxy ,进行访问测试
 [root@haproxy ~]# systemctl start haproxy[root@haproxy ~]# ss -ntl
 State Recv-Q Send-Q Local Address:Port Peer Address:Port
 LISTEN 0 128 10.20.0.10:80 :
 LISTEN 0 128 :22 :
 LISTEN 0 100 127.0.0.1:25 :
 LISTEN 0 128 :::22 :::
 LISTEN 0 128 :::88 ::😗
 LISTEN 0 100 ::1:25 ::😗[root@haproxy ~]# curl 10.20.0.10
 web server index page源码编译安装 haproxy
我们在上一个实验的基础上,删除我们 yum 安装的 haproxy1.5 版本,然后在 haproxy 官网下载最新的 haproxy2.0.4 版本,进行编译安装
[root@haproxy ~]# yum remove haproxy -y
[root@haproxy ~]# yum install wget -y
由于 haproxy 高版本依赖 lua5.3 以上的版本,因此编译安装 haproxy 之前,需要提前编译安装 lua
[root@haproxy ~]# wget http://www.lua.org/ftp/lua-5.3.5.tar.gz
安装编译 lua 所依赖的软件包
 [root@haproxy ~]# yum install libtermcap-devel ncurses-devel libevent-devel readline-devel gcc -y[root@haproxy ~]# tar xf lua-5.3.5.tar.gz
[root@haproxy ~]# cd lua-5.3.5
[root@haproxy lua-5.3.5]# make linux test
[root@haproxy lua-5.3.5]# cd
[root@haproxy ~]#
编译安装 haproxy
 [root@haproxy ~]# yum install gcc gcc-c++ glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib zlib-devel lsof ntpdate -y下载一下 haproxy 源码包,地址 http://www.haproxy.org/download/2.0/src/haproxy-2.0.4.tar.gz
[root@haproxy ~]# ll
 total 5236
 -rw-------. 1 root root 1366 May 8 19:53 anaconda-ks.cfg
 -rw-r–r-- 1 root root 2538442 Aug 19 12:32 haproxy-2.0.4.tar.gz
 drwxr-xr-x 4 1026 1000 58 Jun 27 2018 lua-5.3.5
 -rw-r–r-- 1 root root 303543 Jun 27 2018 lua-5.3.5.tar.gz[root@haproxy ~]# tar xf haproxy-2.0.4.tar.gz
[root@haproxy ~]# cd haproxy-2.0.4
[root@haproxy haproxy-2.0.4]# make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 USE_LUA=1 LUA_INC=/root/lua-5.3.5/src LUA_LIB=/root/lua-5.3.5/src PREFIX=/usr/local/haproxy
[root@haproxy haproxy-2.0.4]# make install PREFIX=/usr/local/haproxy
[root@haproxy haproxy-2.0.4]# cp haproxy /usr/sbin/
[root@haproxy haproxy-2.0.4]# cd
[root@haproxy ~]# haproxy -v
 HA-Proxy version 2.0.4 2019/08/06 - https://haproxy.org/创建 haproxy 的 systemd 配置文件
 [root@haproxy ~]# vi /usr/lib/systemd/system/haproxy.service[root@haproxy ~]# cat /usr/lib/systemd/system/haproxy.service
 [unit]
 Description=HAProxy Load Balancer
 After=syslog.target network.target[Service]
 ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
 ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
 ExecReload=/bin/kill -USR2 $MAINPID[Install]
 WantedBy=multi-user.target
 [root@haproxy ~]#
 [root@haproxy ~]# systemctl daemon-reload
 [root@haproxy ~]#创建 haproxy 服务的配置文件
 [root@haproxy ~]# vi /etc/haproxy/haproxy.cfg[root@haproxy ~]# cat /etc/haproxy/haproxy.cfg
 #---------------------------------------------------------------------
 global
 log 127.0.0.1 local2
 chroot /var/lib/haproxy
 pidfile /var/run/haproxy.pid
 maxconn 4000
 user haproxy
 group haproxy
 daemon
 stats socket /var/lib/haproxy/stats#---------------------------------------------------------------------
 defaults
 mode http
 log global
 option httplog
 option dontlognull
 option http-server-close
 option forwardfor except 127.0.0.0/8
 option redispatch
 retries 3
 timeout http-request 10s
 timeout queue 1m
 timeout connect 10s
 timeout client 1m
 timeout server 1m
 timeout http-keep-alive 10s
 timeout check 10s
 maxconn 3000#---------------------------------------------------------------------
 listen webapps
 bind 10.20.0.10:80
 server web1 127.0.0.1:88 check inter 3000 fall 2 rise 5[root@haproxy ~]# systemctl start haproxy
[root@haproxy ~]# ss -ntl
 State Recv-Q Send-Q Local Address:Port Peer Address:Port
 LISTEN 0 128 10.20.0.10:80 :
 LISTEN 0 128 :22 :
 LISTEN 0 100 127.0.0.1:25 :
 LISTEN 0 128 :::22 :::
 LISTEN 0 128 :::88 ::😗
 LISTEN 0 100 ::1:25 ::😗[root@haproxy ~]# systemctl status haproxy
[root@haproxy ~]# curl 127.0.0.1
 curl: (7) Failed connect to 127.0.0.1:80; Connection refused[root@haproxy ~]# curl 10.20.0.10
 web server index pagehaproxy 主要配置参数介绍
haproxy 的全局配置内容
global : 表示全局配置内容开始
 log : 定义 rsyslog 配置
 chroot : 锁定 haproxy 进程的运行目录
 pidfile : 指定 pid 文件存放路径
 maxconn : 指定进程的最大并发连接数
 user : 指定进程的运行者身份
 group : 指定进程的运行组身份
 daemon : 指定进程以守护进程运行
 nbproc : 指定 haproxy 开启的进程数
 cpu-map : 1.8 版本以上的 haproxy 指定进程号和指定cpu绑定
 nbthread: 指定每个进程所开启的线程数,默认为 1 个
 spread-checks: 分散 tcp 健康监测,防止后台服务器数量过多,压力过大,建议 2-5proxies 配置 haproxy 所代理的主机配置
 defaults :表示代理配置功能开始
 mode http/tcp :表示默认采用的代理模式, L4 或者 L7
 log global :日志配置
 option dontlognull :是否记录空连接日志
 option http-server-close:开启 http 服务端主动关闭连接功能
 option forwardfor :开启 http 转发的 X-Forwarded-For 报文头功能
 option redispatch :当已建立的连接服务器宕机后,强制将用户请求调度到其他健康的服务器
 retries :连接失败后的重复尝试连接次数
 timeout http-request :设置 http 请求最大请求时长
 timeout queue :连接在等待队列中的最大时长
 timeout connect :设置等待连接成功的最长时间
 timeout client :设置客户端不活跃时间
 timeout server :设置服务器端最大不活跃时间
 timeout http-keep-alive :设置连接的 keepalive 时间
 timeout check :设置已连接的连接检查
 maxconn :设置前端最大连接数量listen 方式定义代理服务功能
 listen :后面配置一个代理名称,配置区分大小写
 bind :定义本个代理中 haproxy 对外监听的 ip 地址和端口
 server :指定后端被代理的地址和健康监测配置frontend 和 backend
 通过前后端分别定义的方式完成代理配置,建议使用上面的 listen 方式,比如:
 frontend web
 bind 192.168.10.10:80
 use_backend web_server
 backend web_server
 server 192.168.10.30:80
 server 192.168.10.40:80由于 haproxy 通常会配置多个业务和系统的代理配置,将全部配置信息写到一个配置文件中不便于管理,下面我们可以指定一个目录作为配置文件的存放目录,haproxy 启动时可以读取目录下的全部配置文件,比如我们创建 /etc/haproxy/conf 目录,然后修改 haproxy 的启动配置文件,添加该路径
 [root@haproxy ~]# vi /usr/lib/systemd/system/haproxy.service[root@haproxy ~]# cat /usr/lib/systemd/system/haproxy.service
 [unit]
 Description=HAProxy Load Balancer
 After=syslog.target network.target[Service]
 ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf -c -q
 ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf -p /var/lib/haproxy/haproxy.pid
 ExecReload=/bin/kill -USR2 $MAINPID[Install]
 WantedBy=multi-user.target