码迷,mamicode.com
首页 > 其他好文 > 详细

RHEL6配置HAProxy负载平衡集群

时间:2018-01-13 17:05:19      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:chroot   5.4   配置文件   gpg   缺省   故障   记录   front   name   

配置HAProxy负载平衡集群


实验拓扑图

技术分享图片



操作流程

       Real Server :     192.168.4.53 pc53         192.168.4.54 pc54 

        配置WEB 服务器


        HAProxy调度器  :         192.168.4.50 pc50

        安装并启用HAProxy

         修改配置文件配置负载平衡

        

        Clinet :192.168.4.253 pc253

        连接测试        



具体步骤

环境准备

    配置yum源

    # service iptables stop            //关闭防火墙

    # chkconfig iptables off            //关闭开机自启

    # setenforce 0                            //设置SELinux 为宽松模


配置WEB服务器  pc53 / pc54

    #yum -y install httpd  php  > /dev/null

    #service httpd start  > /dev/null

    #chkconfig httpd on

    [root@pc53 ~] #echo " <?php echo 'web53' ; ?>  " > /var/www/html/test.php

    [root@pc54 ~] #echo " <?php echo 'web54' ; ?>  " > /var/www/html/test.php

    

配置HAProxy分发器  pc50

    安装并启动HAProxy

    # mount /dev/cdrom /mnt/

    RHEL6 光盘文件的LoadBalancer目录中含有HAProxy的RPM包

    在已有的yum源配置文件 上添加如下

    [LoadBalancer]

    name=LoadBalancer

    baseurl=file:///mnt/LoadBalancer

    gpgcheck=0

    

    # yum -y install haproxy

    # rpm -qa haproxy

    haproxy-1.5.4-2.el6.x86_64

    

    # rpm -qc haproxy

    /etc/haproxy/haproxy.cfg        //haproxy配置文件

    /etc/logrotate.d/haproxy

    /etc/sysconfig/haproxy

    # cp /etc/haproxy/haproxy.cfg /root/   //备份haproxy配置文件

    

    # chkconfig haproxy on//设置开机自启

    # chkconfig --list haproxy

    haproxy        0:关闭1:关闭2:启用3:启用4:启用5:启用6:关闭

    

    修改HAProxy配置文件 进行配置

      HAProxy配置文件 说明

        — 命令行:总是具有最高优先级

        — global 部分:全局设置进程级别参数

        — 代理声明部分

                来自于 default, listen, frontend 和 backend

                — default 为后续的其他部分设置缺省参数,缺省参数可以被后续部分重置

                — frontend 描述接受客户端侦听套接字(socket)集

                — backend 描述转发链接的服务器集

                — listen 把frontend 和 backend 结合到一起的完整声明

    不做业务区分  修改配置文件如下

    # vim /etc/haproxy/haproxy.cfg

    global
    log         127.0.0.1 local2            

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid        //haproxy的pid存放路径
    maxconn     4000                     //最大连级数 默认4000
    user        haproxy
    group       haproxy
    daemon                               // 创建1个进程进程入deamon模式运行

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats


defaults
    mode                    http            #默认的模式 mode{tcp|http|health}
    log                     global            # 采用全局定义的日志
    option                  httplog           # 日志类别http日志格式
    option                  dontlognull        #不记录健康检查的日志信息
    option http-server-close
    option forwardfor       except 127.0.0.0/8  #后端服务器可以从Http Header中获得客户端IP
    option                  redispatch        #serverid 服务器挂掉后强制指定向到其他健康服务器
    retries                 3           #3次连接失败就认为u服务不可用,也可以通过后面设置
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s        #如果backend 没有指示,默认为10s
    timeout client          1m         #客户端连接超时
    timeout server          1m        #服务器连接超时
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000        #最大连接数
    stats uri /admin                //定义监控页面 uri

listen weblb 0.0.0.0:80
    cookie SERVERID rewrite
    balance roundrobin
    server weba 192.168.4.53:80 cookie app1inst1 check inter 2000 rise 2 fall 5
    server webb 192.168.4.54:80 cookie app1inst2 check inter 2000 rise 2 fall 5

    

    # service haproxy start     //启动服务

    # netstat -pantu | grep :80

    tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      3192/haproxy  

    

客户端访问

    # elinks --dump 192.168.4.50/test.php

       web53

    # elinks --dump 192.168.4.50/test.php

       web54

    # elinks --dump 192.168.4.50/test.php

       web53

    # elinks --dump 192.168.4.50/test.php

       web54

   # elinks --dump 192.168.4.50/test.php

       web53

    # elinks --dump 192.168.4.50/test.php

       web54

    # firefox 192.168.4.50/admin          //查看监控页面

    技术分享图片





部署基于业务区分HAProxy负载平衡集群



实验拓扑图




技术分享图片



操作流程

       Real Server :      

        配置WEB 服务器  使用HTML网页文件       192.168.4.51 pc51         192.168.4.52 pc52  

                                        使用php 网页文件           192.168.4.53  pc53        192.168.4.54  pc54

        HAProxy调度器  :         192.168.4.50 pc50

        安装并启用HAProxy

         修改配置文件配置负载平衡

        

        Clinet :192.168.4.253 pc253

        连接测试        


具体步骤

环境准备

    配置yum源

    # service iptables stop            //关闭防火墙

    # chkconfig iptables off            //关闭开机自启

    # setenforce 0                            //设置SELinux 为宽松模



配置web服务端 pc 51    pc 52    pc 53    pc54    

部署基本的httpd 服务 

    # yum -y install httpd

在pc 53 和 pc 54 上下载 php 软件包

    #yum -y install  php  


    #service httpd start 

    #chkconfig httpd on

    # cd /var/www/html/

    [root@pc51 html]# echo 192.168.4.51 > index.html 

    [root@pc52 html]# echo 192.168.4.52 > index.html

    [root@pc53 html]# echo '<?php echo "192.168.4.53";?>'  > test.php

    [root@pc54 html]# echo '<?php echo "192.168.4.54";?>'  > test.php


配置HAProxy分发器  pc50

    安装并启动HAProxy

    # mount /dev/cdrom /mnt/

    RHEL6 光盘文件的LoadBalancer目录中含有HAProxy的RPM包

    在已有的yum源配置文件 上添加如下

    [LoadBalancer]

    name=LoadBalancer

    baseurl=file:///mnt/LoadBalancer

    gpgcheck=0

    

    # yum -y install haproxy

    # rpm -qa haproxy

    haproxy-1.5.4-2.el6.x86_64

    

    # rpm -qc haproxy

    /etc/haproxy/haproxy.cfg        //haproxy配置文件

    /etc/logrotate.d/haproxy

    /etc/sysconfig/haproxy

    # cp /etc/haproxy/haproxy.cfg /root/   //备份haproxy配置文件

    

    # chkconfig haproxy on//设置开机自启

    # chkconfig --list haproxy

    haproxy        0:关闭1:关闭2:启用3:启用4:启用5:启用6:关闭


基于业务区分 修改配置文件

    # cd /etc/haproxy/

    修改配置文件

    # vim haproxy.cfg

global
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats


defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 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
    stats uri /admin



frontend  weblb *:80
    acl urlhtml       path_end       -i .html            // acl 名字         匹配路径结尾   不区分大小写 .html
    acl urlphp       path_end       -i .php
    use_backend htmlgrp          if urlhtml             //如果接受到与urlhtml这个ACL 匹配时 去找htmlgrp 组
#   use_backend phpgrp          if urlphp
    default_backend            htmlgrp                 // 默认去找htmlgrp

backend htmlgrp
    balance     roundrobin
    server  web51 192.168.4.51:80 check               //check 后不写参数 默认用 defaults 定义的参数
    server  web52 192.168.4.52:80 check

backend phpgrp
    balance     roundrobin
    server  web53 192.168.4.53:80 check
    server  web52 192.168.4.54:80 check

        


客户端访问

 # firefox 192.168.4.50/admin          //查看监控页面

技术分享图片

//测试html

# elinks --dump 192.168.4.50

   192.168.4.51

# elinks --dump 192.168.4.50

   192.168.4.52

# elinks --dump 192.168.4.50

   192.168.4.51

# elinks --dump 192.168.4.50

   192.168.4.52

 # firefox 192.168.4.50/admin          //查看监控页面

技术分享图片


健康性检查

模拟 51 服务器 故障 

[root@pc51 ~]# service httpd stop

# elinks --dump 192.168.4.50

   192.168.4.52

# elinks --dump 192.168.4.50

   192.168.4.52

# elinks --dump 192.168.4.50

   192.168.4.52

 # firefox 192.168.4.50/admin          //查看监控页面

技术分享图片


模拟 51 服务器 故障已经解决

[root@pc51 ~]# service httpd start

# elinks --dump 192.168.4.50

   192.168.4.52

# elinks --dump 192.168.4.50

   192.168.4.51

# elinks --dump 192.168.4.50

   192.168.4.52

# elinks --dump 192.168.4.50

   192.168.4.51

 # firefox 192.168.4.50/admin          //查看监控页面

技术分享图片


//测试php

# elinks --dump 192.168.4.50/test.php

   192.168.4.53

# elinks --dump 192.168.4.50/test.php

   192.168.4.54

# elinks --dump 192.168.4.50/test.php

   192.168.4.53

# elinks --dump 192.168.4.50/test.php

   192.168.4.54

 # firefox 192.168.4.50/admin          //查看监控页面

技术分享图片







RHEL6配置HAProxy负载平衡集群

标签:chroot   5.4   配置文件   gpg   缺省   故障   记录   front   name   

原文地址:http://blog.51cto.com/13558754/2060586

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!