Traefik

 20th August 2020 at 2:19pm

Traefik 是一个跟随容器生态成长起来的 HTTP 负载均衡器以及反向代理。

安装

我对 Traefik 的使用经验仅限于在 k3s 环境中。如果让 k3s 自动安装 Traefik,你无法修改 Traefik 配置文件,因为 k3s 重启时会覆盖它。于是应该让 k3s 不自动安装 Traefik。我编写了一个 YAML 文件,将它放在 /var/lib/rancher/k3s/server/manifests/ 中即可安装 Traefik,同时它配置好了自动获取 Let's Encrypts 证书的相关配置。

调试

先拿到 Traefik 的 pod ID:

kubectl get pods -n kube-system

再查看它的日志:

kubectl logs <traefik-pod-id> -n kube-system

我的 公共 wiki 用容器运行在 k3s 中,并通过 Traefik 将流量导入以及自动申请 Let's Encrypt 证书。具体做法如下。

编写 public-wiki.yaml,将 wiki 程序运行起来并编写 Traefik 路由规则:

apiVersion: v1
kind: Pod
metadata:
  name: public-wiki
  labels:
    app: public-wiki
spec:
  containers:
  - name: public-wiki
    image: onlyice/public-wiki
    ports:
    - containerPort: 8080
    command: ["tiddlywiki"]
    # Implement anonymous read, authenticated write
    # https://tiddlywiki.com/#WebServer
    args: ["--listen", "root-tiddler=$:/core/save/lazy-all", "gzip=yes", "host=0.0.0.0", "port=8080", "readers=(anon)", "writers=joe", "username=joe", "password=wj9127x,x-3"]
---
apiVersion: v1
kind: Service
metadata:
  name: public-wiki
spec:
  ports:
    - protocol: TCP
      name: web
      port: 10000
      targetPort: 8080
  selector:
    app: public-wiki
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: public-wiki
  annotations:
    kubernetes.io/ingress.class: traefik
spec:
  rules:
  - host: wiki.zhiheng.io
    http:
      paths:
      - path: /
        backend:
          serviceName: public-wiki
          servicePort: web