Traefik 介绍

发布时间: 更新时间: 总字数:334 阅读时间:1m 作者: IP上海 分享 网址

Traefik是一款云原生应用访问代理,可帮助开发人员和devops快速轻松地构建、部署运行微服务

Docker

安装

cat << EOF > docker-compose.yml
version: '3'

services:
  reverse-proxy:
    # The official v2 Traefik docker image
    image: traefik:v2.3
    # Enables the web UI and tells Traefik to listen to docker
    command: --api.insecure=true --providers.docker
    ports:
      # The HTTP port
      - "80:80"
      # The Web UI (enabled by --api.insecure=true)
      - "8080:8080"
    volumes:
      # So that Traefik can listen to the Docker events
      - /Users/xiexianbin/.docker/run/docker-cli-api.sock:/var/run/docker.sock
  whoami:
    # A container that exposes an API to show its IP address
    image: traefik/whoami
    labels:
      - "traefik.http.routers.whoami.rule=Host(`whoami.docker.localhost`)"
EOF
docker-compose up -d reverse-proxy

访问

  • http://localhost:8080/api/rawdata 查看当前api数据
  • http://localhost:8080/dashboard web界面

自动发现新服务

docker-compose up -d whoami

http://localhost:8080/api/rawdata 数据已更新

访问服务:

# curl -H Host:whoami.docker.localhost http://127.0.0.1
Hostname: 99916d96c8c3
...

Traefik Load Balances

docker-compose up -d --scale whoami=2
# curl -H Host:whoami.docker.localhost http://127.0.0.1
Hostname: 495948c5d833
IP: 127.0.0.1
IP: 192.168.32.4
RemoteAddr: 192.168.32.2:52368
GET / HTTP/1.1
Host: whoami.docker.localhost
User-Agent: curl/7.29.0
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 192.168.32.1
X-Forwarded-Host: whoami.docker.localhost
X-Forwarded-Port: 80
X-Forwarded-Proto: http
X-Forwarded-Server: 565353f9f1a1
X-Real-Ip: 192.168.32.1

# curl -H Host:whoami.docker.localhost http://127.0.0.1
Hostname: 99916d96c8c3
IP: 127.0.0.1
IP: 192.168.32.3
RemoteAddr: 192.168.32.2:43664
GET / HTTP/1.1
Host: whoami.docker.localhost
User-Agent: curl/7.29.0
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 192.168.32.1
X-Forwarded-Host: whoami.docker.localhost
X-Forwarded-Port: 80
X-Forwarded-Proto: http
X-Forwarded-Server: 565353f9f1a1
X-Real-Ip: 192.168.32.1

k8s

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

kubectl create ns traefik-v2
# Install in the namespace "traefik-v2"
helm install --namespace=traefik-v2 traefik traefik/traefik

port-forward

kubectl port-forward $(kubectl get pods --selector "app.kubernetes.io/name=traefik" --output=name) --address 0.0.0.0 9000:9000
  • http://127.0.0.1:9000/dashboard/

dashboard

kubectl apply -f dashboard.yaml
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: dashboard
spec:
  entryPoints:
    - web
  routes:
    - match: Host(`traefik.kb.cx`) && (PathPrefix(`/dashboard`) || PathPrefix(`/api`))
      kind: Rule
      services:
        - name: api@internal
          kind: TraefikService
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数