HAproxy 七层负载均衡
2023/8/13...大约 3 分钟
HAproxy 七层负载均衡
概述

特点

实例1
环境

172.16.100.14 web1
172.16.100.15 web2
172.16.100.21 haproxy
172.16.100.13 windows client步骤
WEB
# 优化系统
systemctl stop firewalld ;systemctl disable firewalld
sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
# ntp核对时间
# 创建测试页面
yum -y install httpd
echo $HOSTNAME > /var/www/html/index.htmlHA
yum install epel-* -y
yum install haproxy -y配置haproxy
vim /etc/haproxy/haproxy.cfghaproxy 5大部分内容

global					# 全局配置
	log 127.0.0.1 local3 info					# 日志配置 ,记录到本机
	maxconn 4096					# 最大连接限制(优先级低)
	user nobody					# 用户 uid 123
	group nobody					# 用户组 
	deamon 					# 守护进程形式运行
	nbproc 1					# haproxy的进程数,该值设置为cpu核心数一致过多的进程数导致进程崩溃
	pidfile /var/run/haproxy.pid		# pid
	
defaults
	log global					# 跟global一样
	mode http					# 工作层次 7层http或者4层tcp
	maxconn 2048				# 越往下优先级越高最大连接数(优先级中)
    retries 3					# 健康检查 二次连接失败就是不可用
    option redispatch					# 服务不可用后的操作 重新定向到其他健康服务器,重新匹配
    contimeout 5000					# (重传计时器)定义haoroxy将客户端!!!请求!!!转发至后端服务器,所等待的超时时长
    clitimeout 50000					# (向后长连接) haproxy作为服务器,和用户之间的空闲时间超时,到时候发送fin指令
	srvtimeout 50000					# (向前长连接)haproxy作为服务器,和用户之间的空闲连接的超时时间,到时候发送超时指令
	
    #timeout connect         10s # 等于contimeout
    #timeout client          1m # clitimeout
    #timeout server          1m	# srvtimeout
	option abortonclose					# 当服务器负载很高时,自动结束掉当前队列处理比较久的连接
	
	
	stats uri /admin?stats					# 设置url观察状态uri为 admin?stats
	stats realm Private lands					# 设置统计页面认证时的统计信息
	stats auth admin:password					# 设置统计页面认证的用户名和密码,如果设置多个,另起一行写入即可
	stats hide-version					# 隐藏统计页面的haproxy的版本信息
frontend http-in
	bind 0.0.0.0:80
	mode http
	log global					# 使用global设置
	option httplog				# 
	option httpclose					# 
    acl html url_reg -i \.html$					# 1.访问控制列表名称html,正则规则要求访问以html结尾的url时
    use_backend html-server if html					# 2.如果满足html规则,则推送给后端服务器html-server
    default_backend html-server
backend html-server
	mode http
	balance roundrobin
	option httpchk GET /index.html
	cookie SERVERID insert indirect nocache
	server html-A web1:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5
	server html-B web2:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5
systemctl start haproxy测试结果


实例2
拓扑

环境
haproxy
htmla
htmlb
phpa
phpbNginx七层负载均衡
特点

优势

架构



语法实例

nginx负载均衡算法

环境

案例
vim /etc/nginx/nginx.conf
upstream html {
	server web1:80;
	server web2:80;
	rr;
}
	server {
        location / {
            proxy_pass http://html;
        }
}面试:

内容概述:
       静态调度算法:
       1.rr轮询(默认调度算法)       顺序分配逐一请求
       2.wrr权重轮询算法           权重大转发次数多
       3.ip_hash                 相同ip固定转发
       动态调度算法:
       1.fair调度算法              响应时间短的优先分配
       2.least_conn               连接请求少的优先分配
       3.一致HASH 和url_hash       后台为缓存服务器时效果好状态参数

更新日志
2025/4/11 02:13
查看所有更新日志
acc20-于e62d8-于d6ac4-于392a5-于