1. Answers
  2. How to set a timeout on a Kubernetes Ingress?

How do I set a timeout on a Kubernetes Ingress?

To set a timeout on a Kubernetes Ingress, you typically need to configure the annotations for the Ingress resource. These annotations are specific to the ingress controller you are using. For example, if you are using the NGINX Ingress controller, you can set timeout values using annotations like nginx.ingress.kubernetes.io/proxy-connect-timeout, nginx.ingress.kubernetes.io/proxy-send-timeout, and nginx.ingress.kubernetes.io/proxy-read-timeout.

Here is a detailed explanation and a Pulumi program written in TypeScript to set these timeout annotations on a Kubernetes Ingress resource:

Explanation

  1. Import Dependencies: We import the necessary Pulumi and Kubernetes packages.
  2. Create a Kubernetes Namespace: We create a namespace for our resources.
  3. Create a Kubernetes Ingress Resource: We define an Ingress resource with annotations to set the timeout values.
  4. Annotations for Timeouts: We use annotations specific to the NGINX Ingress controller to set the connection timeout, send timeout, and read timeout.

Pulumi Program

import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

// Create a namespace for our resources
const namespace = new k8s.core.v1.Namespace("example-namespace", {
    metadata: {
        name: "example-namespace",
    },
});

// Create an Ingress resource with timeout annotations
const ingress = new k8s.networking.v1.Ingress("example-ingress", {
    metadata: {
        namespace: namespace.metadata.name,
        annotations: {
            // Set timeouts for the NGINX Ingress controller
            "nginx.ingress.kubernetes.io/proxy-connect-timeout": "10s",
            "nginx.ingress.kubernetes.io/proxy-send-timeout": "10s",
            "nginx.ingress.kubernetes.io/proxy-read-timeout": "10s",
        },
    },
    spec: {
        rules: [{
            host: "example.com",
            http: {
                paths: [{
                    path: "/",
                    pathType: "Prefix",
                    backend: {
                        service: {
                            name: "example-service",
                            port: {
                                number: 80,
                            },
                        },
                    },
                }],
            },
        }],
    },
});

export const ingressName = ingress.metadata.name;
export const ingressNamespace = ingress.metadata.namespace;

Explanation of the Code

  • Namespace: We create a namespace called example-namespace to organize our resources.
  • Ingress Resource: We define an Ingress resource named example-ingress.
    • Annotations: We add annotations to set the proxy connection, send, and read timeouts to 10s each. These annotations are specific to the NGINX Ingress controller.
    • Spec: We define the rules for the Ingress, specifying the host (example.com) and the path (/) that directs traffic to a backend service named example-service on port 80.

How to Use

  1. Install Pulumi CLI: Ensure you have the Pulumi CLI installed and configured.
  2. Set Up Kubernetes Provider: Ensure your Pulumi stack is configured with access to your Kubernetes cluster.
  3. Run the Program: Save the code to a file (e.g., index.ts) and run pulumi up to create the resources.

This program will create a Kubernetes Ingress resource with the specified timeout settings using the NGINX Ingress controller annotations. You can adjust the timeout values as needed for your specific use case.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up