博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
haproxy调度web案例
阅读量:5990 次
发布时间:2019-06-20

本文共 6749 字,大约阅读时间需要 22 分钟。

一、实验架构图

haproxy调度web案例

haproxy通过ACL规则来实现智能负载均衡

当输入bbs.linux.com时,haproxy将请求转发到后端bbs服务器
当输入blog.linux.com时,haproxy将请求转发到后端blog服务器
前端两台haproxy服务器通过部署keepalived来实现haproxy高可用

二、准备工作

系统版本:centos7

haproxy服务器
haproxy01 192.168.122.100
haproxy02 192.168.122.200
bbs服务器
web01 192.168.122.101
web02 192.168.166.102
blog服务器
web03 192.168.122.103
web04 192.168.122.104
关闭firewalld和SElinux,各主机均为固定ip

三、部署bbs服务器和blog服务器的测试页面

安装httpd并启动

[root@web01 ~]# yum install -y httpd[root@web01 ~]# echo "web01.bbs.com" >/var/www/html/index.html[root@web01 ~]# systemctl start httpd[root@web01 ~]# systemctl enable httpd

web01与web02测试页面分别为web01.bbs.com和web02.bbs.com

web03与web04测试页面分别为web03.blog.com和web04.blog.com

四、在haproxy01主机上安装haproxy,并进行配置

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

配置文件所在位置

[root@haproxy01 ~]# rpm -qc haproxy/etc/haproxy/haproxy.cfg/etc/logrotate.d/haproxy

打开并编辑配置文件

[root@haproxy01 ~]# vim /etc/haproxy/haproxy.cfg
global        maxconn 2000        nbproc 1        user nobody        group nobody        log 127.0.0.1 local0 info        daemon        pidfile /var/run/haproxy.piddefaults        mode http        retries 3        timeout connect 5s        timeout client 10s        timeout server 30s        timeout check 2slisten admin_status        bind 0.0.0.0:9088        mode http        log 127.0.0.1 local0 err        stats refresh 30s        stats uri /haproxy-status        stats realm welcome login        stats auth admin:admin        stats hide-version        stats admin if TRUEfrontend test-proxy        bind *:80        mode http        log global        option httplog        option forwardfor        acl host_bbs hdr_dom(host) -i bbs.linux.com        acl host_blog hdr_beg(host) -i blog.        use_backend server_bbs if host_bbs        use_backend server_blog if host_blogbackend server_bbs        mode http        option redispatch        cookie SERVERID        option abortonclose        balance roundrobin        server bbs01 192.168.122.101:80 cookie bbs01 weight 3 check inter 2000 rise 1 fall 2        server bbs02 192.168.122.102:80 cookie bbs02 weight 3 check inter 2000 rise 1 fall 2backend server_blog        mode http        option redispatch        cookie SERVERID        option abortonclose        balance roundrobin        server blog01 192.168.122.103:80 cookie blog01 weight 3 check inter 2000 rise 1 fall 2        server blog02 192.168.122.104:80 cookie blog02 weight 3 check inter 2000 rise 1 fall 2

启动haproxy服务

[root@haproxy01 ~]# systemctl start haproxy[root@haproxy01 ~]# ss -antp |grep haproxyLISTEN     0      128                       *:9088                     *:*      users:(("haproxy",8742,4),("haproxy",8741,4))LISTEN     0      128                       *:80                       *:*      users:(("haproxy",8742,6),("haproxy",8741,6))

五、阶段测试

打开浏览器,通过输入bbs.linux.com,可看到web01和web02的负载均衡效果。通过输入blog.linux.com,可看到web03和web04的负载均衡效果。
haproxy调度web案例
haproxy调度web案例
haproxy调度web案例
haproxy调度web案例

六、测试访问haproxy的监控页面

haproxy调度web案例

七、安装配置haproxy02

软件安装方法和配置文件与haproxy01一致

[root@haproxy02 ~]# yum install -y haproxy[root@haproxy02 ~]# scp 192.168.122.100:/etc/haproxy/haproxy.cfg /etc/haproxy/[root@haproxy02 ~]# systemctl start haproxy[root@haproxy02 ~]# systemctl enable haproxy

八、配置keepalived实现haproxy高可用

分别在两台haproxy主机上安装keepalived

[root@haproxy01 ~]# yum install -y keepalivedhaproxy01主机keepalived配置文件如下[root@haproxy01 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {   router_id haproxy01}vrrp_instance VI_1 {    state MASTER    interface eth0    virtual_router_id 80    priority 100    advert_int 1    authentication {        auth_type PASS        auth_pass redhat    }    virtual_ipaddress {        192.168.122.111    }}
[root@haproxy01 ~]# scp /etc/keepalived/keepalived.conf 192.168.122.200:/etc/keepalived/haproxy02主机keepalived配置文件如下[root@haproxy01 ~]# scp /etc/keepalived/keepalived.conf 192.168.122.200:/etc/keepalived/
! Configuration File for keepalivedglobal_defs { router_id haproxy02}vrrp_instance VI_1 {    state BACKUP    interface eth0    virtual_router_id 80    priority 50    advert_int 1    authentication {            auth_type PASS            auth_pass redhat    }    virtual_ipaddress {            192.168.122.111    }}

分别启动keepalived服务

[root@haproxy01 ~]# systemctl start keepalived[root@haproxy01 ~]# systemctl enable keepalivedln -s '/usr/lib/systemd/system/keepalived.service' '/etc/systemd/system/multi-user.target.wants/keepalived.service'[root@haproxy01 ~]# systemctl status keepalivedkeepalived.service - LVS and VRRP High Availability Monitor   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled)   Active: active (running) since 二 2018-06-05 19:56:24 CST; 33s ago Main PID: 8821 (keepalived)   CGroup: /system.slice/keepalived.service           ├─8821 /usr/sbin/keepalived -D           ├─8822 /usr/sbin/keepalived -D           └─8823 /usr/sbin/keepalived -D
[root@haproxy01 ~]# ip addr show eth02: eth0: 
mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 52:54:00:4f:62:e6 brd ff:ff:ff:ff:ff:ff inet 192.168.122.100/24 brd 192.168.122.255 scope global eth0 valid_lft forever preferred_lft forever inet 192.168.122.111/32 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::5054:ff:fe4f:62e6/64 scope link valid_lft forever preferred_lft forever
[root@haproxy02 ~]# systemctl start keepalived[root@haproxy02 ~]# systemctl enable keepalivedln -s '/usr/lib/systemd/system/keepalived.service' '/etc/systemd/system/multi-user.target.wants/keepalived.service'[root@haproxy02 ~]# systemctl status keepalivedkeepalived.service - LVS and VRRP High Availability Monitor   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled)   Active: active (running) since 二 2018-06-05 19:57:24 CST; 12s ago Main PID: 8738 (keepalived)   CGroup: /system.slice/keepalived.service           ├─8738 /usr/sbin/keepalived -D           ├─8739 /usr/sbin/keepalived -D           └─8740 /usr/sbin/keepalived -D

配置haproxy的记录日志

[root@haproxy02 ~]# vim /etc/sysconfig/rsyslog
# Options for rsyslogd# Syslogd options are deprecated since rsyslog v3.# If you want to use them, switch to compatibility mode 2 by "-c 2"# See rsyslogd(8) for more detailsSYSLOGD_OPTIONS="-c 2 -r"
[root@haproxy02 ~]# vim /etc/rsyslog.conf $ModLoad imudp$UDPServerRun 514local0.*                                                /var/log/haproxy.log
[root@haproxy02 ~]# systemctl restart rsyslog.service haproxy.service[root@haproxy02 ~]# ls /var/log/anaconda  boot.log  cron   dmesg.old    lastlog  messages  rhsm    spooler   tuned  yum.logaudit     btmp      dmesg  haproxy.log  maillog  ppp       secure  tallylog  wtmp
[root@haproxy02 ~]# tail -f /var/log/haproxy.log Jun  5 20:13:58 localhost haproxy[8805]: Proxy test-proxy started.

转载于:https://blog.51cto.com/12244079/2125240

你可能感兴趣的文章
linux snmp配置
查看>>
linux sudo 重定向,实现只有系统管理员才有权限操作的文件中写入信息
查看>>
在zentyal操作系统内安装PacketiX ×××服务器端的步骤
查看>>
如何安全登录业务系统
查看>>
gridify瀑布流风格页面的加载更多功能
查看>>
IT在线 IT在线技术联盟
查看>>
我的友情链接
查看>>
FLO 文件处理方式
查看>>
Cisco Packet Tracert 之 三层交换机DHCP服务
查看>>
5年职业路圆满结束,精彩继续 (Update1)
查看>>
android发布混淆
查看>>
软件项目开发环境构建之五:Confluence、Jira、Bitbucket统一账户管理
查看>>
5G起步,LTE还该不该学?
查看>>
LAMP & LNMP架构php防注入的一些方法
查看>>
oracle 11g install on suse 12 sp3
查看>>
Spring4-内部bean
查看>>
程序员都应该知道的福利
查看>>
Outlook 2010上阅读模式的调整
查看>>
Windows脚本初探之VBScrip流程控制Select Case
查看>>
Java 多线程 线程的五种状态,线程 Sleep, Wait, notify, notifyAll
查看>>