手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

HAproxy的基本配置(负载均衡+日志独立+动静分离+读写分离)

[日期:2017-03-09] 来源:Linux社区  作者:firelowrie [字体: ]

主机环境  RedHat6.5 64位

实验环境  服务端1 ip 172.25.29.2

          服务端2 ip 172.25.29.3 

          管理端1 ip 172.25.29.1

防火墙状态:关闭

1.Haproxy的负载均衡及测试

A)负载均衡的配置

[root@server1 ~]# yum install haproxy -y            #安装haproxy

[root@server1 ~]# vim /etc/haproxy/haproxy.cfg      #进入主配置文件

  59        stats uri /status                #虚拟目录,后面的全标记,自己添加
 60 ##---------------------------------------------------------------------
 61 ## main frontend which proxys to the backends
 62 ##---------------------------------------------------------------------
 63 #frontend  main *:5000
 64 #    acl url_static      path_beg      -i /static /images /javascript /sty    lesheets
 65 #    acl url_static      path_end      -i .jpg .gif .png .css .js
 66 #
 67 #    use_backend static          if url_static
 68 #    default_backend            app
 69 #
 70 ##---------------------------------------------------------------------
 71 ## static backend for serving up images, stylesheets and such
 72 ##---------------------------------------------------------------------
 73 #backend static
 74 #    balance    roundrobin
 75 #    server      static 127.0.0.1:4331 check
 76 #
 77 ##---------------------------------------------------------------------
 78 ## round robin balancing between the various backends
 79 ##---------------------------------------------------------------------
 80 #backend app
 81 #    balance    roundrobin
 82 #    server  app1 127.0.0.1:5001 check
 83 #    server  app2 127.0.0.1:5002 check
 84 #    server  app3 127.0.0.1:5003 check
 85 #    server  app4 127.0.0.1:5004 check
 86 listen wen *:80                                #添加服务
 87        balance roundrobin#机制:轮叫
 88        server web1 172.25.29.2:80 check        #真正的服务端
 89        server web2 172.25.29.3:80 check        #真正的服务端

[root@server1 ~]# /etc/init.d/haproxy start          #开启haproxy

Starting haproxy:                                          [  OK  ]

B)测试

在测试之前,服务端1和服务端2要安装httpd,开启httpd服务,并且有不同的测试页。

[root@server2 ~]# /etc/init.d/httpd start

Starting httpd:                                            [  OK  ]

[root@server3 ~]# /etc/init.d/httpd start

Starting httpd:                                            [  OK  ]

开始测试:172.25.29.1/status (服务端1和服务端2httpd服务都开启

测试:172.25.29.1

刷新之后

当关闭服务端2的httpd服务时:

[root@server3 ~]# /etc/init.d/httpd stop

Starting httpd:                                            [  OK  ]

刷新之后,结果如图:(只有wed1运行,wed2停止)

当再关闭服务端1的httpd服务时:

[root@server2 ~]# /etc/init.d/httpd stop

Starting httpd:                                            [  OK  ]

刷新之后,结果如图:(wed1和wed2都停止)

2.Haproxy中将管理和负载均衡分开及测试

A) 将管理和负载均衡分开的配置

管理端要安装httpd,要有测试页,在/etc/httpd/conf/httpd.conf中将端口改成8080,不然会冲突。   

[root@server1 ~]# vim /etc/haproxy/haproxy.cfg #修改主配置文件

 86 frontend wen *:80
 87        default_backend app
 88 backend app
 89        balance roundrobin
 90        server web1 172.25.29.2:80 check
 91        server web2 172.25.29.3:80 check
 92        server local 127.0.0.1:8080 backup

[root@server1 ~]# /etc/init.d/haproxy reload#刷新

Reloading haproxy:

B)测试

开始测试(开启管理端、服务端1、服务端2的httpd服务)

访问172.25.29.1时,服务端1和服务端2再刷新前后交替出现。

当停止服务端2的httpd时:

[root@server3 ~]# /etc/init.d/httpd stop

Stopping httpd:                                            [  OK  ]

访问172.25.29.1时,只有服务端1刷新之后也不变。

再停止服务端1的httpd时:

[root@server2 ~]# /etc/init.d/httpd stop

Stopping httpd:                                            [  OK  ]

访问172.25.29.1时,出现管理端的测试页

更多详情见请继续阅读下一页的精彩内容http://www.linuxidc.com/Linux/2017-03/141614p2.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款