HAProxy的简单使用

学习HAproxy的记录。逻辑比较混乱。

HAProxy 概念

Haproxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。Haproxy特别适用于那些负载特大的web站点,这些站点通常又需要会保持或七层处理。Haproxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中,同时可以保护你的web服务器不被暴露到网络上。
HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。

emmmm 这个看一眼就好了。感觉很多都是都是要先学会使用,再看概念,理论才会更加清楚。

安装

由于CentOS7 自带安装包。也就不再使用源码安装。如果一定要源码安装,网上教程挺多的。
安装前可产看一下 haproxy信息

yum info haproxy
Name        : haproxy
Arch        : x86_64
Version     : 1.5.18
Release     : 9.el7
Size        : 834 k
Repo        : base/7/x86_64
Summary     : TCP/HTTP proxy and load balancer for high availability environments
URL         : http://www.haproxy.org/
License     : GPLv2+
Description : HAProxy is a TCP/HTTP reverse proxy which is particularly suited for high
            : availability environments. Indeed, it can:
            :  - route HTTP requests depending on statically assigned cookies
            :  - spread load among several servers while assuring server persistence
            :    through the use of HTTP cookies
            :  - switch to backup servers in the event a main server fails
            :  - accept connections to special ports dedicated to service monitoring
            :  - stop accepting connections without breaking existing ones
            :  - add, modify, and delete HTTP headers in both directions
            :  - block requests matching particular patterns
            :  - report detailed status to authenticated users from a URI
            :    intercepted by the application

安装

yum -y install haproxy

建立简单地web服务。用于haproxy代理。

使用nginx搭建简单地 web服务。绑定端口

yum -y install nginx

关于nginx也可以作为负载均衡之类的设置。。。无视掉,现在只用nginx作为一个小的web服务器。
编辑nginx 配置文件。。。设置几个端口对应不同web页面当作一个个服务器吧。

编辑nginx 配置文件

vi /etc/nginx/nginx.conf

在http 模块中 默认80 端口后添加

    server {
        listen 8001;
        server_name www.test_server1.com;
        root /webtest/server1;

        location / {
            index index.html;
        }
    }

    server {
        listen 8002;
        server_name test_server2;
        root /webtest/server2;

        location / {
            index index.html;
        }
    }   

监听8001 和 8002 端口。
然后创建目录和所需文件

mkdir -p /webtest/server1
mkdir -p /webtest/server2

在两个目录下分别创建一个index.html 文件

vi /webtest/server1/index.html 
test_server1--8001
vi /webtest/server2/index.html 
test_server2--8002

然后启动nginx 分别访问 192.168.199.81:8001 192.168.199.81:8002
192.168.199.81-8001
192.168.199.81-8002
一个超级简单的web服务就搭建好了。
192.168.199.81:8001对应test_server1–8001
192.168.199.81:8002对应test_server2–8002

最简单的使用,代理本机其它端口。

最基本的使用,使用一个端口代理其它端口。

代理相关的配置可以如下配置段中。

  • “defaults” 为frontendbackend, listen提供默认配置,这配置默认配置参数可由下一个“defaults”所重新设定。

  • “frontend” 用于定义一系列监听的套接字,这些套接字可接受客户端请求并与之建立连接。前端,指定接收客户端连接侦听套接字设置

  • “backend” 用于定义一系列“后端”服务器,代理将会将对应客户端的请求转发至这些服务器。 后端,指定将连接请求转发至后端服务器的相关设置

  • “listen”段通过关联“frontend”和“backend”定义了一个完整的代理,通常只对TCP流量有用。同时设置前端和后端,适用于一对一环境

所有代理的名称只能使用大写字母、小写字母、数字、-(中线)、_(下划线)、.(点号)和:(冒号)。此外,ACL名称会区分字母大小写)

查看 haproxy配置文件。

cat /etc/haproxy/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

    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

frontend  main *:5000
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js

    use_backend static          if url_static
    default_backend             app

backend static
    balance     roundrobin
 
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值