الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

traefik

A cloud-native reverse proxy that finds services by itself (Docker labels, Kubernetes Ingress) and gets TLS certificates automatically.

المستوى
خدمة DevSecOps
المجموعة
الويب
يحتاج root
للتغييرات
الحزمة
traefik

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

Traefik watches a provider — Docker, Kubernetes, a file — and builds its routes from labels or Ingress/IngressRoute objects, without restarts. It gets and renews Let's Encrypt certificates by itself and has a dashboard, metrics and middlewares (redirects, auth, rate limits, headers). It is the default ingress controller of k3s.

Static config (entry points, providers, certificate resolvers) comes from traefik.yml or flags; dynamic config (routers, services, middlewares) comes from the providers.

طريقة الكتابة

traefik [--configFile=FILE] [--FLAG=value]...
traefik healthcheck | version

الأجزاء الرئيسية

Concepts

entryPoints
The ports Traefik listens on (web :80, websecure :443).
routers
Rules (Host, PathPrefix) that match requests.
services
The backends that get the traffic.
middlewares
Changes on the way: redirects, auth, headers, rate limits.
providers
Where routes come from: docker, kubernetesCRD, kubernetesIngress, file.
certificatesResolvers
How TLS certificates are obtained (ACME / Let's Encrypt).

الخيارات 13

الأكثر استخدامًا

Common flags (also in traefik.yml)

--configFile=FILE
Static configuration file.
--entryPoints.NAME.address=:PORT
Define an entry point.
--providers.docker=true
Read routes from Docker labels.
--providers.docker.exposedByDefault=false
Only containers with traefik.enable=true.
--providers.kubernetescrd / --providers.kubernetesingress
Read routes from Kubernetes.
--providers.file.directory=DIR
Read routes from files.
--certificatesResolvers.NAME.acme.email=MAIL
Let's Encrypt account email.
--certificatesResolvers.NAME.acme.storage=FILE
Where certificates are kept.
--certificatesResolvers.NAME.acme.httpChallenge.entryPoint=web
Use the HTTP challenge.
--api.dashboard=true
Enable the dashboard.
--metrics.prometheus=true
Expose Prometheus metrics.
--accessLog=true
Write access logs.
--log.level=LEVEL
DEBUG, INFO, WARN, ERROR.

أمثلة 10

$ مستخدم عادي# بصلاحية root (أو مع sudo)

  1. 1Run Traefik with the Docker provider

    docker run -d --name traefik -p 80:80 -p 443:443 -v /var/run/docker.sock:/var/run/docker.sock:ro -v ./acme:/acme traefik:v3.2 --providers.docker=true --providers.docker.exposedByDefault=false --entryPoints.web.address=:80 --entryPoints.websecure.address=:443 --certificatesResolvers.le.acme.email=admin@example.com --certificatesResolvers.le.acme.storage=/acme/acme.json --certificatesResolvers.le.acme.httpChallenge.entryPoint=web
  2. 2Publish a container with labels

    docker run -d --name shop -l traefik.enable=true -l 'traefik.http.routers.shop.rule=Host(`shop.example.com`)' -l traefik.http.routers.shop.entrypoints=websecure -l traefik.http.routers.shop.tls.certresolver=le -l traefik.http.services.shop.loadbalancer.server.port=8080 ghcr.io/s0sta/shop:1.4.0
  3. 3The same with Docker Compose

    cat compose.yaml
    services:
      shop:
        image: ghcr.io/s0sta/shop:1.4.0
        labels:
          - traefik.enable=true
          - traefik.http.routers.shop.rule=Host(`shop.example.com`)
          - traefik.http.routers.shop.tls.certresolver=le
  4. 4Redirect all HTTP to HTTPS

    cat traefik.yml
    entryPoints:
      web:
        address: ":80"
        http:
          redirections:
            entryPoint: { to: websecure, scheme: https }
      websecure:
        address: ":443"
  5. 5Route in Kubernetes with an IngressRoute

    cat ingressroute.yaml
    apiVersion: traefik.io/v1alpha1
    kind: IngressRoute
    metadata: { name: shop, namespace: shop }
    spec:
      entryPoints: [websecure]
      routes:
        - match: Host(`shop.example.com`) && PathPrefix(`/api`)
          kind: Rule
          services: [{ name: api, port: 8080 }]
      tls: { certResolver: le }
    kubectl apply -f ingressroute.yaml
  6. 6Add basic auth with a middleware

    htpasswd -nb admin 'S3cret!'
    docker run -d -l 'traefik.http.middlewares.auth.basicauth.users=admin:$$apr1$$...' -l traefik.http.routers.admin.middlewares=auth ...
  7. 7Add security headers

    cat dynamic/headers.yml
    http:
      middlewares:
        secure-headers:
          headers:
            stsSeconds: 31536000
            frameDeny: true
            contentTypeNosniff: true
  8. 8See the dashboard (local only)

    ssh -L 8080:localhost:8080 admin@proxy1
    curl -s http://localhost:8080/api/http/routers | jq '.[].rule'
  9. 9Check health and version

    traefik healthcheck --ping
    traefik version
  10. 10Install as the k3s ingress (already there)

    kubectl get pods -n kube-system -l app.kubernetes.io/name=traefik

انتبه

  • Mounting the Docker socket gives Traefik root power on the host. Mount it read-only, or use a socket proxy.
  • Protect acme.json (rights 600); it holds the private keys of all certificates.
  • Do not expose the dashboard publicly without authentication.

الملفات

/etc/traefik/traefik.yml
Static configuration (path can differ).
acme.json
Stored certificates and keys.

نصائح للحفظ

  • Use Let's Encrypt staging while testing to avoid rate limits.

رموز الخروج

0Success.
1Error.

الدليل الكامل

هذه الصفحة تشرح الأمر بكلمات بسيطة. الدليل الأصلي فيه كل التفاصيل.

في الطرفية: man traefik