Traefik-v2.x快速入門

Traefik v2.0 快速入門

traefik官方文檔python

注意:Traefikv2.0以後的版本在修改了不少bug以後也增長了新的特性,好比增長了TCP的支持,而且更換了新的WEB UI界面git

使用docker-compose來快速啓動traefki實例.

# 查看編輯好的docker-compose配置文件
$ cat traefik-v2.0.yaml
version: '3'
services:
  reverse-proxy:
    image: traefik:2.0.1
    # Enables the web UI and tells Traefik to listen to docker
    # 啓用webUI 並告訴Traefile去監聽docker的容器實例
    command: --api.insecure=true --providers.docker
    ports:
      # traefik暴露的http端口
      - "80:80"
      # webUI暴露的端口(必須制定--api.insecure=true才能夠訪問)
      - "8080:8080"
    volumes:
      # 指定docker的sock文件來讓traefik獲取docker的事件,從而實現動態負載均衡
      - /var/run/docker.sock:/var/run/docker.sock


# 使用docker-compose建立集羣
$ docker-compose -f traefik-v2.0.yaml up -d reverse-proxy
Creating 20190927_reverse-proxy_1 ... done

# 查看使用docker-compose啓動的應用
$ docker-compose -f traefik-v2.0.yaml ps
          Name                        Command               State                     Ports
--------------------------------------------------------------------------------------------------------------
20190927_reverse-proxy_1   /entrypoint.sh --api.insec ...   Up      0.0.0.0:80->80/tcp, 0.0.0.0:8080->8080/tcp

# 直接訪問traefik對外暴露的http接口
curl -s   "http://localhost:8080/api/rawdata" | python -m json.tool
{
    "routers": {
        "reverse-proxy-20190927@docker": {
            "rule": "Host(`reverse-proxy-20190927`)",
            "service": "reverse-proxy-20190927",
            "status": "enabled",
            "using": [
                "http",
                "traefik"
            ]
        }
    },
    "services": {
        "reverse-proxy-20190927@docker": {
            "loadBalancer": {
                "passHostHeader": true,
                "servers": [
                    {
                        "url": "http://172.21.0.2:80"
                    }
                ]
            },
            "serverStatus": {
                "http://172.21.0.2:80": "UP"
            },
            "status": "enabled",
            "usedBy": [
                "reverse-proxy-20190927@docker"
            ]
        }
    }
}

複製代碼

查看Traefik官方Dashboard:github

Traefik來檢測新服務併爲你建立一個路由

# 建立一個新服務
$ cat test-service.yaml
version: '3'
services:
  whoami:
    image: containous/whoami
    labels:
      - "traefik.http.routers.whoami.rule=Host(`whoami.docker.localhost`)"

# 建立服務
$ docker-compose  -f test-service.yaml up -d whoami
Pulling whoami (containous/whoami:)...
latest: Pulling from containous/whoami
6f3614cb05a5: Pull complete

# 查看新建立的服務
$ docker-compose  -f test-service.yaml ps
      Name          Command   State   Ports
--------------------------------------------
20190927_whoami_1   /whoami   Up      80/tcp

# 再次查看traefik中的路由信息(就會發現服務自動加載進去了)
# 其實有點兒相似kong 的路由,只是traefik會自動監聽docker的事件
$ curl -s   "http://localhost:8080/api/rawdata" | python -m json.tool
{
    "routers": {
        "reverse-proxy-20190927@docker": {
            "rule": "Host(`reverse-proxy-20190927`)",
            "service": "reverse-proxy-20190927",
            "status": "enabled",
            "using": [
                "http",
                "traefik"
            ]
        },
        "whoami@docker": {
            "rule": "Host(`whoami.docker.localhost`)",
            "service": "whoami-20190927",
            "status": "enabled",
            "using": [
                "http",
                "traefik"
            ]
        }
    },
    "services": {
        "reverse-proxy-20190927@docker": {
            "loadBalancer": {
                "passHostHeader": true,
                "servers": [
                    {
                        "url": "http://172.21.0.2:80"
                    }
                ]
            },
            "serverStatus": {
                "http://172.21.0.2:80": "UP"
            },
            "status": "enabled",
            "usedBy": [
                "reverse-proxy-20190927@docker"
            ]
        },
        "whoami-20190927@docker": {
            "loadBalancer": {
                "passHostHeader": true,
                "servers": [
                    {
                        "url": "http://172.21.0.3:80"
                    }
                ]
            },
            "serverStatus": {
                "http://172.21.0.3:80": "UP"
            },
            "status": "enabled",
            "usedBy": [
                "whoami@docker"
            ]
        }
    }
}


複製代碼

查看Traefik中的http反向代理記錄:web

測試traefik相關功能

# 測試訪問
$ curl -H Host:whoami.docker.localhost http://localhost
Hostname: f1b280a61fa7
IP: 127.0.0.1
IP: 172.21.0.3
RemoteAddr: 172.21.0.2:44604
GET / HTTP/1.1
Host: whoami.docker.localhost
User-Agent: curl/7.54.0
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 172.21.0.1
X-Forwarded-Host: whoami.docker.localhost
X-Forwarded-Port: 80
X-Forwarded-Proto: http
X-Forwarded-Server: bf98c245d265
X-Real-Ip: 172.21.0.1

# 單機擴容
$ docker-compose -f test-service.yaml up -d --scale whoami=2

# 再次訪問(就會發現自動負載到兩個不一樣的實例上去了)
$ curl -H Host:whoami.docker.localhost http://localhost
Hostname: 3045eb2f3a89
IP: 127.0.0.1
IP: 172.21.0.4
RemoteAddr: 172.21.0.2:55182
GET / HTTP/1.1
Host: whoami.docker.localhost
User-Agent: curl/7.54.0
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 172.21.0.1
X-Forwarded-Host: whoami.docker.localhost
X-Forwarded-Port: 80
X-Forwarded-Proto: http
X-Forwarded-Server: bf98c245d265
X-Real-Ip: 172.21.0.1
複製代碼

查看Traefike後端每一個service的詳情信息:docker

Traefik配置介紹

traefik配置結構圖:json

在traefik中的配置,會涉及到兩方面內容:後端

  • 動態的路由配置(即由k8s-api或docker相關api來自動發現服務的endpoint而進行路由的配置描述)
  • 靜態的啓動配置(即traefik標準的啓動配置參數)

注意:使用docker run traefik[:version] --help可查看traefik的配置參數api

在k8s集羣中部署traefik-v2.0.1

$ kubectl apply -f https://github.com/BGBiao/k8s-ansible-playbooks/blob/master/manifest/traefik/traefik-ds-v2.0.1.yaml

$ kubectl get pods  -n kube-system   | grep traefik-ingress-controller-v2
traefik-ingress-controller-v2-54h54   1/1     Running   0          29m
traefik-ingress-controller-v2-lh2jg   1/1     Running   0          29m
traefik-ingress-controller-v2-tqbvd   1/1     Running   0          29m
traefik-ingress-controller-v2-ww449   1/1     Running   0          29m

$ kubectl --kubeconfig ../../files/kubelet.kubeconfig get svc -n kube-system  | grep v2
traefik-ingress-service-v2   ClusterIP   10.253.86.121   <none>        81/TCP,8081/TCP          30m
複製代碼

查看traefik-v2.0.1的dashboard:app

注意:雖然traefikv2.x改動了不少,可是仍是向下兼容一些內容的,好比我從新建立traefik-v2.0.1以後,以前建立的ingress規則會自動導入歡迎關注個人公衆號,一塊兒進步~負載均衡

相關文章
相關標籤/搜索