Using kubernetes gateway.networking.k8s.io with stable.nchanged.io
TypeScriptThe Kubernetes Gateway API (gateway.networking.k8s.io) is a set of resources that you can use to configure HTTP routing, load balancing, and more sophisticated network topologies in a Kubernetes cluster, effectively serving as an evolution of the
Ingress
resource. With Pulumi, you can create these resources declaratively using code which then translates to the actual Kubernetes resources upon deployment.To demonstrate the usage of the Kubernetes Gateway API with Pulumi, let's consider an example scenario where we setup a simple HTTP routing for a domain
stable.example.io
that forwards traffic to a Kubernetes service. We will define aGateway
resource representing a load balancer that routes traffic, and aHTTPRoute
resource describing how the traffic should be directed to services based on the hostnames and paths.Assuming you have a Kubernetes cluster and Pulumi installed and setup, let's create a Pulumi program in TypeScript:
-
Initialize Your Project: If you haven't already, you'll need to create a new Pulumi project. Use the Pulumi CLI to make a new directory,
cd
into it, and runpulumi new kubernetes-typescript
. -
Write the Code: We will use the
kubernetes
package provided by Pulumi. Below is the TypeScript code:
import * as kubernetes from "@pulumi/kubernetes"; // Create a GatewayClass resource. const gatewayClass = new kubernetes.apiextensions.CustomResource("gatewayClass", { apiVersion: "networking.x-k8s.io/v1alpha1", kind: "GatewayClass", metadata: { name: "example-gateway-class", }, spec: { controller: "example.com/gateway-controller" } }); // Create a Gateway resource, specifying which GatewayClass it belongs to. const gateway = new kubernetes.apiextensions.CustomResource("gateway", { apiVersion: "networking.x-k8s.io/v1alpha1", kind: "Gateway", metadata: { name: "example-gateway", }, spec: { gatewayClassName: gatewayClass.metadata.name, listeners: [ { protocol: "HTTP", port: 80, hostname: { value: "stable.example.io" } } ], } }); // Assuming you have a service named 'example-service' in namespace 'example-namespace' // Create a HTTPRoute resource that forwards traffic to the example-service. const httpRoute = new kubernetes.apiextensions.CustomResource("httpRoute", { apiVersion: "networking.x-k8s.io/v1alpha1", kind: "HTTPRoute", metadata: { name: "example-http-route", }, spec: { gateways: { allow: "All" }, hostnames: ["stable.example.io"], rules: [{ matches: [{ path: { type: "PathPrefix", value: "/" }}], forwardTo: [{ serviceName: "example-service", port: 80, weight: 1, }], }], } }); // Export the Gateway URIs for access export const gatewayUri = gateway.metadata.name.apply(name => `http://${name}.example.io`); export const httpRouteUri = httpRoute.metadata.name.apply(name => `http://${name}.example.io`);
- Understand the Code:
- We declare a
GatewayClass
which is a template for creatingGateway
instances that share common configuration and behavior. - Next, we declare a
Gateway
specifying the class it belongs to and the hostname(s) it will manage traffic for. - Then, we create an
HTTPRoute
resource that specifies how to handle traffic for our domain,stable.example.io
, including the service to forward traffic to (example-service
in this case). - Finally, we export the URIs of the
Gateway
andHTTPRoute
to be able to access them after deployment.
Please note that as of my knowledge cutoff in April 2023, the Kubernetes Gateway API is under active development and might have had changes in the API. Ensure that you are using the appropriate API version (
v1alpha1
in the example) andGateway
setup for your specific Kubernetes cluster version and cloud provider. Thecontroller
value inGatewayClass
needs to match the Gateway controller you have installed in your cluster.- Deploy the Code:
- Use the Pulumi CLI to deploy these resources to your Kubernetes cluster by running
pulumi up
. - Confirm the deployment if the preview looks good.
Remember to replace placeholders like
example-service
with the actual names of your Kubernetes services and to adjust any other parameters according to your setup and requirements.-