Traefik全面使用指南:部署篇

本文最后更新于 65 天前, 如有失效请评论区留言.

本文聊聊如何通过k3s如何部署Traefik

什么是Traefik

Traefik 是一个用于反向代理和负载均衡的现代化 HTTP 反向代理服务器和负载均衡器。

主要特性

仅列出我使用的特性

  • 自动发现和动态配置 可以与容器管理平台如Docker、Kubernetes集成,并根据服务的变化自动更新路由规则
  • 负载均衡
  • 动态路由
  • 内置的 Let's Encrypt 集成
  • 丰富的插件和中间件机制, 如IP白名单等

部署

其实,官网文档已经很全了,这里仅列出一些个性化的设置。

helm repo add traefik https://traefik.github.io/charts
helm repo update

添加官网helm仓库,并更新

自定义配置

image:
  registry: ccr.ccs.tencentyun.com
  repository: k7scn/traefik

ingressClass:
  isDefaultClass: false

ingressRoute:
  healthcheck:
    enabled: true

providers:
  kubernetesCRD:
    enabled: true
    allowCrossNamespace: true
    allowExternalNameServices: true
    allowEmptyServices: true

  kubernetesIngress:
    enabled: true
    allowExternalNameServices: true
    allowEmptyServices: true
    publishedService:
      enabled: true

additionalArguments:
  - "--certificatesresolvers.myresolver.acme.email=traefik-tls@ysicing.net"
  - "--certificatesresolvers.myresolver.acme.storage=/data/acme.json"
  - "--certificatesresolvers.myresolver.acme.httpchallenge=true"
  - "--certificatesresolvers.myresolver.acme.httpchallenge.entrypoint=web"
  - "--api=true"

ports:
  traefik:
    port: 9000
    expose: true
  web:
    port: 8000
    hostPort: 80
  websecure:
    port: 8443
    hostPort: 443
    http3:
      enabled: true
  metrics:
    expose: true

nodeSelector:
  node-role.kubernetes.io/bj: "true"
tolerations: []

persistence:
  enabled: true

log:
  access:
    enabled: true

其实也添加tls相关及限定了调度到哪台机器上。

部署

也可以不指定版本。如果不指定版本需要去掉values.yaml关于registry部分的配置

kubectl create ns traefik-v2
helm upgrade -i traefik traefik/traefik -n traefik-v2 -f ./values.yaml --version 26.0.0

其他问题

可能会提示对/data/acme.json 没有权限,手动处理一下这个权限。

由于我是多云集群,默认全局sc是hostpath,直接去相关节点手动新建这个文件即可, 路径大概是/var/lib/rancher/k3s/storage/[traefik-pvc], 具体pvc需要自行看下,如果没跑服务应该就1个就是traefik的

touch ./acme.json
chmod -v 600 ./acme.json
chown 65532.65532 acme.json

这个也可以通过initContainer去处理, 具体可以参考 traefik/values.yaml#L65

测试

这里我提供一个测试yaml

apiVersion: v1
kind: Service
metadata:
  name: whoami
spec:
  ports:
    - protocol: TCP
      name: web
      port: 80
  selector:
    app: whoami
---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: whoami
  labels:
    app: whoami
spec:
  replicas: 2
  selector:
    matchLabels:
      app: whoami
  template:
    metadata:
      labels:
        app: whoami
    spec:
      nodeSelector:
        node-role.kubernetes.io/bj: "true"
      containers:
        - name: whoami
          image: h2.ysicing.net/containous/whoami
          resources:
            limits:
              cpu: 100m
              memory: 128M
          ports:
            - name: web
              containerPort: 80
---
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: whoami-http
spec:
  entryPoints:
    - web
  routes:
    - match: Host(`whoami.bjslb.ysicing.net`) && PathPrefix(`/notls`)
      kind: Rule
      services:
        - name: whoami
          port: 80
---
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: whoami-https
spec:
  entryPoints:
    - websecure
  routes:
    - match: Host(`whoami.bjslb.ysicing.net`) && PathPrefix(`/tls`)
      kind: Rule
      services:
        - name: whoami
          port: 80
  tls:
   certResolver: myresolver

Chapters

Sponsor

Like this article? $1 reward

Comments

Teacher Du ·v1 安徽合肥 Reply

这个看着不错~

优惠券网站 ·v1 IP未知

Akismet Anti-Spam System 认定当前评论不宜展示,等待人工审核

According to the relevant laws and regulations of the People's Republic of China, the comment function of the current website has been disabled. If you need to comment, please visit ysicing.me, but the comments still need to be reviewed by AI.