Kong-ingress-controller Read-timeout超時(shí)時(shí)間設(shè)置

2023年4月12日18:30:35 發(fā)表評論 4,308 ℃

今天開發(fā)反饋有個(gè)項(xiàng)目導(dǎo)入接口60s就超時(shí)了,按照之前其他項(xiàng)目的經(jīng)驗(yàn),在騰訊云LB設(shè)置proxy_read_timeout和proxy_send_timeout就正常了,因?yàn)檫@兩個(gè)參數(shù)剛好默認(rèn)是60s,項(xiàng)目其他地方也沒有配置超時(shí)時(shí)間。

但是設(shè)置以后,開發(fā)測試還是60s超時(shí)。

懷疑是ingress方面返回的超時(shí),因?yàn)橹按蟛糠猪?xiàng)目使用的都是Traefik Ingress和Nginx Ingress,Traefik Ingress默認(rèn)是不限制超時(shí),Nginx Ingress的默認(rèn)的超時(shí)時(shí)間也比較長。

于是讓開發(fā)提供了接口請求的Headers信息,如下圖:

Kong-ingress-controller Read-timeout超時(shí)時(shí)間設(shè)置

發(fā)現(xiàn)果然是Kong Ingress返回的超時(shí),于是判斷應(yīng)該是kong ingress默認(rèn)超時(shí)是60s。

于是先在Kong官網(wǎng)找了一通,沒找到相關(guān)的配置,又百度谷歌一番搜索,解決方法到挺多。

有讓在ingress里面添加nginx.ingress.kubernetes.io/proxy-read-timeout注解的.

有讓在kong Deployment添加環(huán)境變量KONG_NGINX_PROXY_KEEPALIVE_TIMEOUT、KONG_NGINX_PROXY_PROXY_READ_TIMEOUT等。

可能因?yàn)榘姹驹?,找到的方法測試都無效。

無賴又回到官網(wǎng)全局搜索了timeout關(guān)鍵詞,找到了相關(guān)的注解(版本大于2.8):

konghq.com/connect-timeout

設(shè)置連接超時(shí),以毫秒為單位。例如,將此注釋設(shè)置為60000將指示代理最多等待 60 秒以完成與上游服務(wù)的初始 TCP 連接。

konghq.com/read-timeout

設(shè)置讀取超時(shí),以毫秒為單位。例如,將此注釋設(shè)置為60000將指示代理在發(fā)送請求后最多等待 60 秒,然后超時(shí)并向客戶端返回 504 響應(yīng)。

konghq.com/write-timeout

設(shè)置寫入超時(shí),以毫秒為單位。例如,將此注釋設(shè)置為60000將指示代理在關(guān)閉保持活動(dòng)連接之前最多等待 60 秒而不寫入數(shù)據(jù)。

konghq.com/retries

設(shè)置請求的最大重試次數(shù)。例如,將此注解設(shè)置為,3 如果遇到失?。ɡ绯瑫r(shí)),最多會(huì)重新發(fā)送請求 3 次。

于是馬上在自己寫的測試服務(wù)的ingress添加注釋測試:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: atang-test-ingress
  namespace: default
  annotations:
    konghq.com/connect-timeout: '1000'
    konghq.com/write-timeout: '2000'
    konghq.com/read-timeout: '2000'
    konghq.com/retries: '0'
spec:
  ingressClassName: kong
  rules:
  - host: test.amd5.cn
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: atang-test-svc
            port:
              number: 80

奇跡沒有出現(xiàn),依然60s超時(shí)。

然后又是一番搜索,在Kong的crd文件里面找到timeout的一段描述:

Kong-ingress-controller Read-timeout超時(shí)時(shí)間設(shè)置

The timeout in milliseconds between two successive read operations for transmitting a request to the upstream server. Deprecated: use Service''s "konghq.com/read-timeout" annotation instead.

翻譯過來就是read_timeout這個(gè)參數(shù)已經(jīng)啟用,需要使用Service的konghq.com/read-timeout注釋。

突然反應(yīng)過來注釋應(yīng)該添加在service里面,不是ingress里面,然后馬上添加測試:

apiVersion: v1
kind: Service
metadata:
  name: atang-test-svc
  namespace: default
  annotations:
    konghq.com/connect-timeout: '1000'
    konghq.com/write-timeout: '2000'
    konghq.com/read-timeout: '2000'
    konghq.com/retries: '0'
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: atang-test
  sessionAffinity: None

奇跡終于出現(xiàn)了,2s就返回了超時(shí)。

Kong-ingress-controller Read-timeout超時(shí)時(shí)間設(shè)置

【騰訊云】云服務(wù)器、云數(shù)據(jù)庫、COS、CDN、短信等云產(chǎn)品特惠熱賣中

發(fā)表評論

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: