Traefik is an open-source Edge Router that makes publishing your services a fun and easy experience. It receives requests on behalf of your system and finds out which components are responsible for handling them.
英文自己看哈,可以理解為和nginx做的工作一樣,進行負載均衡,端口暴露等,但是比nginx靈活的多,支持自動更新配置。
三個主要的概念
1、entryPoint
是訪問traefik的網絡接入端口,支持http和TCP。
entrypoint只能定義在靜態文件中,traefik啟動默認加載的文件,默認目錄為 /etc/traefic,所以把配置文件丟到這個目錄下
1 #static configuration 2 api: 3 dashboard: true 4 insecure: true 5 providers: 6 file: 7 filename: "/etc/traefik/dynamic-conf.yml" 8 watch: true 9 entryPoints: 10 web: 11 address: ":80" 12 web-secure: 13 address: ":443" 14 traefik: 15 address: ":8080" 16 cluster-9001: 17 address: ":9001" 18 cluster-9002: 19 address: ":9002" 20 cluster-9003: 21 address: ":9003" 22 cluster-9004: 23 address: ":9004" 24
2、router
routers可以理解為traefik接收到的請求如何路由到service上,或者又哪個service轉到真實的服務商,可以包含許多的規則。
配置樣例(配置到動態配置文件中)
1 tcp: 2 routers: 3 cluster-router-111111: 4 entryPoints: 5 - "cluster-9001" 6 rule: "HostSNI(`*`)" 7 service: "cluster-service-9001"
3、service
The Services
are responsible for configuring how to reach the actual services that will eventually handle the incoming requests.
簡單理解service就是定義如何訪問到真實的服務上去
1 services: 2 cluster-service-9001: 3 loadBalancer: 4 servers: 5 - address: "10.20.26.119:20001" 6 - address: "10.20.26.119:20002"
4、測試
啟動方式
sudo ./traefik (cd到traefik的下載目錄下,把配置文件都丟到/ect/traefik下,啟動的時候會自動到該目錄下加載配置,也會從其他的目錄加載配置,具體的請參考官網說明)
訪問http://xxx.xxx.xxx.xxx:8080/,因為在entrypoints里已經將8080端口開放了。
在Service Details里,Servers是就是真實的服務提供Ip和端口,示例中,20001端口和20002端口都是映射的redis的6379端口。用docker啟動的redis
其中,-p 9002端口是我在entrypoint中暴露的端口,測試成功
更詳細的使用要參考官網:https://docs.traefik.io/v2.1/