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 小时前
基于华为openEuler系统部署Gitblit服务器
运维·服务器·华为
EnglishJun5 小时前
Linux系统编程(二)---学习Linux系统函数
linux·运维·学习
小Pawn爷5 小时前
2.Docker的存储
运维·docker·容器
CaracalTiger5 小时前
OpenClaw-VSCode:在 VS Code 中通过 WebSocket 远程管理 OpenClaw 网关的完整方案
运维·ide·人工智能·vscode·websocket·开源·编辑器
qq_5470261795 小时前
LangChain 1.0 核心概念
运维·服务器·langchain
生而为虫5 小时前
[Windows] 【浏览器自动化精灵V1.0】用Excel表格控制浏览器的自动化
运维·自动化
Fcy6485 小时前
Linux下 进程(二)(进程状态、僵尸进程和孤儿进程)
linux·运维·服务器·僵尸进程·孤儿进程·进程状态
第七序章5 小时前
【Linux学习笔记】初识Linux —— 理解gcc编译器
linux·运维·服务器·开发语言·人工智能·笔记·学习
迎仔5 小时前
A-总览:GPU驱动运维系列总览
linux·运维
AI_56786 小时前
阿里云OSS成本优化:生命周期规则+分层存储省70%
运维·数据库·人工智能·ai