shanghaipc 发表于 2019-1-2 07:37:11

RHEL6配置HAProxy负载均衡集群

配置HAProxy负载平衡集群
  

  实验拓扑图:
  http://s1.运维网.com/images/20180112/1515764003777973.jpg
  

  

  操作流程:
     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 httpdphp
  #service httpd start
  #chkconfig httpd on
   #echo "   " > /var/www/html/test.php
   #echo "   " > /var/www/html/test.php
  

  配置HAProxy分发器pc50
  安装并启动HAProxy
  # mount /dev/cdrom /mnt/
  RHEL6 光盘文件的LoadBalancer目录中含有HAProxy的RPM包
  在已有的yum源配置文件 上添加如下
  
  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          //查看监控页面

http://s1.运维网.com/images/20180113/1515819849943298.jpg
  

  

  

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

  

  实验拓扑图:
http://s1.运维网.com/images/20180113/1515820789476438.jpg
操作流程:
   Real Server :   

      配置WEB 服务器使用HTML网页文件       192.168.4.51 pc51         192.168.4.52 pc52
                                        使用php 网页文件         192.168.4.53pc53      192.168.4.54pc54
      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 installphp


    #service httpd start
    #chkconfig httpd on
    # cd /var/www/html/
    # echo 192.168.4.51 > index.html
    # echo 192.168.4.52 > index.html
    # echo ''> test.php
    # echo ''> test.php


配置HAProxy分发器pc50
安装并启动HAProxy
    # mount /dev/cdrom /mnt/
    RHEL6 光盘文件的LoadBalancer目录中含有HAProxy的RPM包
    在已有的yum源配置文件 上添加如下
   
    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

frontendweblb *: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
    serverweb51 192.168.4.51:80 check               //check 后不写参数 默认用 defaults 定义的参数
    serverweb52 192.168.4.52:80 check
backend phpgrp
    balance   roundrobin
    serverweb53 192.168.4.53:80 check
    serverweb52 192.168.4.54:80 check  客户端访问
  # firefox 192.168.4.50/admin          //查看监控页面
http://s1.运维网.com/images/20180113/1515821789918407.jpg
  //测试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          //查看监控页面
http://s1.运维网.com/images/20180113/1515821813673983.jpg
  健康性检查
  模拟 51 服务器 故障
  # 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          //查看监控页面
http://s1.运维网.com/images/20180113/1515821834922278.jpg
  模拟 51 服务器 故障已经解决
  # 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          //查看监控页面
http://s1.运维网.com/images/20180113/1515821863228006.jpg
  //测试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          //查看监控页面
http://s1.运维网.com/images/20180113/1515821884235934.jpg
  


  

  

  

  




页: [1]
查看完整版本: RHEL6配置HAProxy负载均衡集群