在 Kubernetes 中使用 traefik 快速指南

2025-08-09 11:15:00
丁国栋
原创 147
摘要:本文记录和汇总在 Kubernetes 中使用 traefik。

traefik 是一个云原生应用代理服务,类似于 Nginx Ingress,用于简化和自动化Web应用的服务发现、路由以及负载均衡。它比 Nginx Ingress 的优势在于能像 Caddy 那样自动签发 SSL 证书,这样就可以很容易的实现 HTTPS 访问以及证书轮换。


traefik 在 Kubernetes 中主要依赖 Custom Resource Definitions (CRD) 。因此我们需要查看目前有哪些 traefik 的 CRD。

可能会有以下 CRD:

kubectl get crd|grep -o '.*traefik.io'
ingressroutes.traefik.io
ingressroutetcps.traefik.io
ingressrouteudps.traefik.io
middlewares.traefik.io
middlewaretcps.traefik.io
serverstransports.traefik.io
serverstransporttcps.traefik.io
tlsoptions.traefik.io
tlsstores.traefik.io
traefikservices.traefik.io

要在 traefik 中创建一个服务,需要创建一个 ingressroutes.traefik.io 。


apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
  name: example-service-name-http
spec:
  entryPoints:
  - web
  routes:
  - kind: Rule
    match: Host(`www.example.com`)
    services:
    - name: example-service-name
      port: 80
  - kind: Rule
    match: Host(`example.com`)
    services:
    - name: example-service-name
      port: 80
---
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
  name: example-service-name-https
spec:
  entryPoints:
  - websecure
  routes:
  - kind: Rule
    match: Host(`www.example.com`)
    services:
    - name: example-service-name
      port: 80
  - kind: Rule
    match: Host(`example.com`)
    services:
    - name: example-service-name
      port: 80
  tls:
    certResolver: myresolver
上面的 yaml 文件中有两个不同的 entryPoints,分别是 HTTP 和 HTTPS 服务。HTTPS 要实现正常访问,需要将 DNS 解析到 traefik 服务自身的 Service 的 EXTERNAL-IP 上,并且解析需要生效,如果之前已经解析过,则需要等待 DNS 缓存过期。


使用 kubectl get  middlewares.traefik.io 可以查看目前已经存在的 traefik 中间件。中间件这个词有很多种解释,我认为在这里比较通俗的解释就是中间件是一个过滤器,它把输入接收进来进行处理后再输出出去。过滤器可以有多个,因此中间件有多个。

最为常用的几个中间件功能:

通过Traefik API查看可用的HTTP服务

要通过Traefik的API查看可用的HTTP服务,可以使用以下方法:

1. 首先检查API是否已启用

在Traefik配置中确保API已启用。

静态配置:

例如:/etc/traefik/traefik.yml

api:
  dashboard: true
  insecure: true  # 仅限开发环境,生产环境应使用安全配置

命令行参数:

如果 Traefik 运行在 Kubernetes中,可以通过命令行参数确定是否启用了API端点:

 spec:
      containers:
      - args:
        - --global.checknewversion
        - --global.sendanonymoususage
        - --entrypoints.metrics.address=:9100/tcp
        - --entrypoints.traefik.address=:9000/tcp
        - --entrypoints.web.address=:8000/tcp
        - --entrypoints.websecure.address=:8443/tcp
        - --api.dashboard=true
        - --ping=true
        - --metrics.prometheus=true
        - --metrics.prometheus.entrypoint=metrics
        - --providers.kubernetescrd
        - --providers.kubernetesingress
        - --providers.kubernetesingress.ingressendpoint.publishedservice=kube-system/traefik
        - --entrypoints.websecure.http.tls=true

2. 使用API端点查看HTTP服务

Traefik提供了多个API端点来查看路由和服务信息:

查看所有HTTP路由

GET http://<traefik-host>:<api-port>/api/http/routers

查看所有HTTP服务

GET http://<traefik-host>:<api-port>/api/http/services

查看所有中间件

GET http://<traefik-host>:<api-port>/api/http/middlewares

获取完整的配置概览(包含路由、服务、中间件等)

GET http://<traefik-host>:<api-port>/api/rawdata

3. 使用curl命令示例

# 查看所有HTTP路由
curl http://localhost:9000/api/http/routers
# 查看特定路由详情
curl http://localhost:9000/api/http/routers/<router-name>@<provider>
# 查看所有HTTP服务
curl http://localhost:9000/api/http/services

--

发表评论
博客分类