HAProxy 部署/配置 负载均衡 Centos

  • 负载均衡算上这篇一共写了3个了…分别是 Nginx/LVS/HAProxy
  • 简单的做下比较就是:
  • 功能 : Nginx > HAProxy > LVS
  • 性能 : LVS > HAProxy > Nginx

HAProxy概述

HAProxy简介

  • 适用于那些负载特大的Web站点,这些站点通畅又需要会话保持或7层代理
  • 提供高可用性、负载均衡以及基于TCP和HTTP应用的代理

衡量负载均衡器技能的因素

  • Session rete 会话率
    • 每秒钟产生的会话数
  • Session concurrency 并发数会话
    • 服务器处理会话的时间越长,并发会话数越多
  • Data rete
    • 以MB/s或Mbps衡量
    • 大的对象导致并发会话数增加
    • 高会话数、高数据速率要求更多的内存

HAProxy工作模式

  • mode http
    • 客户端请求被深度分析后再发往服务器
  • mode tcp
    • 4层调度,不检查第七层信息
  • mode health
    • 仅做健康状态检查,已经不建议使用

HAProxy部署/配置

问题

  • 准备4台Linux服务器,两台做Web服务器,1台安装HAProxy,1台做客户端,实现如下功能

    • 客户端访问HAProxy,HAProxy分发请求到后端Real Server
    • 开启HAProxy监控页面,及时查看调度器状态
    • 设置HAProxy为开机启动

方案

  • **使用4台虚拟机,1台作为HAProxy调度器、2台作为Real Server、1台作为客户端,拓扑结构如图-1所示,具体配置如表-1所示。
    图1
    表1

1.准备环境

1.1 配置Web1/Web2/proxy IP地址

[root@web1 ~]# nmcli connection modify eth1 ipv4.method manual \
ipv4.addresses 192.168.2.100/24 connection.autoconnect yes
[root@web1 ~]# nmcli connection up eth1

[root@web2 ~]# nmcli connection modify eth1 ipv4.method manual \
ipv4.addresses 192.168.2.200/24 connection.autoconnect yes
[root@web2 ~]# nmcli connection up eth1

[root@proxy ~]# nmcli connection modify eth0 ipv4.method manual \
ipv4.addresses 192.168.4.5/24 connection.autoconnect yes
[root@proxy ~]# nmcli connection up eth0
[root@proxy ~]# nmcli connection modify eth1 ipv4.method manual \
ipv4.addresses 192.168.2.5/24 connection.autoconnect yes
[root@proxy ~]# nmcli connection up eth1

1.2 配置后端Web服务器,设置两台后端Web服务

[root@web1 ~]# yum -y install httpd
[root@web1 ~]# systemctl start httpd
[root@web1 ~]# echo "192.168.2.100" > /var/www/html/index.html
[root@web2 ~]# yum -y install httpd
[root@web2 ~]# systemctl start httpd
[root@web2 ~]# echo "192.168.2.200" > /var/www/html/index.html

2.部署HAProxy服务器

2.1安装软件

[root@proxy ~]# yum -y install haproxy

2.2修改配置文件

[root@proxy ~]# vim /etc/haproxy/haproxy.cfg
	global
	 log 127.0.0.1 local2   ##[err warning info debug]
	 chroot /usr/local/haproxy
	 pidfile /var/run/haproxy.pid ##haproxy的pid存放路径
	 maxconn 4000     ##最大连接数,默认4000
	 user haproxy
	 group haproxy
	 daemon       ##创建1个进程进入deamon模式运行
	defaults
	 mode http    ##默认的模式mode { tcp|http|health } 
	option dontlognull  ##不记录健康检查的日志信息
	 option httpclose  ##每次请求完毕后主动关闭http通道
	 option httplog   ##日志类别http日志格式
	 option forwardfor  ##后端服务器可以从Http Header中获得客户端ip
	 option redispatch  ##serverid服务器挂掉后强制定向到其他健康服务器
	 timeout connect 10000 #如果backend没有指定,默认为10s
	 timeout client 300000 ##客户端连接超时
	 timeout server 300000 ##服务器连接超时
	 maxconn  3000  ##最大连接数
	 retries  3   ##3次连接失败就认为服务不可用,也可以通过后面设置
	listen stats 0.0.0.0:1080   #监听端口
	    stats refresh 30s   #统计页面自动刷新时间
	    stats uri /stats   #统计页面url
	    stats realm Haproxy Manager #进入管理解面查看状态信息
	    stats auth admin:admin  #统计页面用户名和密码设置
	  
	listen  webs *:80		#集群名,监听端口
	   balance roundrobin		#均衡模式
	   server  web1 192.168.2.100:80 check
	   server  web2 192.168.2.200:80 check

2.3启动服务器并设置开机启动

[root@proxy ~]# systemctl start haproxy
[root@proxy ~]# systemctl enable haproxy

2.4客户端验证

  • 客户端配置与HAProxy相同网络的IP地址,并使用火狐浏览器访问http://192.168.4.5,测试调度器是否正常工作,客户端访问http://192.168.4.5:1080/stats测试状态监控页面是否正常。访问状态监控页的内容,参考图-2所示。
    图2

备注:
Queue队列数据的信息(当前队列数量,最大值,队列限制数量);
Session rate每秒会话率(当前值,最大值,限制数量);
Sessions总会话量(当前值,最大值,总量,Lbtot: total number of times a server was selected选中一台服务器所用的总时间);
Bytes(入站、出站流量);
Denied(拒绝请求、拒绝回应);
Errors(错误请求、错误连接、错误回应);
Warnings(重新尝试警告retry、重新连接redispatches);
Server(状态、最后检查的时间(多久前执行的最后一次检查)、权重、备份服务器数量、down机服务器数量、down机时长)。

发布了8 篇原创文章 · 获赞 9 · 访问量 3417

猜你喜欢

转载自blog.csdn.net/wrtwen/article/details/104867999