Linux运维知识之centos 7 安装HAproxy四层TCP负载均衡配置及测试
小标 2019-03-15 来源 : 阅读 1438 评论 0

摘要:本文主要向大家介绍了Linux运维知识之centos 7 安装HAproxy四层TCP负载均衡配置及测试,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助。

本文主要向大家介绍了Linux运维知识之centos 7 安装HAproxy四层TCP负载均衡配置及测试,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助。

Linux运维知识之centos 7 安装HAproxy四层TCP负载均衡配置及测试

haproxy负载均衡166.110.110.100

后端服务器1 166.110.110.1

后端服务器2 166.110.110.2


--------------------------------------------------centos 7 处理----------------------------------------------------
关闭SELinux
vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq!  #保存退出
setenforce 0 #使配置立即生效

关闭centos 7的防火墙(仅在测试的时候)
systemctl stop firewalld.service
systemctl disable firewalld.service


--------------------------------------------------haproxy安装----------------------------------------------------
yum install wget gcc -y && wget -c --no-check-certificate https://src.fedoraproject.org/repo/pkgs/haproxy/haproxy-1.8.12.tar.gz && tar -xvf haproxy-1.8.12.tar.gz && cd haproxy-1.8.12

groupadd haproxy #添加haproxy组
useradd -g haproxy haproxy -s /bin/false #创建nginx运行账户haproxy并加入到haproxy组,不允许haproxy
--------------------------------------------------haproxy配置----------------------------------------------------
vi /etc/haproxy/haproxy.cfg

修改为以下配置
#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   //haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    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

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
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

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
listen admin_stats 
        bind 127.0.0.1:1080               #监听端口 
        mode http                       #http的7层模式 
        option httplog                  #采用http日志格式 
        #log 127.0.0.1 local0 err 
        maxconn 10 
        stats refresh 30s               #统计页面自动刷新时间 
        stats uri /stats                #统计页面url 
        stats realm Proxy\ Haproxy  #统计页面密码框上提示文本 
        stats auth admin:admin          #统计页面用户名和密码设置 
        stats hide-version              #隐藏统计页面上HAProxy的版本信息 

listen test1 
        bind 0.0.0.0:80 
        mode tcp
        balance leastconn
        #maxconn 40000 
        #log 127.0.0.1 local0 debug 
        server s1 166.110.110.1:80 
        server s2 166.110.110.2:80
------------------------------------------------------------------------------------------------------
centos7下haproxy启动停止重启重载状态等控制命令
systemctl (start, stop, restart, try-restart, reload, force-reload, status) haproxy.service

haproxy web监控信息166.110.110.100为haproxy安装所在服务器IP
//166.110.110.100:1080/status
-----------------------------后端安装lighttpd 作为测试用----------------------------------------------
systemctl stop firewalld.service
systemctl disable firewalld.service
关闭SELinux
vi /etc/selinux/config
将SELINUX=enforcing改为SELINUX=disabled

安装
yum install -y epel-release gcc gcc-c++ autoconf automake  zlib zlib-devel pcre-devel zip unzip libtool bzip2-* && yum install -y lighttpd && systemctl enable lighttpd

编辑配置文件关闭ipv6
vi /etc/lighttpd/lighttpd.conf
注释掉ipv6

启动服务器
systemctl restart lighttpd.service

默认目录及首页
/var/www/lighttpd/index.html

对两个index.html进行修改进行区别
后端第一个服务器
echo "<h1>this is upstream server 1" >> /var/www/lighttpd/index.html

后端第二个服务器
echo "<h1>this is upstream server 2" >> /var/www/lighttpd/index.html


访问haproxy所在服务器IP或者解析的域名刷新试试


-----------------------------Haproxy 负载均衡的8种算法----------------------------------------
balance roundrobin # 轮询,软负载均衡基本都具备这种算法

balance static-rr # 根据权重,建议使用

balance leastconn # 最少连接者先处理,建议使用

balance source # 根据请求源IP,建议使用

balance uri # 根据请求的URI

balance url_param,# 根据请求的URl参数'balance url_param' requires an URL parameter name

balance hdr(name) # 根据HTTP请求头来锁定每一次HTTP请求

balance rdp-cookie(name) # 根据据cookie(name)来锁定并哈希每一次TCP请求


本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注系统运维Linux频道!


本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程