How do I implement path-based routing with Traefik IngressRoute in Kubernetes?
In this guide, we will learn how to set up path-based routing in a Kubernetes cluster using Traefik IngressRoute. Traefik is a popular open-source reverse proxy and load balancer that makes deploying microservices easy. We will define IngressRoute resources to route traffic based on the URL path to different backend services.
The following Pulumi program in TypeScript will:
- Deploy Traefik as the Ingress controller.
- Define two backend services.
- Create IngressRoute resources to route traffic based on URL paths.
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";
// Deploy Traefik as the Ingress controller
const traefik = new k8s.helm.v3.Chart("traefik", {
chart: "traefik",
version: "10.3.0",
fetchOpts: {
repo: "https://helm.traefik.io/traefik",
},
});
// Define the first backend service
const app1Labels = { app: "app1" };
const app1Deployment = new k8s.apps.v1.Deployment("app1-deployment", {
spec: {
selector: { matchLabels: app1Labels },
replicas: 1,
template: {
metadata: { labels: app1Labels },
spec: {
containers: [{
name: "app1",
image: "nginx",
ports: [{ containerPort: 80 }],
}],
},
},
},
});
const app1Service = new k8s.core.v1.Service("app1-service", {
spec: {
selector: app1Labels,
ports: [{ port: 80, targetPort: 80 }],
},
});
// Define the second backend service
const app2Labels = { app: "app2" };
const app2Deployment = new k8s.apps.v1.Deployment("app2-deployment", {
spec: {
selector: { matchLabels: app2Labels },
replicas: 1,
template: {
metadata: { labels: app2Labels },
spec: {
containers: [{
name: "app2",
image: "nginx",
ports: [{ containerPort: 80 }],
}],
},
},
},
});
const app2Service = new k8s.core.v1.Service("app2-service", {
spec: {
selector: app2Labels,
ports: [{ port: 80, targetPort: 80 }],
},
});
// Create IngressRoute for path-based routing
const ingressRoute = new k8s.apiextensions.CustomResource("ingressroute", {
apiVersion: "traefik.containo.us/v1alpha1",
kind: "IngressRoute",
metadata: {
name: "ingressroute",
},
spec: {
entryPoints: ["web"],
routes: [
{
match: "PathPrefix(`/app1`)",
kind: "Rule",
services: [
{
name: app1Service.metadata.name,
port: 80,
},
],
},
{
match: "PathPrefix(`/app2`)",
kind: "Rule",
services: [
{
name: app2Service.metadata.name,
port: 80,
},
],
},
],
},
});
Key Points
- Traefik Deployment: We use the Helm chart to deploy Traefik as the Ingress controller.
- Backend Services: Two backend services are defined using Kubernetes
Deployment
andService
resources. - IngressRoute: The
IngressRoute
resource is used to define path-based routing rules that direct traffic to the appropriate backend service based on the URL path.
Summary
In this guide, we set up path-based routing in a Kubernetes cluster using Traefik IngressRoute. We deployed Traefik as the Ingress controller, defined two backend services, and created IngressRoute resources to route traffic based on URL paths to the respective services. This setup allows for fine-grained control over how traffic is distributed and served 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 upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.