Traefik 一个反向代理的新工具

由于工作需要最近试用了几个反向路由的开源项目,Traefik就是其中之一。

一,Traefik 是干什么用的

简单来说它就是用来作反向代理和负载均衡的,比较适用于微服务化的场景,支持多种分布式的Key-Value存储系统,支持容器技术,下面这个图诠释了它的工作

640?wx_fmt=png&wxfrom=5&wx_lazy=1

二,Traefik 的特性

这些特性都是官方自己说的,再加上个人的一点通俗唱法解释。

  • 我很快 --- 经过测试吧,我感觉是速度一般般,但对于新产品来说速度做的还不错。

  • 安装简单 --- 不需要任何依赖,只是一个单独的可执行文件。(安装Traefik真的是简单,这操作给满分。)

  • 镜像很小 --- 有一个很小的官方 Docker Image。(可执行文件43MB,镜像只有45MB,评什么扣分。)

  • RestApi --- 支持Rest-API。(中规中矩。)

  • 配置热更新 --- 无需重启即可应用最新的配置。(其实这里说的配置只是动态的路由配置。)

  • 熔断机制 --- 对于后端服务的保护上支持熔断和重试机制。

  • 负载均衡 --- 负载策略内置两种,Weighted Round-Robin(wrr)和Dynamic Round-Robin(wrr)。

  • 支持Docker --- Docker, Swarm, Kubernetes, Marathon, Mesos。

  • 支持统计 --- Rest, Prometheus, Datadog, Statd。

  • 支持KV --- 支持多种分布式K-V系统,Zookeeper, Consul, Etcd, ECS等。

  • 多种通信协议 --- HTTP/1.1, HTTP/2, Websocket, GRPC等。

  • 支持ACME和HA --- 个人感觉Traefik的HA也就在ACME的场景下才有用。

  • Web-UI --- 有个AngularJS的Web-UI

三,Traefik 的基础组件

就两个组件,就这么简单,支持自己写 middle-ware。

  • Traefik
    Traefik 的主程序,启动时可以指定配置文件,

    ## 启动方式## 默认的文件名是traefik.toml## 寻址文件优先级是1. /etc/traefik, 2. $HOME/.traefik/ -->, 3. working directory.  traefik --configFile=foo/bar/myconfigfile.toml
  • Dashboard
    一个简单的Dashboard, 可以看当前的路由规则,和转发的结果统计。

四, 配置文件如何使用

Traefik 的配置分为静态配置 和动态配置两大类。

  1. 动态配置:用来控制路由和负载均衡策略,动态配置不需要重起Traefik就可以生效。

  2. 静态配置:简单的说吧除了动态配置的其他均为静态配置范畴,静态配置需要重启Traefik才能生效。

配置详细说明我就不写了,到官网上找你需要的配置是最明智的(我是明智的官网)。
但是在后面的的练习中会说明部分配置的意义。

注意点
  1. 动态配置可以和静态配置一起在同一个文件里,动态配置写在文件的最后。

  2. 如果想用配置文件来指定路由规则的话,需要将动态配置和静态配置文件分开,如下

    ## 在  "静态配置的最后面"  加入下面信息来指定动态配置文件[file]watch = truefilename = "rules.toml"

五,实战

业务需求
  1. 公司有多条产品线,但是每条产品线的负载压力不同,压力由高到低排序为, Mobile--> Web --> PC。

  2. 公司控制成本考虑,要求使用实体服务器,虚拟机或者容器技术来混合部署服务。

  3. 要求可以秒级的动态增加服务和减少服务来应对业务的压力。

画个简单的图来表

640?wx_fmt=png

基础环境

回顾一下上面的缩略图,发现我们需要下列基本的元素。
我们需要有一个Docker环境,需要有一套分布式K-V系统,K-V系统选择etcd。

  • [x] Real-Machine,用虚拟机来作RealMachine。

  • [x] Docker

  • [x] Etcd-Cluster

  • [x] Traefik

  • [x] Demo-Service

    Question

  1. 手上没有docker和etcd-cluster怎么办?
    Answer: 伸手过来我教你

  2. 不想用etcd,因为公司项目没用这个K-V.
    Answer: 你可以更换到任何一款主流的K-V系统,因为Traefik支持很多种K-V。

DemoService

直接从docker-hub上 pull 已经写好的Image就好了。

docker pull wangxingge/simple-web
Traefik

直接从docker-hub 上 pull下来。

docker pull traefik

货全了,开始搭建配置环境。


Traefik启动与配置文件

  • 创建docker-compose.yml 文件。docker-compose读取yml文件来启动镜像。

#docker-compose.ymltraefik:
  image: traefik  command: --logLevel=DEBUG  ports:
    - "80"
    - "8080"
    - "443"
    ## 端口由docker 自由分配,但是需要指定容器需要多少端口,如果要指定宿主机端口的话就写成
    ## 80 默认的http端口,443 默认的https端口,8080默认的后台管理页面端口
    ## ports:
    ## - "80:80"
    ## - "8080:8080"
    ## - "443:443"
  volumes:
    - /var/run/docker.sock:/var/run/docker.sock
    - /opt/traefik/traefik.toml:/traefik.toml
    - /opt/traefik/rules.toml:/rules.toml    ## 磁盘映射,主要是为了映射配置文件和让traefik读取docker的网络配置
    ## traefik.toml 是traefik的静态配置文件
    ## rules.toml 是traefik的动态配置文件,也是路由配置
  • 创建traefik.toml配置文件。
    由于需求是秒级增加服务节点,所以路由配置信息我们不使用 rules.toml的形式提供,而使用分布式K-V来提供。
    在K-V 配置中设置exposebydefault = false 目的是为了不让启动容器时自动添加路由。

#traefik.toml################################################################
# Common configuration
################################################################debug=truelogLevel="DEBUG"[web]
address = ":8080"ProvidersThrottleDuration = 100000000################################################################
# Docker configuration backend
################################################################[docker]
domain = "docker.local"watch = trueexposedbydefault = false################################################################
# kv store
################################################################[etcd]
endpoint = "192.168.196.88:12379"watch = trueprefix = "traefik"
  • 启动Traefik

    # 使用docker-compose 目的是方便起停批量的镜像docker-compose up -d
  • 验证Traefik启动成功,查看docker镜像和后面管理页面是否可用

    [root@centos7 traefik]# docker-compose ps
      Name                   Command            State                                   Ports                                 
    ------------------------------------------------------------------------------------------------------------------------------
    traefik_traefik_1   /traefik --logLevel=DEBUG   Up      0.0.0.0:32772->443/tcp, 0.0.0.0:32773->80/tcp, 0.0.0.0:32771->8080/tcp

640?wx_fmt=png

配置路由信息

前面说明了要使用分布式K-V来进行路由信息的配置,我们选择etcd-cluster。
Traefik的路由配置一共分为2大部分,FrontEnd和 BackEnd。

- Frontend

主要控制访问的路由规则,有三个主要控制方式:Header, Host, Path,都支持指定单个规则和按正则匹配。
匹配方式为某些规则的请求转发到某个Backend上。

- Backend

进行某个Backend匹配某一组服务,控制方式有:轮询控制,断路器控制(熔断),压力控制和健康检查。

  • 实战我们模拟的Frontend和Backend要求为。

  • Frontend: 分别根据Host和Url区分路由。

  • Backend: 三组服务分别是 mobile, web, pc, 轮询方式为wrr,加入健康检查,熔断控制使用最大数量限制。

  • 具体配置如下: 只需要定义frontend,backend不需要定义服务启动时会自动注册Backend.

Alias Path Value
1 /traefik_configurations/1/frontends/front_web/priority 3
1 /traefik_configurations/1/frontends/front_web/passHostHeader true
1 /traefik_configurations/1/frontends/front_web/backend backend_web
1 /traefik_configurations/1/frontends/front_web/routes/test_1/rule Host:web.eastmoney.com
1 /traefik_configurations/1/frontends/front_mobile/priority 3
1 /traefik_configurations/1/frontends/front_mobile/passHostHeader true
1 /traefik_configurations/1/frontends/front_mobile/backend backend_mobile
1 /traefik_configurations/1/frontends/front_mobile/routes/test_1/rule Host:mobile.eastmoney.com
1 /traefik_configurations/1/frontends/front_pc/priority 3
1 /traefik_configurations/1/frontends/front_pc/passHostHeader true
1 /traefik_configurations/1/frontends/front_pc/backend backend_pc
1 /traefik_configurations/1/frontends/front_pc/routes/test_1/rule Host:pc.eastmoney.com
2 /traefik_configurations/2/frontends/front_web/priority 3
2 /traefik_configurations/2/frontends/front_web/passHostHeader true
2 /traefik_configurations/2/frontends/front_web/backend backend_web
2 /traefik_configurations/2/frontends/front_web/routes/test_1/rule Path:/web_root/web/{subdomain:[a-z]+}
2 /traefik_configurations/2/frontends/front_mobile/priority 3
2 /traefik_configurations/2/frontends/front_mobile/passHostHeader true
2 /traefik_configurations/2/frontends/front_mobile/backend backend_mobile
2 /traefik_configurations/2/frontends/front_mobile/routes/test_1/rule Path:/web_root/web/{subdomain:[a-z]+}
2 /traefik_configurations/2/frontends/front_pc/priority 3
2 /traefik_configurations/2/frontends/front_pc/passHostHeader true
2 /traefik_configurations/2/frontends/front_pc/backend backend_pc
2 /traefik_configurations/2/frontends/front_pc/routes/test_1/rule Path:/web_root/web/{subdomain:[a-z]+}

启动后台服务

根据目前的需要后台服务的运行环境有两种:测试过程我们使用Docker的环境方便一些。

宿主 启动方式 优缺点
Docker 使用docker-compose 再用labels 指定路由信息 方便动态调整服务数量,不需要修改路由配置。
实体机 直接启动直接读取K-V的路由信息 需要服务启动时到K-V中进行注册之后路由才会生效,一个服务使用一台服务器产生资源浪费。
  • 启动后台服务 业务说明了有三条主要业务线:mobile, pc, web,为了方便那我们分别为每个业务线创建一个docker-compose要用的配置文件。

    web:
    image: wangxingge/simple-web:latestcommand: /opt/simple-web --kvaddr="http://192.168.196.88:12379" --kv=true --backend="backend_web" -watch="/traefik/alias"ports:
     - "80"

mobile: image: wangxingge/simple-web:latest command: /opt/simple-web --kvaddr="http://192.168.196.88:12379" --kv=true --backend="backend_mobile" -watch="/traefik/alias" ports:

  • "80"

pc: image: wangxingge/simple-web:latest command: /opt/simple-web --kvaddr="http://192.168.196.88:12379" --kv=true --backend="backend_pc" -watch="/traefik/alias" ports:

  • "80"

    /opt/simple-web 启动程序

    kvaddr: K-V系统的地址

    kv: 是否使用K-V系统

    backend: 当前程序使用哪些backend, backend_web 表示当前服务为WEB业务提供服务。

    watch: 把traefik.toml中的K-V项的prefix再加上/alias就可以了,目的是为了配合traefik进行动态注册

    根据当前的线上压力情况,我们启动5个Mobile, 3个Web, 2个PC的后台服务。

    docker-compose up -d docker-compose scale web=5 docker-compose scale mobile=3 docker-compose scale pc=2

    启动和配置成功以后观查一下Traefik的Web页面  
    ![image](http://ogmbad78f.bkt.clouddn.c ... _1.png)

测试路由设置是否可用,测试步骤如下

  1. 在使用1号配置的时候按照Host进行路由。

  2. 使用Postman并设置相应的Host值进行请求,并观察返回结果。

  3. 切换至2号配置。

  4. 使用Postman并根据想应的URL进行请求,并观察返回结果。

  5. 动态添加和缩减容器数量,docker-compose scale serviceName=serviceCount

  6. 观察Traefik的后台管理页面,是否已经更新相应的路由。

  7. 使用Postman请求,观查是否后端相应的服务器数量是否已更新。

猜你喜欢

转载自blog.csdn.net/RA681t58CJxsgCkJ31/article/details/79336407