HAProxy简介
HAProxy是基于TCP四层和HTTP七层的开源的第三方应用负载均衡软件。具有高可靠性、高稳定性、高并发处理能力、透明代理和支持ACL功能等特点。HAProxy是一个功能强大且优秀的负载均衡集群解决方案。
HAProxy负载均衡算法
Haproxy的负载均衡算法在backend模块中通过balance命令来定义,常见的算法有:
roundrobin: 轮叫调度算法,可以通过定义权值来分配后端服务器 static-rr : 静态的基于权重的轮叫调度算法 source: 源IP地址哈希算法,按请求访问的IP地址进行hash运算,将来自用一个IP地址的访问固定到同一台后端服务器 uri: URI哈希算法,按请求访问的URI进行hash运算,将请求转发到匹配的后端服务器 uri_param: 根据访问的URL路径中的参数选择后端服务器 leastconn: 最少连接调度算法,将连接请求转发到最少连接数目的后端服务器 hdr(name): 表示根据HTTP请求头进行转发 cookie: 表示根据定义的SERVERID转发请求 |
需求分析:
现有4个域名通过HAProxy代理对外提供web服务,HAProxy是集群中的前端代理和负载调度器的角色,分别代理后端的多台Tomcat、Nginx+PHP、Apache服务器(Real Server),HAProxy同时搭建了两台,分别是Master和Backup,通过Keepalved实现双机热备,防止单点故障。HAProxy和Real Server的调度规则如下:
HAProxy --> Domain1 --> 192.168.1.10 --> 192.168.1.231/192.168.1.232(Nginx+PHP) HAProxy --> Domain2 --> 192.168.1.10 --> 192.168.1.231:8080(Tomcat) HAProxy --> Domain3 --> 192.168.1.10 --> 192.168.1.232:8080(Tomcat) HAProxy --> Domain4 --> 192.168.1.10 --> 192.168.1.211:80(Apache) |
实验拓扑:
环境介绍:(系统CentOS6.7)
Hostname | IPADDR | Role | Server |
master | 192.168.1.33 | Director | haproxy-1.5.9 + keepalived |
backup | 192.168.1.34 | Director | haproxy-1.5.9 + keepalived |
host1 |
192.168.1.231 | Real Server | Tomcat Nginx PHP |
host2 | 192.168.1.232 | Real Server | Tomcat Nginx PHP |
host3 | 192.168.1.211 | Real Server | Apache MySQL |
Web server:
Domain1: www.huangming.org Domain2: web1.huangming.org Domain3: web2.huangming.org Domain4: www.localhost.com |
Step1:在Master上安装HAProxy服务
1、安装haproxy-1.5.9.tar.gz,官方地址http://www.haproxy.com
# tar zxf haproxy-1.5.9.tar.gz # cd haproxy-1.5.9 # make TARGET=linux26 PREFIX=/usr/local/haproxy # make install PREFIX=/usr/local/haproxy |
2、创建haproxy配置文件目录
# mkdir /usr/local/haproxy/conf # cp examples/haproxy.cfg /usr/local/haproxy/conf/ |
HAproxy的配置文件
HAproxy配置文件主要由5个部分组成:
1、global部分:用来设定全局配置参数,属于进程级,通常和操作系统配置有关
2、defaults部分:默认参数配置部分。在此部分下,默认会自动引用到下面的frontend、backend和listen部分中,因此如果某些参数属于公用的配置,只需在defaults部分添加一次即可。
3、frontend部分:用于设置接收用户请求的前端虚拟节点。frontend是在HAProxy1.3版本之后才引入的一个组件,同时引入的还有backend组件。frontend可以根据ACL规则直接指定要使用的后端backend
4、backend部分:此部分用于设置集群后端服务器集群的配置,也就是用来添加一组真实服务器,以处理前端用户的请求,添加的真实服务器类似于LVS中的real server节点。
5、listen部分:此部分是frontend和backend部分的结合体。在HAProxy1.3版本之前,HAProxy的所有配置选项都在这个部分中设置。
Step2:在Master上安装keepalived
1、下载keepalived最新版
# wget http://www.keepalived.org/software/keepalived-1.2.20.tar.gz |
2、安装keepalived
# yum install pcre-devel openssl-devel popt-devel libnl-devel -y # yum install libnfnetlink libnfnetlink-devel -y # tar zxf keepalived-1.2.20.tar.gz # cd keepalived-1.2.20 # ./configure --prefix=/usr/local/keepalived --sysconf=/etc --with-kernel-dir=/usr/src/kernels/2.6.32-573.el6.x86_64 # make && make install # ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/ |
3、启动keepalived,设置开启启动
[[email protected] haproxy]# service keepalived start [[email protected] haproxy]# chkconfig --add keepalived [[email protected] haproxy]# chkconfig keepalived on [[email protected] haproxy]# chkconfig --list keepalived keepalived 0:off 1:off 2:on 3:on 4:on 5:on 6:off
Step3:在Backup上安装haproxy和keepalived
[[email protected] ~]# ls /usr/local/haproxy/ conf doc sbin share [[email protected] ~]# ls /usr/local/keepalived/ bin sbin share [[email protected] ~]# ls /etc/keepalived/ keepalived.conf samples [[email protected] ~]# service keepalived start ;启动keepalived |
Step4:配置HAProxy
global log 127.0.0.1 local0 info ;日志级别(err/warning/info/debug) maxconn 4096 ;每个haproxy进程可接受的最大并发连接数(等于ulimit -n) uid nobody ;运行haproxy进程的用户 gid nobody ;运行haproxy进程的组 daemon ;设置haproxy启动时进入后台运行 nbproc 1 ;设置haproxy启动时可创建的进程数,默认为1,应该设置为小于CPU核数 pidfile /usr/local/haproxy/logs/haproxy.pid ;HAProxy进程pid文件 defaults mode http ;设置haproxy默认的运行模式(tcp、http、health) retries 3 ;设置连接后端服务器的失败重试次数 maxconn 20000 ;设置开启haproxy时允许的最大并发连接总数,该数值可以也在运行时(-n)指定 timeout connect 10s ;设置成功连接后端服务器的最长等待时间 timeout client 30s ;客户端发送数据时的最长等待时间 timeout server 30s ;服务器端回应客户端数据发送的最长等待时间 timeout check 2s ;对后端服务器的检测超时时间 listen admin_stats ;HAProxy监测页面的配置 bind 0.0.0.0:19088 ;定义监听的IP地址和端口 mode http log 127.0.0.1 local0 err stats refresh 30s stats uri /haproxy-status stats realm welcome login\ Haproxy stats auth admin:123456abc stats hide-version stats admin if TRUE frontend www ;定义域名相关的配置 bind *:80 mode http option httplog option forwardfor log global acl host_www hdr_reg(host) -i ^(www.huangming.org|huangming.org) ;定义ALC规则 acl host_web1 hdr_dom(host) -i web1.huangming.org acl host_web2 hdr_beg(host) -i web2. acl host_localhost hdr_dom(host) -i www.localhost.com #ACL规则定义的方式有hdr_reg(host)、hdr_dom(host)、hdr_beg(host)、url_sub、url_dir、path_beg、path_end等,-i表示不匹配大小写 use_backend server_www if host_www use_backend server_web1 if host_web1 use_backend server_web2 if host_web2 use_backend server_localhost if host_localhost default_backend server_default backend server_www ;后端真实服务器组 mode http option redispatch option abortonclose balance source cookie SERVERID option httpchk GET /forum.php server www1 192.168.1.231:80 cookie www1 weight 5 check inter 2000 rise 2 fall 3 server www2 192.168.1.232:80 cookie www2 weight 5 check inter 2000 rise 2 fall 3 backend server_web1 ;后端真实服务器组 mode http option redispatch option abortonclose balance source cookie SERVERID option httpchk GET /index.jsp server web1 192.168.1.231:8080 cookie web1 weight 7 check inter 2000 rise 2 fall 3 backend server_web2 ;后端真实服务器组 mode http option redispatch option abortonclose balance source cookie SERVERID option httpchk GET /index.jsp server web2 192.168.1.232:8080 cookie web2 weight 7 check inter 2000 rise 2 fall 3 backend server_localhost ;后端真实服务器组 mode http option redispatch option abortonclose balance roundrobin cookie SERVERID option httpchk GET /index.html server localhost 192.168.1.211:80 cookie server_id weight 3 check inter 2000 rise 2 fall 3 backend server_default ;默认连接的服务器组 mode http option redispatch option abortonclose balance source cookie SERVERID option httpchk HEAD /forum.php server default1 192.168.1.231:80 cookie default1 weight 2 check inter 2000 rise 2 fall 3 server default2 192.168.1.232:80 cookie default2 weight 2 check inter 2000 rise 2 fall 3 |
Step5:配置Keepalved
global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id HAProxy_DEVEL } vrrp_script check_haproxy { script "killall -0 haproxy" interval 2 weight 30 } vrrp_instance HAProxy_HA { state BACKUP ;主备均设置为BACKUP interface eth0 virtual_router_id 80 priority 100 ;备上设置90 advert_int 2 nopreempt authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.10/24 dev eth0 } track_script { check_haproxy } notify_master "/etc/keepalived/master.sh" ;切换为主时执行的脚本 notify_backup "/etc/keepalived/backup.sh" ;切换为从时执行的脚本 notify_fault "/etc/keepalived/fault.sh" ;切换为fault时执行的脚本 } |
Step6:在Backup上配置HAProxy和Keepalved
1、将Master上配置好的haproxy.cfg、keepalived.conf发送到Backup上对应的路径下,并将priority改为90
[[email protected] haproxy]# rsync -av conf/haproxy.cfg 192.168.1.34:/usr/local/haproxy/conf/ [[email protected] keepalived]# rsync -av keepalived.conf 192.168.1.34:/etc/keepalived/
Step7:启动HAProxy
1、默认启动
[[email protected] ~]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg |
2、查看haproxy监听的端口
[[email protected] ~]# netstat -ntlp |grep haproxy tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 67498/haproxy tcp 0 0 0.0.0.0:19088 0.0.0.0:* LISTEN 67498/haproxy |
3、指定并发连接数启动HAProxy
[[email protected] ~]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg -n 50000 |
4、登陆HAProxy监测页面
Step8:启动Keepalved
[[email protected] ~]# service keepalived restart Stopping keepalived: [ OK ] Starting keepalived: [ OK ] |
[[email protected] ~]# service keepalived restart Stopping keepalived: [ OK ] Starting keepalived: [ OK ] |
1、在Master上查看VIP
[[email protected] ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:bd:50:ec brd ff:ff:ff:ff:ff:ff inet 192.168.1.33/24 brd 192.168.1.255 scope global eth0 inet 192.168.1.10/24 scope global secondary eth0 inet6 fe80::20c:29ff:febd:50ec/64 scope link valid_lft forever preferred_lft forever |
2、测试VIP是否正常提供服务
3、测试keepalived是否实现故障转移功能
1、在Master上停止keepalived服务 [[email protected] ~]# service keepalived stop Stopping keepalived: [ OK ] |
2、在backup上查看是否接管VIP [[email protected] ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:f7:ad:3d brd ff:ff:ff:ff:ff:ff inet 192.168.1.34/24 brd 192.168.1.255 scope global eth0 inet 192.168.1.10/24 scope global secondary eth0 inet6 fe80::20c:29ff:fef7:ad3d/64 scope link valid_lft forever preferred_lft forever |
Step9:测试通过域名访问HAProxy,测试是否实现转发和负载功能
1、通过VIP对全部域名的解析,并测试访问
http://www.huangming.org
http://web1.huangming.org
http://web2.huangming.org
http://www.localhost.com
默认转发的后端web服务器
Step10:HAProxy的日志功能配置
1、创建haproxy.conf文件 [[email protected] ~]# vim /etc/rsyslog.d/haproxy.conf $ModLoad imudp $UDPServerRun 514 local3.* /usr/local/haproxy/logs/haproxy.log local0.* /usr/local/haproxy/logs/haproxy.log 2、修改/etc/sysconfig/rsyslog [[email protected] ~]# vim /etc/sysconfig/rsyslog SYSLOGD_OPTIONS="-c 2 -r -m 0" ;-r表示接收远程日志 3、重启rsyslog服务 [[email protected] ~]# service rsyslog restart |
# cat /usr/local/haproxy/logs/haproxy.log
Step11:HAProxy启动常用参数选项
-v: 显示版本
-d: 以debug模式运行
-db: 表示禁用后台启动模式
-D: 表示以后台的模式启动
-q: 表示安静模式,程序运行不输出任何信息
-c: 对HAProxy配置文件进行语法检查
-n: 设置最大并发连接数
-m: 限制可用内存大小,单位MB
-N: 设置默认连接数
-p: 设置HAProxy运行的PID文件
-de: 表示不使用epoll模型
-ds: 不使用speculative epoll
-dp: 不使用poll模型
-sf: 程序启动后向PID文件的进程发送FINISH信号
-st: 程序启动后向PID文件的进程发送TERMINATE信号,用于重启HAProxy