traefik是一个使你把微服务暴露出来变的更容易的http反向代理和负载均衡软件。traefik支持K8S、docker swarm、mesos、consul、etcd、zookeeper等基础设施组件,个人认为更适合容器化的微服务,traefik的配置会自动的、动态的配置更新自己。
假如你在基础架构组件中部署了大量的微服务,你一般通过服务发现或者资源管理框架来管理这些服务,这时候你想访问微服务,你需要一个反向代理。传统的反向代理需要你配置每一个访问到的微服务,在环境中当你增加、删除、升级、横向扩展服务的时候,你都需要调整反向代理配置,而传统的反向代理是不支持动态配置的。为了适应容器化微服务的这种场景,traefik就诞生了,traefik可以监听你的服务发现/基础架构组件的管理API,并且每当你的微服务被添加、移除、杀死或更新都会被感知,并且可以自动生成它们的配置文件。 这样指向到你服务的路由将会被直接创建出来。
官方针对traefik吹了一大堆,在我看来有用的就一个支持K8S、docker swarm等,和容器结合比较紧密。所以一般情况下大家都是以容器的方式运行traefik。traefik的主程序就是一个二进制文件,你可以在非容器环境下使用。
普通青年快速入门
制作traefik image
git clone https://github.com/containous/traefik.git
docker build -t traefik .
启动traefik
配置compose文件
version: '3'
services:
reverse-proxy:
image: traefik # The official Traefik docker image
command: --api --docker # Enables the web UI and tells Træfik to listen to docker
ports:
- "80:80" # The HTTP port
- "8080:8080" # The Web UI (enabled by --api)
volumes:
- /var/run/docker.sock:/var/run/docker.sock # So that Traefik can listen to the Docker events
启动traefik
docker-compose up -d reverse-proxy
或者直接以容器启动
docker run -d -p 8080:8080 -p 80:80 -v $PWD/traefik.toml:/etc/traefik/traefik.toml traefik
非普通青年入门
traefik并非只能在container里面运行,也可以使用二进制文件直接启动
下载二进制文件
https://github.com/containous/traefik/releases
简单的配置文件
https://raw.githubusercontent.com/containous/traefik/master/traefik.sample.toml
ps:默认traefik会寻找/etc/traefik/traefik.toml下的配置文件,当然也可以通过-c参数指定配置文件
启动应用
创建服务compose文件
version: '3'
services:
whoami:
image: emilevauge/whoami
networks:
- web
labels:
- "traefik.backend=whoami"
- "traefik.frontend.rule=Host:whoami.docker.localhost"
networks:
web:
external:
name: traefik_webgateway
ps:其中最关键的是labels的设置
测试
curl -H Host:whoami.docker.localhost http://127.0.0.1
下面开始仔细讲解traefik的点点滴滴
entrypoints
进来的请求在entrypoints处结束,entrypoint是traefik的网络入口,entrypoint监听端口,SSL,做流量redirect。在经过entrypoint后,流量会被转发到一个匹配的frontend上,frontend定义了从entrypoint到backends的路由,路由是通过Host、Path、Headers来决定的,可以匹配或者拒绝一个请求。frontend把请求传送到backend,backend是由一个或者多个servers组成的,servers主要是设置负载均衡策略。最后server会把请求转发到私网内真正的微服务上去
entrypoints是traefik的网络入口,可以通过如下方式定义:
- 一个端口 (80, 443...)
- SSL (证书, 密钥, 由受信任的CA签名的客户端证书的身份验证...)
- 重定向到其他的入口点 (重定向 HTTP 到 HTTPS)
显示一个entrypoint定义的例子:
[entryPoints]
[entryPoints.http]
address = ":80"
[entryPoints.http.redirect]
entryPoint = "https"
[entryPoints.https]
address = ":443"
[entryPoints.https.tls]
[[entryPoints.https.tls.certificates]]
certFile = "tests/traefik.crt"
keyFile = "tests/traefik.key"
- 定义了两个entrypoints,一个是http,一个是https
- http监听在80端口,https监听在443端口
- 当启用ssl的时候,需要提供CA证书
- 把http entrypoint的请求都重定向到https entrypoint上
frontends
frontends由一组规则组成,这些规则确定传入请求如何从entrypoint转发到backend。规则可以分为两种类型:修饰符和匹配器。
Modifiers
Modifier规则只修改请求,它们对正在做出的路由决策没有任何影响,下列是已经存在的modifier规则:
AddPrefix: /products:为请求URL路径添加前缀
ReplacePath: /serverless-path:替换path,并把老的path添加到X-Replaced-Path头
ReplacePathRegex: ^/api/v2/(.*) /api/$1:
Matchers
Matcher规则确定一个特定的请求应该被转发到哪个backend,用逗号分隔的规则值之间是'或'的关系,用分号分隔的规则值是必须全部满足的关系。下面是一些已经存在的matcher 规则:
Headers: Content-Type, application/json: 通过 Headers 可以添加一个匹配规则来匹配请求头部包含的值。它接受要匹配的键/值对序列。
HeadersRegexp: Content-Type, application/(text|json): 也可以在 Headers 中使用正则表达式。它接受要匹配的键/值对序列,序列内容解析是通过正则匹配的
Host: traefik.io, www.traefik.io: 匹配请求 Host 必需在给定域名列表内。
HostRegexp: traefik.io, {subdomain:[a-z]+}.traefik.io: 添加匹配请求 Host 的正则表达式。 它接受一个以{}包括起来的为空或更多url变量的模版。变量的值可以以一个可选的正则表达式来匹配。
Method: GET, POST, PUT: Method 可以添加一个HTTP请求方法的匹配。它接受要匹配的一个或多个请求方法序列。
Path: /products/, /articles/{category}/{id:[0-9]+}: Path 可以添加一个URL路径的匹配。它接受一个以{}包括起来的为空或更多url变量的模版。
PathStrip: /products/ 和 Path 相同,但从请求的URL路径中去掉的给定的前缀。
PathStripRegex: /articles/{category}/{id:[0-9]+} Match exact path and strip off the path prior to forwarding the request to the backend. It accepts a sequence of literal and regular expression paths.
PathPrefix: /products/, /articles/{category}/{id:[0-9]+} PathPrefix 可以添加一个URL路径前缀的匹配。它匹配给定模版中的完整URL路径前缀。
PathPrefixStrip: /products/ 和 PathPrefix 相同,但从请求的URL路径中去掉的给定的前缀。
PathPrefixStripRegex: /articles/{category}/{id:[0-9]+} Match request prefix path and strip off the path prefix prior to forwarding the request to the backend. It accepts a sequence of literal and regular expression prefix paths. Starting with Traefik 1.3, the stripped prefix path will be available in the X-Forwarded-Prefix header.
Query: foo=bar, bar=baz 匹配查询对象,接受k=v的格式
ps:为了在Host和Path matchers规则中使用正则,你必须使用命名捕获,例如:/posts/{id:[0-9]+},你可以选择启用 passHostHeader 来转发客户端请求Header中的 Host 字段到后端
显示一个frontends定义的例子:
[frontends]
[frontends.frontend1]
backend = "backend2"
[frontends.frontend1.routes.test_1]
rule = "Host:test.localhost,test2.local