Kubernetes LoadBalancer Example Using a Pod - neerajk555/Kubernetes GitHub Wiki

We will:

  1. Create a single NGINX Pod
  2. Expose it via a LoadBalancer service
  3. Access it using the external IP
  4. Clean up afterward

Pod YAML File — nginx-pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80

LoadBalancer Service YAML — nginx-lb-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-lb-service
spec:
  type: LoadBalancer
  selector:
    app: nginx
  ports:
  - port: 80
    targetPort: 80

Execution Steps

Step 1: Create the NGINX Pod

kubectl apply -f nginx-pod.yaml

Step 2: Expose the Pod via LoadBalancer Service

kubectl apply -f nginx-lb-service.yaml

Step 3: Check if LoadBalancer got an external IP

kubectl get svc nginx-lb-service

Example output (on cloud provider):

NAME               TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)        AGE
nginx-lb-service   LoadBalancer   10.0.220.186    35.194.81.103   80:31339/TCP   1m

Wait for a minute or two if EXTERNAL-IP is

Step 4: Access the app

Open browser and go to:

http://<EXTERNAL-IP> You should see the default NGINX welcome page.


Clean-up Steps

kubectl delete -f nginx-lb-service.yaml
kubectl delete -f nginx-pod.yaml

Minikube Note

If you're running this on Minikube or local cluster:

minikube tunnel

Then run:

kubectl get svc nginx-lb-service

Minikube will assign a reachable external IP for local testing.

⚠️ **GitHub.com Fallback** ⚠️