traefik 結合 docker-compose 的快速安裝及使用

traefik 介紹

traefik 是一個爲了讓部署微服務更加便捷而誕生的現代HTTP反向代理、負載均衡工具。 它支持多種後臺 (Docker, Swarm, Kubernetes, Marathon, Mesos, Consul, Etcd, Zookeeper, BoltDB, Rest API, file…) 來自動化、動態的應用它的配置文件設置。

特性一覽

  • 它非常快
  • 無需安裝其他依賴,通過 Go 語言編寫的單一可執行文件
  • 支持 Rest API
  • 多種後臺支持:Docker, Swarm, Kubernetes, Marathon, Mesos, Consul, Etcd, 並且還會更多
  • 後臺監控, 可以監聽後臺變化進而自動化應用新的配置文件設置
  • 配置文件熱更新。無需重啓進程
  • 正常結束 http 連接
  • 後端斷路器
  • 輪詢,rebalancer 負載均衡
  • Rest Metrics
  • 支持最小化 官方 docker 鏡像
  • 後臺支持 SSL
  • 前臺支持 SSL(包括 SNI)
  • 清爽的 AngularJS 前端頁面
  • 支持 Websocket
  • 支持 HTTP/2
  • 網絡錯誤重試
  • 支持 Let’s Encrypt (自動更新 HTTPS 證書)
  • 高可用集羣模式

本文將分享 traefik 結合 docker-compose 的一點使用經驗。

docker(docker-ce) 及 docker-compose(1.23.2) 的快速安裝

sudo curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
sudo curl -L https://github.com/docker/compose/releases/download/1.23.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

docker-compose 安裝 traefik 及使用

用其搭配 docker-compose 部署網站,可輕鬆綁定域名,設置 https , 負載均衡,已在多個項目使用,文檔可靠,強烈推薦!

以下爲使用的基本操作

  1. 創建 network,使 traefik 及網站處於同一網絡
  2. 創建 traefik.toml(官方文檔)
  3. 創建 acme.json (touch acme.json && chmod 600 ./acme.json)
  4. 創建 docker-compose.yml

docker-compose.yml

使用 docker-compose up -d 即可構建 traefik 服務,
根據 labels 標籤通過監聽其內部的 8090 端口,並綁定了域名 traefik.testdomain.com

docker-compose 可使用的 labes 配置見文檔:http://docs.traefik.cn/toml#docker-backend
traefik.toml 配置見文檔:http://docs.traefik.cn/toml

使用之前需要先創建一個網絡(docker network create me_gateway),讓 traefik 及所有網站都使用一個網絡,這樣就能夠自動將域名綁定到對應的容器中

下面是一個 traefik 的 docker-compose.yml 配置

version: '3'
services:
  me_traefik:
    restart: always
    image: traefik:1.7.4
    ports:
      - '80:80'
      - '443:443'
    labels:
      - 'traefik.backend=me_traefik'
      - 'traefik.frontend.rule=Host:traefik.testdomain.com'
      - 'traefik.enable=true'
      - 'traefik.passHostHeader=true'
      - 'traefik.protocol=https'
      - 'traefik.port=8090'
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./traefik.toml:/traefik.toml
      - ./acme.json:/acme.json
    networks:
      - webgateway
networks:
  webgateway:
    external:
      name: me_gateway

volumes 說明

  • /var/run/docker.sock:主機 docker
  • ./traefik.toml:traefik 配置文件
  • ./acme.json:Let's Encrypt 配置,會根據 traefik.toml 生成,映射出來,後續重啓數據將不會丟失,但是需要爲其添加讀寫權限(chmod 600 acme.json),初始化時可以 touch acme.json 生成一個空文件

下面是一個 traefik 的 traefik.toml 配置示例

################################################################
# Global configuration
################################################################
debug = false
logLevel = "ERROR"
defaultEntryPoints = ["http","https"]
[entryPoints]
  [entryPoints.http]
  address = ":80"
#    [entryPoints.http.redirect]
#      entryPoint = "https"
# 啓用https
  [entryPoints.https]
  address = ":443"
    [entryPoints.https.tls]
  [entryPoints.webentry]
    address = ":8090"
    [entryPoints.webentry.auth]
      [entryPoints.webentry.auth.basic]
         users = ["test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/"]
[api]
dashboard = true
entrypoint = "webentry"

[ping]

[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "testdomain.com"
watch = true
exposedByDefault = false
usebindportip = true
swarmMode = false
network = "me_gateway"

[acme]
# 用於註冊的郵箱地址
email = "[email protected]"
# 證書存儲使用的文件或鍵。
storage = "acme.json"
# 代理acme驗證證書challenge/apply的入口點。
# 警告, 必需指向到一個443端口作爲入口點
entryPoint = "https"
# 啓用按需證書。如果這個主機名還沒有證書,這將會在與一個主機名發起請求的第一個TLS握手中向Let's Encrypt請求一個證書。
# 警告,第一次在請求中獲取主機證書會導致TLS握手會非常慢,這會引起Dos攻擊。
# 警告,值得注意的是Let's Encrypt是有請求上限的:https://letsencrypt.org/docs/rate-limits
onDemand = false
# 啓用根據前端Host規則來生成證書。這將會爲每個具有Host規則的前端生成一個Let's Encrypt的證書。
# 舉個例子,一個具有規則的Host:test1.traefik.cn,test2.traefik.cn 將會爲主域名test1.traefik.cn與SAN(替代域名) test2.traefik.cn生成一個證書。
onHostRule = true
  [acme.httpChallenge]
  entryPoint="http"

traefik 自動申請 https 證書(Let’s Encrypt)

文檔地址:http://docs.traefik.cn/toml#acme-lets-encrypt-configuration

defaultEntryPoints = ["http","https"]
[entryPoints]
  [entryPoints.http]
  address = ":80"
#    [entryPoints.http.redirect]
#      entryPoint = "https"
# 啓用https
  [entryPoints.https]
  address = ":443"
    [entryPoints.https.tls]
  [entryPoints.webentry]
    address = ":8090"

traefik 自定義入口並配置賬戶密碼

文檔地址:http://docs.traefik.cn/toml#entrypoints-definition
但是,按照文檔來,可能並不一定能夠配置成功~,如果不成功就參考下面的配置吧,

defaultEntryPoints = ["http","https"]
[entryPoints]
  [entryPoints.http]
  address = ":80"
#    [entryPoints.http.redirect]
#      entryPoint = "https"
# 啓用https
  [entryPoints.https]
  address = ":443"
    [entryPoints.https.tls]
  [entryPoints.webentry]
    address = ":8090"
    [entryPoints.webentry.auth]
      [entryPoints.webentry.auth.basic]
         users = ["test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/"]
[api]
dashboard = true
entrypoint = "webentry"
  1. 在 api 節點指定 entrypoint= 入口點
  2. 配置 entryPoints 節點
[entryPoints]
    [entryPoints.入口點]
        address = ":8090"
        [entryPoints.webentry.auth]
        [entryPoints.webentry.auth.basic]
            users = ["test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/"]

上訴密碼需要使用 htpasswd 生成,可在服務器生成,也可使用新鮮出爐的 metools 的htpasswd 密碼生成
在線生成了。

當 traefik 部署完成,後續網站綁定域名只需要在 docker-compose.yml 中指定 labels對應值即可自動綁定域名,申請 https 等操作了(指定到同一個網絡),關於更多使用場景及方法,還是需要去查看文檔 ,簡單的可以參考我的配置,clone 後通過 docker-compose.yml 就可快速在服務器構建你的項目了

相關地址

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章