1. Answers
  2. Deploy Kubernetes Gateway and NATS JetStream

How do I deploy Kubernetes Gateway and NATS JetStream?

In this guide, we will deploy a Kubernetes Gateway using gateway.networking.k8s.io and set up a NATS JetStream instance using Pulumi. This will involve creating a Kubernetes Gateway resource, defining a GatewayClass, and deploying NATS JetStream.

Key Points

  • We will create a Kubernetes GatewayClass and a Gateway for routing.
  • We will deploy a NATS JetStream instance in the Kubernetes cluster.
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

// Define the NATS JetStream deployment
const natsDeployment = new k8s.apps.v1.Deployment("nats-deployment", {
    metadata: { name: "nats" },
    spec: {
        selector: { matchLabels: { app: "nats" } },
        replicas: 1,
        template: {
            metadata: { labels: { app: "nats" } },
            spec: {
                containers: [{
                    name: "nats",
                    image: "nats:latest",
                    ports: [{ containerPort: 4222 }, { containerPort: 8222 }],
                    args: ["-js"]
                }]
            }
        }
    }
});

// Define the NATS JetStream service
const natsService = new k8s.core.v1.Service("nats-service", {
    metadata: { name: "nats" },
    spec: {
        ports: [
            { port: 4222, targetPort: 4222 },
            { port: 8222, targetPort: 8222 }
        ],
        selector: { app: "nats" }
    }
});

// Create a GatewayClass
const gatewayClass = new k8s.apiextensions.CustomResource("my-gatewayclass", {
    apiVersion: "gateway.networking.k8s.io/v1alpha2",
    kind: "GatewayClass",
    metadata: { name: "my-gatewayclass" },
    spec: {
        controller: "example.com/gateway-controller"
    }
});

// Create a Gateway
const gateway = new k8s.apiextensions.CustomResource("my-gateway", {
    apiVersion: "gateway.networking.k8s.io/v1alpha2",
    kind: "Gateway",
    metadata: { name: "my-gateway" },
    spec: {
        gatewayClassName: "my-gatewayclass",
        listeners: [{
            protocol: "HTTP",
            port: 80,
            routes: {
                kind: "HTTPRoute",
                name: "my-route"
            }
        }]
    }
});

// Define an HTTPRoute for routing traffic to the NATS service
const httpRoute = new k8s.apiextensions.CustomResource("my-route", {
    apiVersion: "gateway.networking.k8s.io/v1alpha2",
    kind: "HTTPRoute",
    metadata: { name: "my-route" },
    spec: {
        rules: [{
            matches: [{ path: { type: "Prefix", value: "/" } }],
            forwardTo: [{ serviceName: "nats", port: 4222 }]
        }]
    }
});

Summary

In this guide, we deployed a Kubernetes Gateway and a NATS JetStream instance using Pulumi. We created a GatewayClass, a Gateway, and an HTTPRoute to route traffic to the NATS service. This setup allows for efficient traffic management and message streaming within a 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