Kubernetes LoadBalancer Example Using a Pod - neerajk555/Kubernetes GitHub Wiki
We will:
- Create a single NGINX Pod
- Expose it via a LoadBalancer service
- Access it using the external IP
- 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
kubectl apply -f nginx-pod.yaml
kubectl apply -f nginx-lb-service.yaml
kubectl get svc nginx-lb-service
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
Open browser and go to:
http://<EXTERNAL-IP>
You should see the default NGINX welcome page.
kubectl delete -f nginx-lb-service.yaml
kubectl delete -f nginx-pod.yaml
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.