1. Answers
  2. Building a Kubernetes Ingress with Pulumi

How do I build a Kubernetes Ingress with Pulumi?

In this guide, we will create a Kubernetes Ingress resource using Pulumi. Ingress resources in Kubernetes manage external access to services within a cluster, typically HTTP.

We will define an Ingress resource that routes traffic to two different services based on the request path.

Key Points:

  • We will use the @pulumi/kubernetes package to define our Kubernetes resources.
  • An Ingress resource will be created to route traffic to two different backend services.
  • We will define the necessary services and deployments to ensure the Ingress has endpoints to route traffic to.

Here is the complete Pulumi program in TypeScript:

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

// Define a namespace
const namespace = new k8s.core.v1.Namespace("my-namespace", {
    metadata: { name: "my-namespace" }
});

// Define a deployment for the first service
const app1Labels = { app: "app1" };
const app1Deployment = new k8s.apps.v1.Deployment("app1-deployment", {
    metadata: { namespace: namespace.metadata.name },
    spec: {
        selector: { matchLabels: app1Labels },
        replicas: 1,
        template: {
            metadata: { labels: app1Labels },
            spec: {
                containers: [{
                    name: "app1",
                    image: "nginx",
                    ports: [{ containerPort: 80 }]
                }]
            }
        }
    }
});

// Define a service for the first deployment
const app1Service = new k8s.core.v1.Service("app1-service", {
    metadata: { namespace: namespace.metadata.name },
    spec: {
        selector: app1Labels,
        ports: [{ port: 80, targetPort: 80 }]
    }
});

// Define a deployment for the second service
const app2Labels = { app: "app2" };
const app2Deployment = new k8s.apps.v1.Deployment("app2-deployment", {
    metadata: { namespace: namespace.metadata.name },
    spec: {
        selector: { matchLabels: app2Labels },
        replicas: 1,
        template: {
            metadata: { labels: app2Labels },
            spec: {
                containers: [{
                    name: "app2",
                    image: "nginx",
                    ports: [{ containerPort: 80 }]
                }]
            }
        }
    }
});

// Define a service for the second deployment
const app2Service = new k8s.core.v1.Service("app2-service", {
    metadata: { namespace: namespace.metadata.name },
    spec: {
        selector: app2Labels,
        ports: [{ port: 80, targetPort: 80 }]
    }
});

// Define the Ingress resource
const ingress = new k8s.networking.v1.Ingress("my-ingress", {
    metadata: {
        namespace: namespace.metadata.name,
        annotations: { "nginx.ingress.kubernetes.io/rewrite-target": "/" }
    },
    spec: {
        rules: [{
            http: {
                paths: [
                    {
                        path: "/app1",
                        pathType: "Prefix",
                        backend: {
                            service: {
                                name: app1Service.metadata.name,
                                port: { number: 80 }
                            }
                        }
                    },
                    {
                        path: "/app2",
                        pathType: "Prefix",
                        backend: {
                            service: {
                                name: app2Service.metadata.name,
                                port: { number: 80 }
                            }
                        }
                    }
                ]
            }
        }]
    }
});

Summary:

In this guide, we created a Kubernetes Ingress resource using Pulumi. We defined two deployments and services, and then used an Ingress resource to route traffic based on the request path to the appropriate service. This setup allows external HTTP traffic to be directed to different services within the 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