Skip to content

Kubernetes Gateway API Implementation Guide

This guide explains how to deploy an application using Kubernetes Deployment, Service, and Gateway API (GatewayClass, Gateway, HTTPRoute), and also how to clean up the resources.


1. Create Deployment

deployment.yaml

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx-app
  template:
    metadata:
      labels:
        app: nginx-app
    spec:
      containers:
        - name: nginx
          image: nginx:latest
          ports:
            - containerPort: 80

Apply:

kubectl apply -f deployment.yaml

Verify pods:

kubectl get pods

2. Create Service

service.yaml

yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  selector:
    app: nginx-app
  ports:
    - port: 8080
      targetPort: 80
  type: LoadBalancer

Apply:

kubectl apply -f service.yaml

Check:

kubectl get svc

3. Install Gateway API CRDs

kubectl apply -k "github.com/kubernetes-sigs/gateway-api/config/crd?ref=v0.8.0"

Verify CRDs:

kubectl get crds | grep gateway

4. Create GatewayClass

gateway-class.yaml

yaml
apiVersion: gateway.networking.k8s.io/v1beta1
kind: GatewayClass
metadata:
  name: nginx-gateway-class
spec:
  controllerName: example.net/nginx

Apply:

kubectl apply -f gateway-class.yaml

Check:

kubectl get gatewayclass

5. Create Gateway

gateway.yaml

yaml
apiVersion: gateway.networking.k8s.io/v1beta1
kind: Gateway
metadata:
  name: nginx-gateway
  namespace: default
spec:
  gatewayClassName: nginx-gateway-class
  listeners:
    - name: http
      protocol: HTTP
      port: 80
      allowedRoutes:
        kinds:
          - kind: HTTPRoute

Apply:

kubectl apply -f gateway.yaml

Check:

kubectl get gateway -n default

6. Create HTTPRoute

http-route.yaml

yaml
apiVersion: gateway.networking.k8s.io/v1beta1
kind: HTTPRoute
metadata:
  name: nginx-route
  namespace: default
spec:
  parentRefs:
    - name: nginx-gateway
  hostnames:
    - "nginx.cloudmateria.com"
  rules:
    - matches:
        - path:
            type: Prefix
            value: /
      backendRefs:
        - name: nginx-svc
          port: 8080

Apply:

kubectl apply -f http-route.yaml

Check:

kubectl get httproute -n default

7. Map Domain Locally (Windows)

Edit:

C:\Windows\System32\drivers\etc\hosts

Add:

127.0.0.1 nginx.cloudmateria.com

8. Test Setup

Using curl:

curl -H "Host: nginx.cloudmateria.com" http://localhost:80

Or open in browser:

http://nginx.cloudmateria.com

You should see the NGINX welcome page.


9. Clean Up / Destroy Resources

To remove all resources created for this setup:

bash
kubectl delete httproute nginx-route -n default
kubectl delete gateway nginx-gateway -n default
kubectl delete gatewayclass nginx-gateway-class
kubectl delete svc nginx-svc
kubectl delete deployment nginx-app
kubectl delete crd $(kubectl get crds | grep gateway | awk '{print $1}')  # optional: remove Gateway CRDs if no longer needed

Verify deletion:

kubectl get pods,svc,gateway,httproute,gatewayclass

All should be removed.


Deployment Completed Successfully