集群工具之HAProxy

集群工具之HAProxy

HAProxy简介

  • 它是一款实现负载均衡的调度器
  • 适用于负载特别大的web站点
  • HAProxy的工作模式
    • mode http:只适用于web服务
    • mode tcp:适用于各种服务
    • mode health:仅做健康检查,很少使用

配置HAProxy

haproxy

  • clienteth0->192.168.88.10
  • haproxyeth0->192.168.88.50;eth1->192.168.99.50
  • web1eth0->192.168.99.100
  • web2eth0->192.168.99.200

环境准备

  • client
# 配置ip
nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.88.10/24 ipv4.gateway 192.168.88.254 connection.autoconnect yes
  • HAProxy
# 配置ip
nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.88.50/24 ipv4.gateway 192.168.88.254 autoconnect yes
nmcli connection up eth0

nmcli connection modify eth1 ipv4.method manual ipv4.addresses 192.168.99.50/24 ipv4.gateway 192.168.99.254 autoconnect yes
nmcli connection up eth1

# 安装haproxy
yum install -y haproxy
  • web1
# 配置ip
nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.99.100/24 ipv4.gateway 192.168.88.254 connection.autoconnect yes

# 开启httpd服务
systemctl start httpd
echo "web1" > /var/www/html/index.html
  • web2
# 配置ip
nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.99.200/24 ipv4.gateway 192.168.88.254 connection.autoconnect yes

# 开启httpd服务
systemctl start httpd
echo "web2" > /var/www/html/index.html

相关配置

  • haproxy
# 修改haproxy服务的配置文件
vim /etc/haproxy/haproxy.cfg
# 文件内容如下,63行之前的不需要改动,将63行后面的全部删除,包括63行,替换成下面的内容
listen myweb 0.0.0.0:80  # 定义本机监听地址
    balance roundrobin  # 调度算法为轮巡
    server web1 192.168.99.100 check inter 2000 rise 2 fall 5  # web1服务器健康检查,2000ms检查一次,连续2次成功,代表健康,连续5次失败,代表服务器宕机
    server web2 192.168.99.200 check inter 2000 rise 2 fall 5  # web2服务器健康检查,2000ms检查一次,连续2次成功,代表健康,连续5次失败,代表服务器宕机

listen stats 0.0.0.0:1080  # 定义监控地址的端口
    stats refresh 30s  # 监控页面30s自动刷新
    stats uri /stats  # 定义监控的uri地址是/stats
    stats auth bhlu:1234  # 定义监控页面的用户名是bhlu,密码是1234
    
# 启动服务
systemctl start haproxy

# 查看服务是否正常启动,如果有报错-l可以全部显示出来
systemctl status haproxy -l

效果演示

  • client
for i in {1..6}; do curl http://192.168.88.50; done
# web1 web2 web1 web2 web1 web2
  • 访问监控地址

haproxy-stats

补充:负载均衡调度器的简单比较

  1. nginx:可以工作在第4层和第7层。可以根据url进行负载均衡。正则表达式支持的更广泛。
  2. lvs:效率最高。工作在第4层。
  3. haproxy:可以工作在第4层和第7层。可以根据url进行负载均衡。支持有限的正则表达式,属于适中的状态。

相关推荐

  1. HAproxy

    2024-04-24 11:22:05       28 阅读
  2. Haproxy 负载均衡

    2024-04-24 11:22:05       17 阅读
  3. Haproxy 搭建 web

    2024-04-24 11:22:05       8 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-24 11:22:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-24 11:22:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-24 11:22:05       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-24 11:22:05       20 阅读

热门阅读

  1. vue-json-editor

    2024-04-24 11:22:05       12 阅读
  2. map_or

    2024-04-24 11:22:05       13 阅读
  3. 如何对同一docker注册表使用多个身份验证/登录

    2024-04-24 11:22:05       14 阅读
  4. 深入Spring Boot配置机制:如何高效管理应用配置

    2024-04-24 11:22:05       14 阅读
  5. C++笔记

    C++笔记

    2024-04-24 11:22:05      12 阅读
  6. web server apache tomcat11-16-mbean

    2024-04-24 11:22:05       15 阅读