负载均衡
haproxy
HAProxy:是法国开发者Willy Tarreau开发的一个开源软件,是一款具备高并发、高性能的TCP和HTTP负载均衡器
支持基于cookie的持久性
自动故障切换,
支持正则表达式
web状态统计
HAProxy是TCP / HTTP反向代理服务器,尤其适合于高可用性高并发环境
1)可以针对HTTP请求添加cookie,进行路由后端服务器
2)可平衡负载至后端服务器,并支持持久连接
3)支持基于cookie进行调度
4)支持所有主服务器故障切换至备用服务器
5)支持专用端口实现监控服务
6)支持不影响现有连接情况下停止接受新连接请求
7)可以在双向添加,修改或删除HTTP报文首部
8)支持基于pattern实现连接请求的访问控制
9)通过特定的URI为授权用户提供详细的状态信息
历史版本更新功能:1.4 1.5 1.6 1.7 1.8 1.9 2.0-dev
1.5:开始支持SSL,IPV6,keepalived……
1.6:DNS解析支持,HTTP连接多路复用……
1.7:服务器动态配置,多类型证书……
1.8:多线程,HTTP/2缓存……
编译安装haproxy-1.8.20:
[root@centos7_3 haproxy]# pwd /data/haproxy [root@centos7_3 haproxy]# ls haproxy-1.8.20.tar.gz
1)安装包
[root@centos7_3 haproxy]# yum install gcc gcc-c++ glibc glibc-devel pcre pcre-develo penssl openssl-devel systemd-devel net-tools vim iotop bc zip unzip zlib-devel lrzsz tree screen lsof tcpdump wget ntpdate -y
2)解压
[root@centos7_3 haproxy]# tar -xvf haproxy-1.8.20.tar.gz [root@centos7_3 haproxy]# ls haproxy-1.8.20 haproxy-1.8.20.tar.gz
3)
[root@centos7_3 haproxy]# cd haproxy-1.8.20/ [root@centos7_3 haproxy-1.8.20]# make ARCH=x86_64 TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/usr/local/haproxy
4)
[root@centos7_3 haproxy-1.8.20]# make install PREFIX=/usr/local/haproxy #会创建出/usr/local/haproxy目录
5)
[root@centos7_3 haproxy-1.8.20]# ls CHANGELOG doc haproxy MAINTAINERS reg-tests src VERDATE contrib ebtree include Makefile ROADMAP SUBVERS VERSION CONTRIBUTING examples LICENSE README scripts tests [root@centos7_3 haproxy-1.8.20]# cp haproxy /usr/sbin/
6)创建启动脚本
[root@centos7_3 ~]# vim /usr/lib/systemd/system/haproxy.service [Unit] Description=HAProxyLoad Balancer After=syslog.targetnetwork.target [Service] ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid ExecReload=/bin/kill -USR2 $MAINPID [Install] WantedBy=multi-user.target
7)创建配置文件(该配置文件,从Yum安装拷贝过来的)
[root@centos7_3 haproxy-1.8.20]#mkdir /etc/haproxy [root@centos7_3 haproxy-1.8.20]# vim /etc/haproxy/haproxy.cfg global maxconn 100000 chroot /usr/local/haproxy #stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin uid 99 gid 99 daemon nbproc 2 cpu-map 1 0 cpu-map 2 1 #cpu-map 3 2 #cpu-map 4 3 pidfile /usr/local/haproxy/run/haproxy.pid log 127.0.0.1 local3 info defaults option http-keep-alive option forwardfor maxconn 100000 mode http timeout connect 300000ms timeout client 300000ms timeout server 300000ms listen stats mode http bind 0.0.0.0:9999 stats enable log global stats uri /haproxy-status stats auth haadmin:q1w2e3r4ys listen web_port bind 0.0.0.0:80 mode http log global server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5
8)启动
[root@centos7_3 haproxy-1.8.20]# pwd /data/haproxy/haproxy-1.8.20 [root@centos7_3 haproxy-1.8.20]# ./haproxy -v HA-Proxy version 1.8.20 2019/04/25 Copyright 2000-2019 Willy Tarreau <willy@haproxy.org> [root@centos7_3 haproxy-1.8.20]# systemctl status haproxy ● haproxy.service - HAProxyLoad Balancer Loaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled; vendor preset: disabled) Active: inactive (dead) [root@centos7_3 haproxy-1.8.20]# systemctl start haproxy [root@centos7_3 haproxy-1.8.20]# systemctl status haproxy ● haproxy.service - HAProxyLoad Balancer Loaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled; vendor preset: disabled) Active: active (running) since Mon 2021-04-19 19:27:52 CST; 1s ago Process: 21403 ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q (code=exited, status=0/SUCCESS) Main PID: 21407 (haproxy) Tasks: 3 CGroup: /system.slice/haproxy.service ├─21407 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid ├─21411 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid └─21412 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid Apr 19 19:27:52 centos7_3 systemd[1]: Starting HAProxyLoad Balancer... Apr 19 19:27:52 centos7_3 systemd[1]: Started HAProxyLoad Balancer. Apr 19 19:27:52 centos7_3 haproxy[21407]: [WARNING] 108/192752 (21407) : Proxy 'stats': in...st. Hint: Some lines were ellipsized, use -l to show in full. [root @ centos7 haproxy-1.8.20]#ps -ef |grep haproxy root 19428 13268 0 12:51 pts/0 00:00:00 grep --color=auto haproxy
命令:
[root@centos7_3 ~]# haproxy -h HA-Proxy version 1.8.20 2019/04/25 Copyright 2000-2019 Willy Tarreau <willy@haproxy.org> Usage : haproxy [-f <cfgfile|cfgdir>]* [ -vdVD ] [ -n <maxconn> ] [ -N <maxpconn> ] [ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ] [-- <cfgfile>*] -v displays version ; -vv shows known build options. -d enters debug mode ; -db only disables background mode. -dM[<byte>] poisons memory with <byte> (defaults to 0x50) -V enters verbose mode (disables quiet mode) -D goes daemon ; -C changes to <dir> before loading files. -W master-worker mode. -Ws master-worker mode with systemd notify support. -q quiet mode : don't display messages -c check mode : only check config files and exit -n sets the maximum total # of connections (2000) -m limits the usable amount of memory (in MB) -N sets the default, per-proxy maximum # of connections (2000) -L set local peer name (default to hostname) -p writes pids of all children to this file -de disables epoll() usage even when available -dp disables poll() usage even when available -dS disables splice usage (broken on old kernels) -dR disables SO_REUSEPORT usage -dr ignores server address resolution failures -dV disables SSL verify on servers side -sf/-st [pid ]* finishes/terminates old pids. -x <unix_socket> get listening sockets from a unix socket