0
0
GCPcloud~5 mins

GKE Ingress with Load Balancer in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you run apps on Google Kubernetes Engine, you often want to let users access them from the internet. GKE Ingress with Load Balancer helps by directing internet traffic to your app safely and efficiently.
When you want to expose a web app running in GKE to users on the internet.
When you need to balance traffic between multiple copies of your app for better speed and reliability.
When you want to use a single IP address to route traffic to different services inside your cluster.
When you want Google Cloud to automatically create and manage a load balancer for your app.
When you want to use HTTPS with automatic certificate management for your app.
Config File - ingress.yaml
ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    kubernetes.io/ingress.class: "gce"
spec:
  rules:
  - host: example-app.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: example-service
            port:
              number: 80

This file defines an Ingress resource for GKE.

apiVersion and kind specify this is an Ingress.

metadata includes the name and an annotation telling GKE to use the Google Cloud load balancer.

spec defines rules: traffic to example-app.com on any path goes to the Kubernetes service named example-service on port 80.

Commands
This command creates the Ingress resource in your GKE cluster. It tells GKE to set up a load balancer to route traffic to your app.
Terminal
kubectl apply -f ingress.yaml
Expected OutputExpected
ingress.networking.k8s.io/example-ingress created
This command checks the status of the Ingress and shows the external IP address assigned by Google Cloud load balancer.
Terminal
kubectl get ingress example-ingress
Expected OutputExpected
NAME CLASS HOSTS ADDRESS PORTS AGE example-ingress gce example-app.com 34.123.45.67 80 1m
This command shows detailed information about the Ingress, including backend services and events to help troubleshoot if needed.
Terminal
kubectl describe ingress example-ingress
Expected OutputExpected
Name: example-ingress Namespace: default Address: 34.123.45.67 Default backend: <none> Rules: Host Path Backends ---- ---- -------- example-app.com / example-service:80 Annotations: kubernetes.io/ingress.class: gce Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal CREATE 2m loadbalancer-controller Ingress example-ingress
Key Concept

If you remember nothing else from this pattern, remember: GKE Ingress creates a Google Cloud load balancer that routes internet traffic to your Kubernetes services.

Common Mistakes
Not adding the annotation 'kubernetes.io/ingress.class: "gce"' in the Ingress metadata.
Without this annotation, GKE does not know to create a Google Cloud load balancer for your Ingress.
Always include the annotation 'kubernetes.io/ingress.class: "gce"' in your Ingress metadata.
Using a service name or port that does not exist in the cluster.
The load balancer cannot route traffic if the backend service is missing or misconfigured, causing errors.
Verify your service exists and the port number matches before creating the Ingress.
Checking Ingress status immediately after creation without waiting for the external IP.
It takes some time for Google Cloud to provision the load balancer and assign an IP, so the ADDRESS field may be empty at first.
Wait a few minutes and run 'kubectl get ingress' again to see the external IP.
Summary
Create an Ingress YAML file that defines rules to route traffic from the internet to your Kubernetes service.
Apply the Ingress with 'kubectl apply -f ingress.yaml' to create the load balancer in Google Cloud.
Use 'kubectl get ingress' and 'kubectl describe ingress' to check the load balancer's external IP and details.