HAproxy部署配置

HAproxy部署配置

拓扑图

说明:

haproxy服务器IP:172.16.253.200/16 (外网)、192.168.29.140/24(内网)
博客服务器组IP:192.168.29.130/24、192.168.29.131/24
网站服务器组IP:192.168.29.120/24、192.168.29.121/24
默认服务器组IP:192.168.29.110/24、192.168.29.111/24

一 HAProxy主机配置

[root@local ~]# yum install haproxy -y
[root@local ~]# vim   /etc/haproxy/haproxy.cfg

1 global部分

用来设定全局配置参数,属于进程级的配置,通常和操作系统配置有关。

global
    log         127.0.0.1 local2 info		##全局日志配置,local2为日志设备,info为日志级别
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid		##指定HAProxy进程的Pid文件,启动进程的用户必须有访问次文件的权限
    maxconn     4000		##设定每个进程可接受的最大并发连接数
    user        haproxy		##设置运行haproxy进程的用户,可使用UID代替
    group       haproxy		##设置运行haproxy进程的组,可使用GID代替
    daemon			##设置haproxy进程进入后台运行
   
stats socket /var/lib/haproxy/stats

2 default部分

默认参数的配置部分。在次部分配置的参数值,默认会自动引用到下面frontend、backend、listen部分中,因此,如果某些参数属于公共的配置,只需在default部分添加一次即可。而如果在frontend、backend、和listen部分也配置了与default部分一样的参数,那么default部分的参数对应的值自动被覆盖。

defaults
    mode                    http		##设置haproxy实例默认运行模式,有tcp、http、health三个值
    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         5s
    timeout client          10s		##设置连接客户端发送数据时的最长等待时间,默认单位为毫秒
    timeout server          10s		##设置服务器端回应客户端数据发送的最长等待时间,默认单位为毫秒
    timeout http-keep-alive 10s	##持久连接的持久时长
    timeout check           2s		##设置对后端服务器的监测超时时间,默认单位为毫秒
maxconn                 3000

3 listen部分

此部分是frontend和backend部分的结合体

listen	admin_stats
	bind	0.0.0.0:19088			##设置监控统计页面的监听的IP和端口
	mode	http
	log	127.0.0.1 local2 err
	stats	refresh	30s			##设置哈haproxy监控统计页面的自动刷新时间
	stats	uri	/haproxy-status	##设置haproxy监控统计页面的URL路径,可随意指定,例如“stats uri  /haproxy-status”,就可以通过http://IP:PORT//haproxy-status查看
	stats	realm	welcome	login\ Haproxy 		##设置登录haproxy统计页面是密码框上的提示信息
	stats	auth	admin:admin		##设置登录统计页面的用户名和密码,可同时设置多个,每行一个
	stats	hide-version			##用来隐藏统计页面上haproxy的版本信息
	stats	admin	if	TRUE		##通过设置此选项,可以在监控页面上手工启动或者禁用后端服务器

4 frontend部分

此部分用于设置接收用户请求的前端虚拟节点。frontend可以根据ACL规则直接指定要使用的后端backend。
(1)frontend部分

frontend  www
	bind	*:80		##此选项只能在frontend和listen部分使用,用于定义一个或多个监听的套接字。格式为:bind  [<address>:<port_range>]  interface   <interface>
	mode	http		##实例的运行模式
	option	httplog		##启用日志记录http请求。默认haproxy日志不记录http请求
	option	forwardfor	##在由haproxy发往后端主机的请求报文中添加“X-Forwarded-For”首部,其值前端客户端的地址;用于向后端主发送真实的客户端IP 
	option	httpclose	##次选项表示在客户端和服务器端完成一次连接请求后,haproxy自动关闭此TCP连接
	log	global		##表示使用全局的日志级别,引用global部分定义的log配置

(2)通过haproxy的ACL规则实现智能负载均衡
haproxy通过ACL完成两个主要的功能:

	1)通过设置ACL的规则检查客户端请求是否合法。如果符合ACL规则要求,那么将放行,如果不符合规则,则直接中断请求。
	2)符合ACL规则要求的请求将被提交到后端的backend服务器集群,进而实现基于ACL规则的负载均衡
	3)haproxy的ACL规则通常用在frontend部分中

格式为:acl 自定义的acl名称 acl方法 -i [ 匹配的路径或文件]

	acl:为关键字,表示定义ACL规则的开始,后面跟上自定义的ACL名称。
	acl方法:这个字段定义ACL的方法,haproxy定义了很多ACL方法,常用的有hdr_reg(host)、hdr_dom(host)、hdr_beg(host)、url_sub、url_dir、path_beg、path_end等
	-i:表示不区分大小写,后面跟上要匹配的路径、文件或正则表达式。
	说明:与ACL规则一起使用的haproxy参数还有use_backend,use_backend  后面跟实例名,表示在满足ACL规则后去哪里请求哪个backend实例
	acl host_www	hdr_reg(host) -i  ^(www.tb.com|tb.com) 	##正则表达式匹配,表示如果开都有www.tb.com或tb.com 
	acl host_blog	hdr_beg(host) -i blog.			##表示如果前缀包含blog.字符,则匹配
	use_backend server_www if host_www		##表示如果满足host_www 则去请求后端的server_www主机
	use_backend server_blog if host_blog
	default_backend server_default		##表示如果前面都不匹配,则去请求server_default主机

5 backend部分

此部分用于设置后端服务器集群的配置,也是用来添加一组真实服务器,以处理客户端的请求。

backend server_default		定义后端主机,格式为:backend  SERVER_NAME
	mode	http			##实例的运行模式
	option	redispatch		##次参数用于cookie保持的环境中
	option	abortonclose		##自动结束当前队列中处理时间较长的进程
	balance  roundrobin		##定义负载均衡算法,roundrobin为加权轮询算法
    	cookie	SERVERID		##表示允许向cookie中插入SERVERID
	option httpchk GET /check_status.html	##启用http的服务监测功能,采用GET方式,通过监测check_status,html页面的状态来确定服务器的状态
	server default1 192.168.29.110:80 cookie default1 weight 2 check inter 2000 rise 2 fall 3	
	server default2 192.168.29.111:80 cookie default2 weight 2 check inter 2000 rise 2 fall 3	
定义后端主机,格式为:server  <name>  <address>[:port]  [param*]
	param为一系列参数,
	cookie为当前server指定其cookie值,用于实现基于cookie的会话黏性
	weight 设置后端服务器的权重,
	check表示启用后端服务器执行健康状态监测,
	rise设置从故障状态转换到正常状态需要成功检查的次数,
	fall设置后端服务器从从正常状态转换为不可以状态需要检查的次数
	disabled:标记为不可用;
	redir <prefix>:将发往此server的所有GET和HEAD类的请求重定向至指定的URL
backend server_www
	mode	http
	option	redispatch
	option	abortonclose
	balance	source		##定义负载均衡算法,source为基于源IP 的算法,可以使同一客户端IP的请求始终被转发至某台特定的后端服务器
    	cookie	SERVERID	
	option httpchk GET /check_status.jsp
	server www1 192.168.29.120:80 cookie www1 weight 6 check inter 2000 rise 2 fall 3
	server www2 192.168.29.121:80 cookie www2 weight 6 check inter 2000 rise 2 fall 3
backend server_blog
	mode	http
	option	redispatch
	option	abortonclose
	balance	roundrobin
    	cookie	SERVERID	
	option httpchk GET /check_blog.php
	server blog1 192.168.29.130:80 cookie blog1 weight 5 check inter 2000 rise 2 fall 3
	server blog2 192.168.29.131:80 cookie blog2 weight 5 check inter 2000 rise 2 fall 3

二 后端服务器配置

1 默认服务器配置

(1)虚拟主机配置

[root@local ~]# vim /etc/httpd/conf.d/vhost.conf
<VirtualHost 192.168.29.110:80>
        DocumentRoot "/data/web1"
<Directory "/data/web1/">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
</VirtualHost>

(2)状态监测页配置

[root@local ~]# echo "This is check_status.page ip:192.168.29.110" > /data/web1/check_status.html

(3)默认页面配置

[root@local ~]# echo "This is default page ip:192.168.29.110" > /data/web1/index.html

按照以上方式配置192.168.29.111主机

2 网站服务器配置

(1)虚拟主机配置

[root@local ~]# vim /etc/httpd/conf.d/vhost.conf 
<VirtualHost 192.168.29.120:80>
        DocumentRoot "/data/web1"
        ServerName www.tb.com
<Directory "/data/web1/">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
</VirtualHost>

(2)状态监测页配置

[root@local ~]# echo "This is check_status page ip:192.168.29.120" > /data/web1/check_status.jsp

(3)默认页面配置

[root@local ~]# echo "This is www page ip:192.168.29.120" > /data/web1/index.html

按照以上方式配置192.168.29.121主机

3 博客服务器组

(1)配置虚拟主机

[root@local ~]# vim /etc/httpd/conf.d/vhost.conf 
<VirtualHost 192.168.29.130:80>
        DocumentRoot "/data/web1"
        ServerName www.blog.tb.com
<Directory "/data/web1/">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
</VirtualHost>

(2)状态监测页配置

[root@local ~]# echo "This is check_status page ip:192.168.29.130" > /data/web1/check_blog.php

(3)默认页面配置

[root@local ~]# echo "This is blog page ip:192.168.29.130" > /data/web1/index.html

按照以上方式配置192.168.29.131主机

三 测试

1 启动haproxy

[root@local ~]# systemctl start haproxy

2 测试

(1)访问网站服务器

[root@local ~]# for i in {1..10} ; do curl www.tb.com ; done
This is www page ip:192.168.29.120
This is www page ip:192.168.29.120
This is www page ip:192.168.29.120
This is www page ip:192.168.29.120
This is www page ip:192.168.29.120
This is www page ip:192.168.29.120
This is www page ip:192.168.29.120
This is www page ip:192.168.29.120
This is www page ip:192.168.29.120
This is www page ip:192.168.29.120

由于有会话粘性,所有都是用一台服务器响应请求
(2)访问blog服务器

[root@local ~]# for i in {1..10} ; do curl blog.tb.com ; done
This is blog page ip:192.168.29.131
This is blog page ip:192.168.29.130
This is blog page ip:192.168.29.131
This is blog page ip:192.168.29.130
This is blog page ip:192.168.29.131
This is blog page ip:192.168.29.130
This is blog page ip:192.168.29.131
This is blog page ip:192.168.29.130
This is blog page ip:192.168.29.131
This is blog page ip:192.168.29.130

可以看到,轮询算法生效了
(3)测试默认服务器

[root@local ~]# for i in {1..10} ; do curl 172.16.253.200 ; done
This is default page ip:192.168.29.110
This is default page ip:192.168.29.111
This is default page ip:192.168.29.110
This is default page ip:192.168.29.111
This is default page ip:192.168.29.110
This is default page ip:192.168.29.111
This is default page ip:192.168.29.110
This is default page ip:192.168.29.111
This is default page ip:192.168.29.110
This is default page ip:192.168.29.111

成功了

3 haproxy状态监测页面

(1)输入http://172.16.253.200:19088/haproxy-status登录状态监测页
(2)输入用户名密码

(3)登录成功

posted @ 2017-08-07 19:10  Sunzz  阅读(675)  评论(0编辑  收藏  举报