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 annotations specific to the Ingress controller you are using. For example, if you are using the NGINX Ingress controller, you can set various timeout values using annotations.

Here’s how you can create an Ingress resource in Kubernetes with the NGINX Ingress controller and set a timeout for the backend service.

Step-by-Step Explanation

  1. Import Pulumi and Kubernetes Packages: We start by importing the necessary Pulumi and Kubernetes packages.
  2. Create a Kubernetes Namespace: It’s a good practice to create a namespace for isolating resources.
  3. Define the Ingress Resource: We define the Ingress resource with annotations to set the timeout values.

Annotations for Timeouts

  • nginx.ingress.kubernetes.io/proxy-connect-timeout: Sets the timeout for establishing a connection to the backend service.
  • nginx.ingress.kubernetes.io/proxy-send-timeout: Sets the timeout for sending data to the backend service.
  • nginx.ingress.kubernetes.io/proxy-read-timeout: Sets the timeout for reading data from the backend service.

Here is a complete Pulumi program in TypeScript to set these timeouts:

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

// Create a namespace
const namespace = new k8s.core.v1.Namespace("example-namespace");

// Define the Ingress resource
const ingress = new k8s.networking.v1.Ingress("example-ingress", {
    metadata: {
        namespace: namespace.metadata.name,
        annotations: {
            "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: [{
            http: {
                paths: [{
                    path: "/",
                    pathType: "Prefix",
                    backend: {
                        service: {
                            name: "example-service",
                            port: {
                                number: 80,
                            },
                        },
                    },
                }],
            },
        }],
    },
}, { dependsOn: namespace });

// Export the Ingress URL
export const ingressUrl = ingress.status.loadBalancer.ingress[0].hostname;

Explanation of the Code

  • Namespace Creation: We create a namespace called example-namespace to isolate our Ingress resource.
  • Ingress Resource: The Ingress resource is defined with annotations to set the timeout values. The spec section defines the rules for routing traffic to the backend service named example-service on port 80.
  • Annotations: The annotations under metadata are specific to the NGINX Ingress controller and set the connection, send, and read timeouts to 10 seconds each.
  • Export: We export the Ingress URL so that we can easily access it after deployment.

Running the Program

To deploy this program, ensure you have the Pulumi CLI installed and configured with access to your Kubernetes cluster. Then, run the following commands:

  1. pulumi stack init (if you haven’t already created a stack)
  2. pulumi up

This will create the namespace, the Ingress resource with the specified timeouts, and output the Ingress URL.

You can adjust the timeout values as needed by modifying the annotations. This program assumes you have an NGINX Ingress controller running in your Kubernetes cluster.

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