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

浏览器访问




相关推荐
阿里云大数据AI技术5 小时前
阿里云 EMR AI 助手正式发布:从问答工具到全栈智能运维助手
运维·人工智能
SkyWalking中文站1 天前
认识 Horizon UI · 6/17:Trace 探索器
运维·监控·自动化运维
火车叼位1 天前
写给初级开发者:SSL、SSH、HTTPS 与证书体系全解析
运维
小猿姐2 天前
唯品会大规模数据库云原生实践:基于 KubeBlocks 管理数千实例的统一运维之路
运维·elasticsearch·云原生
SkyWalking中文站2 天前
认识 Horizon UI · 5/17:3D 基础设施地图
运维·监控·自动化运维
SkyWalking中文站3 天前
认识 Horizon UI · 1/17:SkyWalking 新一代可观测性控制台
运维·前端·监控
雪梨酱QAQ3 天前
Kubeneters HA Cluster部署
运维
江华森3 天前
Spring Cloud 微服务全栈实战:从 Eureka 到 Docker Compose 一文贯通
运维
江华森3 天前
Matplotlib 数据绘图基础入门
运维
江华森3 天前
NumPy 数值计算基础入门
运维