1:Traefik 做爲web服務,使用etd庫做爲配置統一存儲空間,實現traefik服務能夠方便的增刪節點,解耦traefik服務啓動後服務配置的問題。 2:Traefik 使用http驗證方式申請ssl證書,即每配置一個域名讓traefik 幫咱們自動申請一個ssl證書.
1:安裝 traefik 1.7+ 2:安裝 etcd 3:安裝 Etcdkeeper 4:配置 traefik 對接 etcd 庫
使用AWS雲平臺測試: 雲主機一臺、配置公網IP地址,開放80 443 8080 1180 端口 traefik v1.7+ web 服務 佔用端口: http 80 https 443 traefik 管理頁面 8080 配置目錄 /etc/traefik Etcd: 高可用、強一致性的服務發現存儲倉庫, 做爲traefik 後端配置存儲 佔用端口: 外部客戶端鏈接 2379 etcd服務間通訊 2380 Etcdkeeper: Etcd web界面,支持v3的api 佔用端口(能夠本身設置):11800 Docker : 用於模擬快速啓動一個後端web服務 鏡像地址:containous/whoami
佔用端口: http 80 https 443 traefik 管理頁面 8080 配置目錄 /etc/traefik
wget https://github.com/containous/traefik/releases/download/v1.7.19/traefik_linux-amd64 mv traefik_linux-amd64 /usr/bin/traefik chmod 755 /usr/bin/traefik mkdir -p /etc/traefik touch /etc/traefik/acme.json chmod 755 /etc/traefik chmod 600 /etc/traefik/acme.json
[root@ip-10-3-1-119 traefik]# traefik version Version: v1.7.19 Codename: maroilles Go version: go1.12.12 Built: 2019-10-28_02:07:32PM OS/Arch: linux/amd64
至此traefik配置完畢 !!! html
docker run -d -p 8880:80 containous/whoami docker run -d -p 8890:80 containous/whoami
vi /etc/traefik/traefik.toml
#開啓debug 模式,方便調試,Default,false debug = true #日誌級別, "DEBUG", "INFO", "WARN", "ERROR", "FATAL", "PANIC" logLevel = "INFO" # 同時支持http和https defaultEntryPoints = ["http", "https"] [entryPoints] [entryPoints.http] address = ":80" #啓用壓縮傳輸 compress = true #http強制跳轉https [entryPoints.http.redirect] entryPoint = "https" [entryPoints.https] address = ":443" #啓用壓縮傳輸 compress = true [entryPoints.https.tls] # 配置自動Let's Encrypt證書 [acme] email = "kjh@mail.com" ##加密文件的存儲位置 storage = "/etc/traefik/acme.json" ##證書類型,必需指向到一個443端口 entryPoint = "https" #在新域名接受第一次https請求時申請證書 onDemand = false #自動爲acme.entryPoint下的新域名申請證書 onHostRule = true [acme.httpChallenge] #acme 驗證方式支持 dns 、http、tls,本次使用https #https://letsencrypt.org/zh-cn/docs/challenge-types/ entryPoint="http" # 開啓日誌功能 #成功訪問日誌 [accessLog] filePath = "/var/log/traefik/acceslog.txt" format = "json" #服務啓動日誌 [traefikLog] filePath = "/var/log/traefik/traefik.log" # 開啓web管理端 [web] address = ":8080" #設置RESTAPI 爲只讀模式 readOnly = true [web.auth.basic] #test/test 登錄名/密碼 可用openssl生成 #users = ["test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/"] #啓用詳細信息輸出,會在管理界面下方打印一些錯誤信息,提供參考; [web.statistics] ecentErrors = 10 #開啓api,修改服務配置,生產環境推薦添加加密認證 [api] entryPoint = "traefik" #開啓管理面板 dashboard = true debug = true #使用文件方式管理配置 [file] #在指定目錄查找配置文件 directory = "/etc/traefik/rules" #監視文件變動 watch = true #使用etcd做爲存儲開啓此配置,須要與文件管理配置同時開啓,否則沒法正常同步配置 [etcd] #節點地址:端口 endpoint = "10.3.1.119:2379" #強制使用v3版本api useAPIV3 = true #監視配置變動 watch = true
/etc/traefik/rules web域名爲:kjh.pt1.jp 後端服務: http://54.238.247.28:8880 http://10.3.1.119:8890
vi /etc/traefik/rules/kjh.pt1.toml 添加配置以下:
[frontends] [frontends.ft01] backend = "bk01" [frontends.ft01.routes.rule_1] rule = "Host:kjh.pt1.jp,kjh01.pt1.jp" [backends] [backends.bk01] [backends.bk01.servers.server1] url = "http://54.238.247.28:8880" weight = 10 [backends.bk01.servers.server2] url = "http://10.3.1.119:8890" weight = 10
注意:上面配置的域名須要先在dns 作解析,指向traefik節點IP地址,否則沒法正常申請ssl證書!!! linux
yum install etcd -y vi /etc/etcd/etcd.conf 修改 ttp://localhost:2379 爲 http://0.0.0.0:2379 #主要修改項以下: ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379" ETCD_ADVERTISE_CLIENT_URLS="http://0.0.0.0:2379" #啓動服務 systemctl daemon-reload systemctl start etcd systemctl status etcd
wget https://github.com/evildecay/etcdkeeper/releases/download/v0.7.5/etcdkeeper-v0.7.5-linux_x86_64.zip unzip etcdkeeper-v0.7.5-linux_x86_64.zip cd etcdkeeper chmod 755 etcdkeeper #後臺啓動 ./etcdkeeper -p 11800 &
若是能夠瀏覽 etd 庫內容爲正常,但此時並沒有文件;github
同步traefik 配置至 etcd庫web
traefik storeconfig -c traefik.toml
[root@ip-10-3-1-119 traefik]# traefik storeconfig traefik.toml ........ 0,"DebugLogGeneratedTemplate":false,"Directory":"/etc/traefik/rules","TraefikFile":""} 2020/01/05 21:19:22 Writing config to KV
1:導入配置時返回信息無報錯,返回 Writing config to KV;
2:訪問etcd ui ,查看traefik配信息是否導入成功,顯示目錄大體以下;docker
/ etc traefik
執行traefik 命令便可,默認會到/etc/traefik/目錄下尋找配置文件。
訪問traefik 節點IP地址:8080 能夠看到 文件的配置和KV 庫的配置,配置文件配置的域名能夠正常訪問。
效果圖以下:json
https://zhuanlan.zhihu.com/p/74042144
http://www.javashuo.com/article/p-wacpslhw-er.html
https://ystyle.top/2017/12/08/traefik-getting-start/
https://jupyterhub-traefik-proxy.readthedocs.io/en/latest/toml.html
https://my.oschina.net/guol/blog/2209678後端