运维项目实战——HAproxy负载均衡

1、haproxy服务

在调度器里下载并开启haproxy.server

dnf install haproxy -y
systemctl enable --now haproxy

修改haproxy的配置文件

vim /etc/haproxy/haproxy.cfg
  
     62 #---------------------------------------------------------------------
     63 # main frontend which proxys to the backends
     64 #---------------------------------------------------------------------
     65 frontend  main *:80     #修改端口为80
     66         #acl blacklist src 172.25.6.250
     67         #block if blacklist
     68         #errorloc 403 http://172.25.6.5:8000
     69         #acl testfie path /test.html
     70         #http-request deny if testfile blacklist
     71         #redirect location http://www.baidu.com if backlist
     72     #acl url_static       path_beg       -i /images
     73     #acl url_static       path_end       -i .jpg .gif .png .css .js
     74         #acl read_request method GET
     75         #acl read_request method HEAD
     76         #acl write_request method PUT
     77         #acl write_request method POST
     78    #use_backend static         if read_request
     79    #use_backend app            if write_request
     80    default_backend             app 
     81 
     82 #---------------------------------------------------------------------
     83 # static backend for serving up images, stylesheets and such
     84 #---------------------------------------------------------------------
     85 #backend static
     86  #  balance     roundrobin
     87   #  server      static 172.25.6.6:80 check
     88 
     89 #---------------------------------------------------------------------
     90 # round robin balancing between the various backends
     91 #---------------------------------------------------------------------
     92 backend app
     93     balance     roundrobin
     94     #balance    source
     95     #balance    static-rr
     96     server  app1 172.25.7.4:80 check    
     97     server  app2 172.25.6.5:80 check    
     98     #server backup 127.0.0.1:8000 backup做监控以及用户认证

2、Apache服务

在服务器1里下载Apache并开启Apache

dnf install httpd -y
systemctl enable --now httpd

在服务器2里下载Apache并开启Apache

dnf install httpd -y
systemctl enable --now httpd

在两个服务器的Apache默认发布目录下写index.html文件

echo this is vm3 > /var/www/html/index.html

echo this is vm4 > /var/www/html/index.html

3、真机下访问server2

[root@westos ~]# curl 172.25.7.3
this is vm3
[root@westos ~]# curl 172.25.7.3
this is vm4
[root@westos ~]# curl 172.25.7.3
this is vm3
[root@westos ~]# curl 172.25.7.3
this is vm4
[root@westos ~]# curl 172.25.7.3
this is vm3
[root@westos ~]# curl 172.25.7.3
this is vm4

4、判断负载均衡是否实现

关闭server3的http服务后重新访问server2
显示只访问到server4

[root@westos ~]# curl 172.25.7.3
this is vm4
[root@westos ~]# curl 172.25.7.3
this is vm4
[root@westos ~]# curl 172.25.7.3
this is vm4
[root@westos ~]# curl 172.25.7.3
this is vm4
[root@westos ~]# curl 172.25.7.3
this is vm4
[root@westos ~]# curl 172.25.7.3
this is vm4

5、添加监控

vim /etc/haproxy/haproxy.cfg
 
 59     stats uri /status    #添加监控
 60     stats auth han:westos #设置用户名为han密码为westos
 61     stats refresh 5s       #设置为5秒刷新一次监控
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值