traefik 配置自動申請ssl免費證書

什麼是 SSL 證書?

安全套接字層 (SSL) 證書(有時稱爲數字證書)用於在瀏覽器或用戶計算機與服務器或網站之間建立加密連接。SSL 連接可保護在每次訪問(稱爲會話)期間交換的敏感數據(例如信用卡信息),以防被非授權方攔截。SSL 連接可保護在每次訪問(稱爲會話)期間交換的敏感數據(例如信用卡信息),以防被非授權方攔截。

實現目標:

traefik 轉發或者提供的域名都能夠支持https 請求!

測試環境介紹

K8s 集羣
阿里雲dns賬戶:(需要讀寫dns服務)
ALICLOUD_ACCESS_KEY
ALICLOUD_SECRET_KEY

k8s - yaml 文件如下:

創建用戶授權

---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: traefik-outer-ingress-controller
rules:
  - apiGroups:
      - ""
    resources:
      - services
      - endpoints
      - secrets
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - extensions
    resources:
      - ingresses
    verbs:
      - get
      - list
      - watch
  - apiGroups:
    - extensions
    resources:
    - ingresses/status
    verbs:
    - update
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: traefik-outer-ingress-controller
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: traefik-outer-ingress-controller
subjects:
- kind: ServiceAccount
  name: traefik-outer-ingress-controller
  namespace: kube-system

服務配置

注意: 所有需要啓動traefik的節點配置標籤如下
traefik: "traefik-outer"

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: traefik-outer-ingress-controller
  namespace: kube-system
---
kind: DaemonSet
apiVersion: extensions/v1beta1
metadata:
  name: traefik-outer-ingress-controller
  namespace: kube-system
  labels:
    k8s-app: traefik-outer-ingress-lb
spec:
  selector:
    matchLabels:
      k8s-app: traefik-outer-ingress-lb
  template:
    metadata:
      labels:
        k8s-app: traefik-outer-ingress-lb
        name: traefik-outer-ingress-lb
    spec:
      serviceAccountName: traefik-outer-ingress-controller
      terminationGracePeriodSeconds: 60
      hostNetwork: true
      containers:
      - image: traefik:1.7.19
        name: traefik-outer-ingress-lb
        env:
        - name: ALICLOUD_ACCESS_KEY              # 添加環境變量ALICLOUD_ACCESS_KEY
          value: LTAIxxxxxxxxxxxAYfXqk                 # 阿里雲RAM賬號的access_key
        - name: ALICLOUD_SECRET_KEY              # 添加環境變量ALICLOUD_SECRET_KEY
          value: gfNxxxxxxxxxxxoOslfc                   # 阿里雲RAM賬號的access_secret
        resources:
          limits:
            cpu: 1000m
            memory: 1024Mi
          requests:
            cpu: 1000m
            memory: 1024Mi
        ports:
        - name: http
          containerPort: 80
          hostPort: 80
        - name: https
          containerPort: 443
          hostPort: 443
        - name: admin
          containerPort: 8080
          hostPort: 8080
        args:
        - --api
        - --kubernetes
        - --configfile=/traefik.toml
        - --insecureskipverify            #如果後端服務是https協議時不驗證其證書
        - --logLevel=INFO                 #日誌級別
        - --defaultEntryPoints=http,https #traefik同時開啓HTTP和HTTPS服務
        - --entrypoints=Name:https Address::443 TLS #HTTPS服務監聽在443端口
        - --entrypoints=Name:http Address::80 #HTTPS服務監聽在443端口,與http跳轉https配置衝突,只能配置一項;
#        - --entrypoints=Name:http Address::80 Redirect.EntryPoint:https #HTTP監聽在80端口,並將流量重定向至https
        - --acme                         #開啓證書驗證
        - [email protected]   #用於註冊的郵箱地址
        - --acme.storage=/tmp/acme.json  #證書申請臨時文件存儲位置
        - --acme.acmeLogging=true        #打開日誌,方便排錯
        - --acme.entryPoint=https        #證書類型,必需指向到一個443端口
        - --acme.httpchallenge.entrypoint=http    # 驗證域名時使用的協議
        - --acme.dnschallenge                     # 域名驗證方式
        - --acme.dnschallenge.provider=alidns     # 域名提供商
        - --acme.dnschallenge.delaybeforecheck=5  # 驗證域名延時
        - --acme.onHostRule=true      #自動爲acme.entryPoint下的新域名申請證書
        - --acme.domains=ptmind.com   #要申請證書的域名
        - --acme.domains=lingxi365.cn #要申請證書的域名
        - --acme.domains=lingxi.link  #要申請證書的域名
      nodeSelector:
        traefik: "traefik-outer"
---
kind: Service
apiVersion: v1
metadata:
  name: traefik-outer-ingress-service
  namespace: kube-system
spec:
  selector:
    k8s-app: traefik-outer-ingress-lb
  ports:
    - protocol: TCP
      port: 80
      name: web
    - protocol: TCP
      port: 443
      name: https
    - protocol: TCP
      port: 8080
      name: admin
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: traefik-outer-web-ui
  namespace: kube-system
spec:
  rules:
  - host: traefik.ptmind.com #配置管理頁面的域名
    http:
      paths:
      - path: /
        backend:
          serviceName: traefik-outer-ingress-service
          servicePort: admin

traefik 命令


--api --kubernetes --configfile=/traefik.toml --insecureskipverify --logLevel=INFO \
--defaultEntryPoints=http,https '--entrypoints=Name:https Address::443 TLS' \
'--entrypoints=Name:http Address::80' --acme --acme.dnschallenge [email protected] \
--acme.storage=/tmp/acme.json --acme.acmeLogging=true --acme.entryPoint=https --acme.httpchallenge.entrypoint=http \
--acme.dnschallenge.provider=alidns --acme.dnschallenge.delaybeforecheck=5 \
--acme.domains=ptmind.com --acme.domains=lingxi365.cn --acme.domains=lingxi.link \
--acme.onHostRule=true

其它參數解釋

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