DEV Community

Cover image for Part-84: ๐Ÿš€Expose a Pod with Google Kubernetes Engine (GKE) LoadBalancer Service (Declarative Way)
Latchu@DevOps
Latchu@DevOps

Posted on

Part-84: ๐Ÿš€Expose a Pod with Google Kubernetes Engine (GKE) LoadBalancer Service (Declarative Way)

In this guide, weโ€™ll learn how to deploy a simple Pod in Google Kubernetes Engine (GKE) and expose it to the outside world using a LoadBalancer Service โ€” all in a declarative way (using YAML manifests).


Step 01: Understanding Kubernetes YAML Top-Level Objects

Every Kubernetes resource is defined in a YAML manifest.
The top-level objects youโ€™ll see in almost every YAML file are:

apiVersion:  # Defines the API version to use (e.g., v1, apps/v1)
kind:        # Type of Kubernetes resource (Pod, Service, Deployment, etc.)
metadata:    # Information about the resource (name, labels, namespace)
spec:        # Desired state (configuration details)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ Think of it like this:

  • apiVersion โ†’ which API group to use.
  • kind โ†’ what resource you want.
  • metadata โ†’ how Kubernetes identifies it.
  • spec โ†’ how it should behave.

Step 02: Create a Simple Pod Definition

Letโ€™s create a Pod that runs a simple Nginx-based application.

๐Ÿ“‚ Create a directory for manifests:

mkdir kube-manifests
cd kube-manifests
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“„ File: 01-pod-definition.yaml

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
    - name: myapp
      image: stacksimplify/kubenginx:1.0.0
      ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ Key points:

  • We label the Pod with app: myapp.
  • The container runs on port 80.

โœ… Apply it:

kubectl apply -f 01-pod-definition.yaml
kubectl get pods
Enter fullscreen mode Exit fullscreen mode

p1


Step 03: Create a LoadBalancer Service

Now that we have a Pod, letโ€™s expose it using a LoadBalancer Service.
This Service will provision a GCP external load balancer and route traffic to our Pod.

๐Ÿ“„ File: 02-pod-LoadBalancer-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: myapp-pod-loadbalancer-service
spec:
  type: LoadBalancer
  selector:
    app: myapp   # matches our Pod label
  ports:
    - name: http
      port: 80       # Service port (external entry point)
      targetPort: 80 # Container port inside Pod
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ Flow of traffic:

[Internet User] โ†’ [GCP LoadBalancer] โ†’ [K8s Service Port 80] โ†’ [Pod Container Port 80]

โœ… Apply it:

kubectl apply -f 02-pod-LoadBalancer-service.yaml
kubectl get svc
Enter fullscreen mode Exit fullscreen mode

p2

๐Ÿ”Ž Look for an External IP assigned to your Service.

โœ… Test it:

curl http://<Load-Balancer-External-IP>
Enter fullscreen mode Exit fullscreen mode

You should see the Nginx response page ๐ŸŽ‰

p3


Step 04: Clean Up

When done, delete the resources:

kubectl delete -f 01-pod-definition.yaml
kubectl delete -f 02-pod-LoadBalancer-service.yaml
Enter fullscreen mode Exit fullscreen mode

p4


๐ŸŽฏ Summary

  • Pods are the smallest deployable unit in Kubernetes.
  • Services provide a stable endpoint to access Pods.
  • A LoadBalancer Service in GKE creates a GCP Load Balancer and exposes your app to the internet.

This is the simplest way to expose a Pod to the outside world.


๐ŸŒŸ Thanks for reading! If this post added value, a like โค๏ธ, follow, or share would encourage me to keep creating more content.


โ€” Latchu | Senior DevOps & Cloud Engineer

โ˜๏ธ AWS | GCP | โ˜ธ๏ธ Kubernetes | ๐Ÿ” Security | โšก Automation
๐Ÿ“Œ Sharing hands-on guides, best practices & real-world cloud solutions

Top comments (0)