Skip to content

How to set kong timeout in 5 Mins

Kong API Gateway is using its default timeout of 1 minute. it’s not long enough sometimes. We want to set the customized timeout value for upstream services. we are using Kong to replace the default Nginx controller in our Kubernetes environment.

Background: CentOS 7.9 x64; Our Kubernetes version: 1.17; Kong version 2.3.3

Create a Kong Ingress

Kong doesn’t support using env to increase the timeout, so we need to create a KongIngress resource first.

The default timeout is 60000 ms. In this demo, I will set up 3000001, which is around 3000s.

apiVersion: configuration.konghq.com/v1
kind: KongIngress
metadata:
  annotations:
    kubernetes.io/ingress.class: "kong"
  name: kong-timeout-conf
proxy:
  connect_timeout: 3000001
  protocol: http
  read_timeout: 3000001
  retries: 10
  write_timeout: 3000001

Update upstream services

We need to add below in the upstream service.

annotations: 
  konghq.com/override: $YOUR_KONGINGRESS_NAME

The completed YAML

kind: Service
apiVersion: v1
metadata:
  annotations:
    konghq.com/override: kong-timeout-conf
  name: report-server
  labels:
    deployment: report-server
spec:
  selector:
    app: report-server
  type: NodePort
  ports:
    - name: report-port
      port: 9527
      targetPort: 9527
    - name: debug-port
      port: 9528
      targetPort: 9528
      nodePort: 39528

Test and verify

Use curl to get Kong services detailed information.

curl -k https://127.0.0.1:8444/services

The Kong output showed the timeout value of the report-server has increased to 3000001 ms, which is around 3000s.

        {
            "host": "report-server.gosysops-prod.9527.svc",
            "id": "093f8d1e-b3b8-5c22-942c-339a22b215f2",
            "protocol": "http",
            "read_timeout": 3000001,
            "tls_verify_depth": null,
            "port": 80,
            "updated_at": 1632379287,
            "ca_certificates": null,
            "created_at": 1632379287,
            "connect_timeout": 3000001,
            "write_timeout": 3000001,
            "name": "gosysops.report-server.9527",
            "retries": 10,
            "path": "/",
            "tls_verify": null,
            "client_certificate": null,
            "tags": null
        },

Attention

Some people might want to add Nginx annotations, like(nginx.ingress.kubernetes.io/proxy‑connect‑timeout: 900) in the ingress file. but this won’t be working.

These values are for Nginx Ingress Controller and Kong doesn’t support those annotations.

Conclusion

You only need two steps to increase/decrease timeout for Kong

  1. Create KongIngress CRD resource
  2. add kongIngress annotation in the upsream services.