1. 程式人生 > >【Kubernetes】traefik代理重定向302過多問題

【Kubernetes】traefik代理重定向302過多問題

問題描述

通過traefik添加了服務域名代理,如:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: common-service
  namespace: default
  annotations:
    kubernetes.io/ingress.class: traefik
spec:
  rules:
  - host: test.example.com
    http:
      paths:
      - path: /
        backend:
          serviceName
: test-server servicePort: 8080

訪問該服務時, http://test.example.com, 陷入了302重定向的死迴圈:

location: https://test.example.com:443/

問題跟蹤解決

經過排查後,發現是在自己的traefik.toml配置中,所有的http請求都會被轉發到https處理,如:

defaultEntryPoints = ["http","https"]
insecureSkipVerify = true
[entryPoints]
  [entryPoints.http]
  address = ":80"
  [entryPoints.http.redirect]
    entryPoint = "https"
  [entryPoints.https]
    address = ":443"
  [entryPoints.https.tls]
    [[entryPoints.https.tls.certificates]]
      certFile = "/opt/k8s/ssl/ssl.crt"
      keyFile = "/opt/k8s/ssl/ssl.key"

因為我的服務test-server是隱藏在nginx代理之後的,大概就經歷了這麼一個流程:

使用者 --(https://test.example.com)--> nginx --(http://192.168.x.xx)--> test-server (返回302地址:https://test.example.com:443)
使用者 --(https://test.example.com:443)--> nginx --(http://192.168.x.xx)--> test-server (返回302:https://test.example.com:443)
// 死迴圈

當前的解決方法: 取消http強制轉https的邏輯, 或者只對指定的域名轉https協議。

#註釋traefik.toml以下兩行, 更新configmap及test-server
#  [entryPoints.http.redirect]
#    entryPoint = "https"

# 或加上過濾規則,如:
[entryPoints.http.redirect]
  regex = "^http://test2.example.com/(.*)"
  replacement = "https://test2.example.com/$1"