Linux入门教程:centos 7 安装HAproxy四层TCP负载均衡配置及测试,

简介: haproxy负载均衡166.110.110.100后端服务器1 166.110.110.1后端服务器2 166.110.110.2

image.png
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.
#
#   http://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
http://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请求

注明:有需求的朋友可以提前领取阿里云免费代金卷,以防后事只需
阿里云优惠劵直达地址

相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
2月前
|
网络协议 安全 测试技术
手撕测试tcp服务器效率工具——以epoll和io_uring对比为例
手撕测试tcp服务器效率工具——以epoll和io_uring对比为例
41 2
|
3月前
|
C++
jrtplib开源库系列之一:jrtplib介绍、安装和测试(window 10环境介绍)
关于jrtplib库网上已经有很多介绍,而且目前jrtplib作者已经停止更新(Apr 18, 2020),最新版本为v3.11.2。本系列内容也以该版本进行介绍。 相信你已经对RTP/RTCP协议有一定的了解,并想更深入的了解RTP协议的具体实现,jrtplib就是使用使用C++实现的RTP/RTCP协议。具体标准为RFC3550,如果想仔细阅读原文,但是对英文又有点吃力,可以参考我的博客RTP/RTCP中英文对照,在博客的后面有百度链接,是对RFC3550的中文翻译,可能很多地方不太准确,有些内容是自己添加进去的,希望不会影响你的阅读。
34 0
|
3月前
|
网络协议 Linux
拿来即用的Linux上TCP服务端程序
拿来即用的Linux上TCP服务端程序
|
3天前
|
分布式计算 Hadoop Scala
Spark【环境搭建 01】spark-3.0.0-without 单机版(安装+配置+测试案例)
【4月更文挑战第13天】Spark【环境搭建 01】spark-3.0.0-without 单机版(安装+配置+测试案例)
6 0
|
9天前
|
网络协议 Ubuntu Unix
Linux 下使用 socket 实现 TCP 客户端
Linux 下使用 socket 实现 TCP 客户端
|
30天前
|
负载均衡 网络协议 安全
slb监听协议tcp
SLB的TCP监听协议适用于WebSocket支持和通用TCP服务,如FTP、SMTP。配置时设定监听端口,健康检查通过TCP握手检测服务器可用性。会话保持基于源IP,可配置SSL/TLS卸载或端到端加密。注意特定HTTP头传递和平台限制。适用于需要传输层流量管理和不依赖HTTP的应用。
18 3
|
1月前
|
消息中间件 Kafka Linux
Kafka【付诸实践 03】Offset Explorer Kafka 的终极 UI 工具安装+简单上手+关键特性测试(一篇学会使用 Offset Explorer)
【2月更文挑战第21天】Kafka【付诸实践 03】Offset Explorer Kafka 的终极 UI 工具安装+简单上手+关键特性测试(一篇学会使用 Offset Explorer)
177 2
|
2月前
|
网络协议 关系型数据库 MySQL
Linux下的网络编程——C/S模型TCP(二)
Linux下的网络编程——C/S模型TCP(二)
33 0
Linux下的网络编程——C/S模型TCP(二)
|
3月前
|
测试技术 API 芯片
阿萨聊测试:如何在Mac 电脑上安装Postman?
阿萨聊测试:如何在Mac 电脑上安装Postman?
117 0
阿萨聊测试:如何在Mac 电脑上安装Postman?
|
3月前
|
网络协议 网络安全 Go
Kali下安装渗透测试常用工具dnsx和dirsearch
Kali下安装渗透测试常用工具dnsx和dirsearch
61 0