Kubernetes Gateway API Deployment with Multi Site Configurations


Prerequisite Resources
After We deployed some pods :
# Running Nginx without Manifest
kubectl run nginx --image=nginx --port=80
kubectl expose pod nginx --type=ClusterIP --port=80 --target-port=80 --name=nginx

# Running Elasticsearch from ELK Stack

kubectl describe svc nginx
kubectl describe svc elasticsearch -n elk-stack

Step 1: Install Gateway API Resources
First, we need to install the Gateway API CRDs (Custom Resource Definitions):
# Install Git
sudo apt install git

# Install Gateway API resources
kubectl kustomize "https://github.com/nginx/nginx-gateway-fabric/config/crd/gateway-api/standard?ref=v1.5.1" | kubectl apply -f -

# Verify installation
kubectl get crd | grep gateway

Step 2: Configure NGINX Gateway Fabric
Next, we'll install NGINX Gateway Fabric as our Gateway API controller:
# Deploy NGINX Gateway Fabric CRDs
kubectl apply -f https://raw.githubusercontent.com/nginx/nginx-gateway-fabric/v1.6.1/deploy/crds.yaml

# Deploy NGINX Gateway Fabric
kubectl apply -f https://raw.githubusercontent.com/nginx/nginx-gateway-fabric/v1.6.1/deploy/nodeport/deploy.yaml

# Verify the deployment
kubectl get pods -n nginx-gateway

# View the nginx-gateway service
kubectl get svc -n nginx-gateway nginx-gateway -o yaml

Step 3: Create GatewayClass and Gateway Resources
Now, let's create the GatewayClass and Gateway resources. Save the following YAML to a file named -> gateway-resources.yaml
---
# GatewayClass defines the controller that implements the Gateway API
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
  name: nginx
spec:
  controllerName: gateway.nginx.org/nginx-gateway-controller
---
# Gateway resource defines the listener and ports
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: nginx-gateway
  namespace: nginx-gateway
spec:
  gatewayClassName: nginx
  listeners:
  - name: http
    port: 80
    protocol: HTTP
    allowedRoutes:
      namespaces:
        from: All
If we want Gateway Affinity to specific node use this patch
# Add node affinity to the nginx-gateway deployment
kubectl patch deployment nginx-gateway -n nginx-gateway -p '
{
  "spec": {
    "template": {
      "spec": {
        "affinity": {
          "nodeAffinity": {
            "requiredDuringSchedulingIgnoredDuringExecution": {
              "nodeSelectorTerms": [
                {
                  "matchExpressions": [
                    {
                      "key": "kubernetes.io/hostname",
                      "operator": "In",
                      "values": ["data2"]
                    }
                  ]
                }
              ]
            }
          }
        }
      }
    }
  }
}'
Apply the configuration:
kubectl apply -f gateway-resources.yaml

# Verify resources
kubectl get gatewayclass
kubectl get gateway -n nginx-gateway

Step 4: Expose the Frontend Service
Now, let's create an HTTPRoute to route traffic to the nginx service. Save the following YAML to nginx-route.yaml:
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: nginx-route
  namespace: default
spec:
  parentRefs:
  - name: nginx-gateway
    namespace: nginx-gateway
  hostnames:
  - "nginx.local"
  - "nginx.kishoreweb.com"
  rules:
  - matches:
    - path:
        type: PathPrefix
        value: /
    backendRefs:
    - name: nginx
      port: 80
Now, let's create an HTTPRoute to route traffic to the elasticsearch service. Save the following YAML to elasticsearch-route.yaml:
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: elasticsearch-route
  namespace: elk-stack
spec:
  parentRefs:
  - name: nginx-gateway
    namespace: nginx-gateway
  hostnames:
  - "elastic.local"
  - "elastic.kishoreweb.com"
  rules:
  - matches:
    - path:
        type: PathPrefix
        value: /
    backendRefs:
    - name: elasticsearch
      port: 9200
Apply the configuration:
kubectl apply -f nginx-route.yaml
kubectl apply -f elasticsearch-route.yaml

# Verify the HTTPRoute
kubectl get httproute nginx-route
kubectl describe httproute nginx-route

kubectl get httproute elasticsearch-route
kubectl describe httproute elasticsearch-route

Kubernetes Worker Node Commands
kishore@control:~$ history
    1  sudo apt-get install open-vm-tools-desktop -y
    2  history
    3  kubelet --version
    4  ifconfig
    5  sudo apt install net-tools
    6  ifconfig
    7  sudo hostnamectl set-hostname control
    8  sudo nano /etc/hosts
    9  sudo rm /etc/machine-id
   10  sudo systemd-machine-id-setup
   11  hostnamectl | grep "Machine ID"
   12  sudo apt  install curl
   13  sudo swapoff -a
   14  sudo sed -i '/swap.img/s/^/#/' /etc/fstab
   15  cat <

Kubernetes Worker Node Commands