haproxy负载均衡

Haproxy介绍

haproxy --- haproxy是一款高性能的负载均衡软件,因为专注做负载均衡这一块,比nginx的负载均衡更加专业主要做负载均衡的七层,也可以做4层负载均衡,负载均衡是通过OSI协议对应的。
七层负载均衡 :用的是http协议。
四层负载均衡 :用的是TCP协议加端口号做的负载均衡。

Haproxy七层负载均衡实验

192.168.20.141 主服务器

192.168.20.135 后端服务器

192.168.20.138 后端服务器

后端服务器配置:

复制代码
#两台都做
 yum -y install nginx
 echo serverweb2 > /usr/share/nginx/html/index.html 
systemctl start nginx

浏览器访问

haproxy负载均衡实验

复制代码
yum -y install haproxy  #安装haproxy
cd /etc/haproxy
cp haproxy.cfg  haproxy.cfg.back #备份haproxy配置文件
vim haproxy.cfg    #修改配置文件
global
    log         127.0.0.1 local2 info
    pidfile     /var/run/haproxy.pid
    maxconn     4000   #优先级低
    user        haproxy
    group       haproxy
    daemon               #以后台形式运行ha-proxy
    nbproc 1                #工作进程数量  cpu内核是几就写几
defaults
    mode                    http  #工作模式 http ,tcp 是 4 层,http是 7 层
    log                     global
    retries                 3   #健康检查。3次连接失败就认为服务器不可用,主要通过后面的check检查
    option                  redispatch  #服务不可用后重定向到其他健康服务器。
    maxconn                 4000  #优先级中
    contimeout              5000  #ha服务器与后端服务器连接超时时间,单位毫秒ms
    clitimeout              50000 #客户端超时
    srvtimeout              50000 #后端服务器超时
listen stats
    bind                        *:90
    stats                       enable
    stats uri                   /haproxy  #使用浏览器访问 http://192.168.246.169/haproxy,可以看到服务器状态  
    stats auth                  qianfeng:123  #用户认证,客户端使用elinks浏览器的时候不生效
frontend  web
    mode                        http  
    bind                            *:80   #监听哪个ip和什么端口
    option                  httplog             #日志类别 http 日志格式
    acl html url_reg  -i  \.html$  #1.访问控制列表名称html。规则要求访问以html结尾的url
    use_backend httpservers if  html #2.如果满足acl html规则,则推送给后端服务器httpservers
    default_backend    httpservers   #默认使用的服务器组
backend httpservers    #名字要与上面的名字必须一样
    balance     roundrobin  #负载均衡的方式
    server  http1 192.168.20.141:80 maxconn 2000 weight 1  check inter 1s rise 2 fall 2
    server  http2 192.168.20.135:80 maxconn 2000 weight 1  check inter 1s rise 2 fall 2

#启动haproxy
systemctl start haproxy

浏览器访问


查看监控

页面主要参数解释

Queue

Cur: current queued requests //当前的队列请求数量

Max:max queued requests //最大的队列请求数量

Limit: //队列限制数量

Errors

Req:request errors //错误请求

Conn:connection errors //错误的连接

Server列表:

Status:状态,包括up(后端机活动)和down(后端机挂掉)两种状态

LastChk: 持续检查后端服务器的时间

Wght: (weight) : 权重

keepalived+haproxy实验

192.168.20.136 备服务器

192.168.20.141 主服务器

192.168.20.135 后端服务器

192.168.20.138 后端服务器

复制代码
备服务器安装haproxy
yum -y install haproxy
 cd /etc/haproxy
cp haproxy.cfg  haproxy.cfg.back #备份haproxy配置文件
vim haproxy.cfg    #修改配置文件
global
    log         127.0.0.1 local2 info
    pidfile     /var/run/haproxy.pid
    maxconn     4000   #优先级低
    user        haproxy
    group       haproxy
    daemon               
    nbproc 1               
defaults
    mode                    http
    log                     global
    retries                 3 
    option                  redispatch  
    maxconn                 4000 
    contimeout              5000 ms
    clitimeout              50000 
    srvtimeout              50000 
listen stats
    bind                        *:90
    stats                       enable
    stats uri                   /haproxy  
    stats auth                  qianfeng:123 
frontend  web
    mode                        http  
    bind                            *:80  
    option                  httplog          
    acl html url_reg  -i  \.html$ 
    use_backend httpservers if  html 
    default_backend    httpservers   
backend httpservers   
    balance     roundrobin  
    server  http1 192.168.20.141:80 maxconn 2000 weight 1  check inter 1s rise 2 fall 2
    server  http2 192.168.20.135:80 maxconn 2000 weight 1  check inter 1s rise 2 fall 2

#启动haproxy
systemctl start haproxy

#主、备调度器安装软件(两台都装,两台机器只有配置文件不一样)
yum install -y keepalived
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived   #主配置文件

global_defs {
   router_id director1
}
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 80
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.20.155/24
    }
}



! Configuration File for keepalived  #次配置文件

global_defs {
   router_id directory2
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    nopreempt
    virtual_router_id 80
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.20.155/24
    }
}

#启动keepalived
systemctl start  keepalived

浏览器访问




相关推荐
润滑卫士4 分钟前
油液水分在线传感器技术对比:电容法、电阻法与微波法的测量原理与适用性
运维
Decay__4 分钟前
附-01 · MySQL 压测与容量规划 —— sysbench 基准测试、容量评估与参数基线
运维·数据库·sql·mysql·架构
mennekes22 分钟前
暗装插座箱 VS 明装插座箱,工地和车间怎么取舍
运维·网络·科技·安全·制造
叼烟扛炮22 分钟前
Linux 学习笔记(三十六):并发进阶——读写锁 vs 自旋锁,到底怎么选?
linux·运维·服务器·笔记·学习
正经教主29 分钟前
【FDE系列】阶段2:Day 43(UP详细版):Docker Compose — 多容器一键编排
运维·docker·容器
大耳朵爱学习39 分钟前
Andrew Ng深度解析:普通人如何利用AI把想法变成工具,实现工作自动化(小白程序员必看)
运维·人工智能·ai·自动化·大模型·就业·大模型学习
木雷坞40 分钟前
AnythingLLM Docker部署:中文资料上传后无引用的检查路径
运维·docker·容器
张赐荣40 分钟前
争渡读屏OCR文字/图像识别插件接口功能实现详解
运维·服务器·ocr
步行cgn1 小时前
Spring MVC 删除功能:用隐藏表单模拟 DELETE 请求的完整解析
linux·运维·服务器
润滑卫士1 小时前
工业油液在线监测系统架构:传感网络、数据采集与通讯协议设计
运维