1. Answers
  2. Setting up path-based routing for services using IngressClassParams

How do I set up path-based routing for services using IngressClassParams?

In this example, we will set up path-based routing for services in a Kubernetes cluster using IngressClassParams. This allows us to route traffic to different services based on the URL path.

Key Points:

  • IngressClass: Defines the controller responsible for handling Ingress resources.
  • IngressClassParams: Provides additional parameters for the IngressClass.
  • Ingress: Manages external access to the services in a cluster, typically HTTP.

Below is the Pulumi program that sets up an Ingress with path-based routing.

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

// Define the IngressClass
const ingressClass = new k8s.networking.v1.IngressClass("example-ingress-class", {
    metadata: {
        name: "example-ingress-class",
    },
    spec: {
        controller: "example.com/ingress-controller",
        parameters: {
            apiGroup: "networking.k8s.io",
            kind: "IngressClassParams",
            name: "example-ingress-class-params",
        },
    },
});

// Define the IngressClassParams
const ingressClassParams = new k8s.apiextensions.CustomResource("example-ingress-class-params", {
    apiVersion: "networking.k8s.io/v1",
    kind: "IngressClassParams",
    metadata: {
        name: "example-ingress-class-params",
    },
    spec: {
        // Add any custom parameters here
    },
});

// Define the services
const service1 = new k8s.core.v1.Service("service1", {
    metadata: {
        name: "service1",
    },
    spec: {
        selector: { app: "app1" },
        ports: [{ port: 80, targetPort: 80 }],
    },
});

const service2 = new k8s.core.v1.Service("service2", {
    metadata: {
        name: "service2",
    },
    spec: {
        selector: { app: "app2" },
        ports: [{ port: 80, targetPort: 80 }],
    },
});

// Define the Ingress with path-based routing
const ingress = new k8s.networking.v1.Ingress("example-ingress", {
    metadata: {
        name: "example-ingress",
    },
    spec: {
        ingressClassName: ingressClass.metadata.name,
        rules: [
            {
                host: "example.com",
                http: {
                    paths: [
                        {
                            path: "/app1",
                            pathType: "Prefix",
                            backend: {
                                service: {
                                    name: service1.metadata.name,
                                    port: { number: 80 },
                                },
                            },
                        },
                        {
                            path: "/app2",
                            pathType: "Prefix",
                            backend: {
                                service: {
                                    name: service2.metadata.name,
                                    port: { number: 80 },
                                },
                            },
                        },
                    ],
                },
            },
        ],
    },
});

Summary:

  • We created an IngressClass to specify the controller.
  • We defined IngressClassParams to provide additional parameters for the IngressClass.
  • We created two services, service1 and service2.
  • We set up an Ingress resource with path-based routing rules to direct traffic to the appropriate service based on the URL path.

This configuration allows you to manage traffic routing efficiently within 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