阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Linux系统负载均衡软件之Haproxy+Apache

131次阅读
没有评论

共计 4686 个字符,预计需要花费 12 分钟才能阅读完成。

Haproxy 提供高可用性、负载均衡和基于 TCP 和 HTTP 应用的反向代理, 特别适用于那些负载特大的 web 站点, 这些站点通常又需要会话保持或七层处理。Haproxy 运行在当前的硬件上, 完全可以支持数以万计的并发连接, 并且它的运行模式使得它可以很简单安全的整合到架构中, 同时可以保护你的 web 服务器不被暴露到网络上。

环境规划:

tong1:  192.168.1.247  haproxy
tong2:  192.168.1.248  web1
tong3:  192.168.1.249  web2

1. 网络配置

tong1 节点:

[root@tong1 ~]# hostname
tong1
[root@tong1 ~]# ifconfig | grep Mask
          inet addr:192.168.1.247  Bcast:192.168.1.255  Mask:255.255.255.0
          inet addr:127.0.0.1  Mask:255.0.0.0
[root@tong1 ~]# vim /etc/hosts

192.168.1.247 tong1
192.168.1.248 tong2
192.168.1.249 tong3

[root@tong1 ~]#

tong2 节点和 tong3 节点一样配置

2. 在 tong1 节点安装 haproxy 软件, 开启 haproxy 日志功能

[root@tong1 ~]# yum install haproxy -y

[root@tong1 ~]# vim /etc/sysconfig/rsyslog          – 添加 - r 参数记录 haproxy 日志

SYSLOGD_OPTIONS=” -r -c 2″

[root@tong1 ~]# vim /etc/rsyslog.conf              – 定义日志存放的路径

local2.*                      /var/log/haproxy.log

[root@tong1 ~]# /etc/init.d/rsyslog  restart
Shutting down system logger:                              [OK]
Starting system logger:                                    [OK]
[root@tong1 ~]#

[root@tong1 ~]# cd /etc/haproxy/
[root@tong1 haproxy]# vim haproxy.cfg

global

    log        127.0.0.1 local2              – 开启日志功能

    chroot      /var/lib/haproxy            – 运行的路径
    pidfile    /var/run/haproxy.pid        –pid 文件存放处
    maxconn    4000                  – 最大连接数
    user        haproxy                  – 所属运行的用户
    group      haproxy                – 所属运行的用户组
    daemon                                – 后台运行服务

 

defaults
    mode                    http              – 处理的模式 (http 是七层,tcp 是四层)
    log                    global              – 启用全局日志记录
    option                  httplog            – 日志类别 http 格式
    option                  dontlognull        – 不记录健康检查日志
    option http-server-close             
    option forwardfor      except 127.0.0.0/8   
    option                  Redispatch        –serverId 对应的服务器挂掉后强制定向到其它服务器   

    retries                3                    –3 次连接失败就定义服务器不可用
    timeout http-request    10s
    timeout queue          1m
    timeout connect        10s    – 服务器连接超时
    timeout client          1m        – 客户端连接超时
    timeout server          1m      – 服务端连接超时
    timeout http-keep-alive 10s    – 持久连接
    timeout check          10s        – 心跳检查超时
    maxconn                3000

 

listen admin_status          – 自定义监听名, 任意取
    bind 0.0.0.0:80            – 绑定端口
    mode http                  – 模式
    log 127.0.0.1 local3 err        – 记录错误日志
    stats refresh 20s                – 每隔 20s 刷新

    stats hide-version                – 隐藏 haproxy 版本信息

    stats uri /haproxy-stats        – 在域名后面添加 /haproxy-stats 可以查看 haproxy 监控状态

    stats auth haproxy:system    – 用户名和密码    stats hide-version
    stats admin if TRUE              – 可以手动启动和停止服务

 

listen site_status                  – 检查后端主机的健康
    bind 0.0.0.0:80
    mode http
    log 127.0.0.1 local3 err
    monitor-uri /site-stats          – 使用 url 地址检查

 

frontend  main_status      – 定义 acl 规则

    bind 0.0.0.0:80      – 绑定到 80 端口

    mode http

    log global

    option httplog

    option forwardfor

    acl web1 hdr_reg(host) -i ^(www.itnihao.cn|ww1.itnihao.cn)$ 

    – 匹配 www.itnihao.cn 和 ww1.itnihao.cn 两个域名就放到 web1 变量中

    acl web2  url_sub          -i  killall=      – 请求中包含 killall= 就放入到 web2 变量中

    acl web3  path_beg      -i  /static /images /Javascript /stylesheets   

 

    use_backend  server_web3 if web3    – 满足 web3 变量的就丢到 server_web3 里面的虚拟主机

    default_backend server_web4            – 都不满足就丢到 server_web4 里面的虚拟主机

 

backend server_web3
    mode http
    balance    roundrobin
    option httpchk GET /test.html    – 定义首页 址

    server  ton1 192.168.1.248:80 check inter 1500 rise 3 fall 3 weight 1 

– 后端服务器,inter 1500 是检查心跳频率,rise 3 是 3 次正确可用,fall 3 是 3 次失败不可用,weight 1 是权重 

    server  ton2 192.168.1.249:80 check inter 1500 rise 3 fall 3 weight 1

 

backend server_web4
    mode http
    balance    roundrobin
    option httpchk GET /index.html
    server  ton3 192.168.1.248:80 check inter 1500 rise 3 fall 3 weight 1
    server  ton4 192.168.1.249:80 check inter 1500 rise 3 fall 3 weight 1

[root@tong1 haproxy]# /etc/init.d/haproxy restart
Stopping haproxy:                                          [OK]
Starting haproxy:                                          [OK]
[root@tong1 haproxy]#

 

3. 在后端主机安装 apache 服务

tong2 节点:

[root@tong2 ~]# hostname
tong2

[root@tong2 ~]# yum install httpd -y

[root@tong2 ~]# vim /etc/httpd/conf/httpd.conf

ServerName 127.0.0.1

[root@tong2 ~]# echo ‘node2’ > /var/www/html/index.html

[root@tong2 ~]# echo ‘static node2’ > /var/www/html/test.html

[root@tong2 ~]# /etc/init.d/httpd restart
Stopping httpd:                                            [OK]
Starting httpd:                                        [OK]
[root@tong2 ~]#

 

tong3 节点:

[root@tong3 ~]# hostname
tong3
[root@tong3 ~]# yum install httpd -y

[root@tong3 ~]# vim /etc/httpd/conf/httpd.conf

ServerName 127.0.0.1

[root@tong3 ~]# echo ‘node3’ > /var/www/html/index.html

[root@tong3 ~]# echo ‘static node3’ > /var/www/html/test.html

[root@tong3 ~]# /etc/init.d/httpd restart
Stopping httpd:                                            [OK]
Starting httpd:                                        [OK]
[root@tong3 ~]#

 

4. 检查 haproxy 的状态和监控

后台管理和监控 url 页面

Linux 系统负载均衡软件之 Haproxy+Apache

Linux 系统负载均衡软件之 Haproxy+Apache 

后端主机的监控页面

(1) 后端主机状态正常

Linux 系统负载均衡软件之 Haproxy+Apache

(2) 后端主机不正常, 出现宕机

Linux 系统负载均衡软件之 Haproxy+Apache

���常访问节点

Linux 系统负载均衡软件之 Haproxy+ApacheLinux 系统负载均衡软件之 Haproxy+Apache

Haproxy+Keepalived 搭建 Weblogic 高可用负载均衡集群 http://www.linuxidc.com/Linux/2013-09/89732.htm

Keepalived+HAProxy 配置高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/56748.htm

CentOS 6.3 下 Haproxy+Keepalived+Apache 配置笔记 http://www.linuxidc.com/Linux/2013-06/85598.htm

Haproxy + KeepAlived 实现 WEB 群集 on CentOS 6 http://www.linuxidc.com/Linux/2012-03/55672.htm

Haproxy+Keepalived 构建高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/55880.htm

HAproxy 的详细介绍 :请点这里
HAproxy 的下载地址 :请点这里

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计4686字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中