Beginner’s Guide to Kubernetes Services: ClusterIP, NodePort, LoadBalancer
Kubernetes is a tool to run and manage apps in containers.
To connect apps inside and outside the cluster, Kubernetes uses ClusterIP services.
- ClusterIP –> for inside the cluster only
- NodePort –> for access from outside using a port
- LoadBalancer –> for public internet access
Services help apps talk to each other, even if the app restarts or changes place.
They also help users connect to your app from outside.
Github link please click hear – GitHub
Why Are Kubernetes Services Important?
In Kubernetes, Pods are not permanent. They can stop, restart, or move to another place at any time. Because of this, their IP address keeps changing. So, it’s difficult to connect to them directly.
That’s why we use Services in Kubernetes. A service gives a fixed IP and name to your app, so other apps can easily find and talk to it—even if the Pod restarts or changes. It also helps to share traffic between multiple Pods, like load balancing.
Key Differences and Use Cases of Kubernetes Service Types
Feature | ClusterIP | NodePort | LoadBalancer |
---|---|---|---|
Accessibility | Internal only (within cluster) | External access via Node IP and a specific port | External access via a public IP (cloud load balancer) |
Use Case | Internal communication | Dev/test environments needing basic external access | Public-facing apps in production needing scalable access |
Load Balancing | Internal kube-proxy load balancing | No native LB; each node forwards traffic on a fixed port | External cloud LB distributes traffic to backend Kubernetes nodes |
IP Addresses | Single virtual IP assigned by Kubernetes | Uses each node’s IP and a static port (e.g., 30000–32767) | Uses a public IP provided by the cloud provider |
Cloud Required? | No | No | Yes (requires cloud-managed LoadBalancer support) |
Scalability | Scales with additional pods | Scales with more nodes | Scales with more nodes |
ALSO READ:
- Reusable Terraform Modules Made My AWS EC2 Setup Super Easy
- Step-by-Step Success: Build a 3-Tier Web Application with Docker on AWS
- Build a 2-Tier Flask MySQL Docker Project
01 – ClusterIP – Internal Only (Default)
- What it is: Gives an internal IP that other apps inside the cluster can use.
- Use it : the frontend app needs to talk to the backend or database inside the cluster.
- Access: Only from inside the cluster.
- Load Balancing: Yes, between pods.
- Drawback: Cannot access it from outside (like a browser or external users).
# Service Definition
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector: # labels from Pod
project: demo
component: backend
environment: dev
version: "1.0.0"
ports:
- protocol: TCP
port: 80 # Port on the service (ClusterIP)
targetPort: 80 # Port inside the pod (container)
type: ClusterIP
---
# Pod Definition
apiVersion: v1
kind: Pod
metadata:
name: demo-pod
labels:
project: demo
component: backend
environment: dev
version: "1.0.0"
spec:
containers:
- name: nginx
image: nginx
02 – NodePort – External Access via Node IP
- What it is: Opens a fixed port on every node in the cluster.
- Use it when you want to access the app from outside for testing or dev purposes.
- Access: Using Node’s IP and NodePort.
- Port Range: 30000–32767
- Load Balancing: Not automatic between nodes.
- Drawback: Not suitable for production apps, and reverse proxy is needed for complex routing.
kind: Service
apiVersion: v1
metadata:
name: nginx-nodePort
spec:
type: NodePort
selector: # labels are used as selectors
project: services
component: frontend
environment: dev
version: "1.0.0"
ports:
- protocol: TCP
port: 80 # service port
targetPort: 80
nodePort: 30007
---
kind: Pod
apiVersion: v1
metadata:
name: nginx-nodePort
labels:
project: services
component: frontend
environment: dev
version: "1.0.0"
spec:
containers:
- name: nginx
image: nginx
03 – LoadBalancer – Best for Production (Cloud Only)
- What it is: Creates a cloud load balancer (like in AWS, GCP, Azure).
- Use it when: You want public access to your app with proper load balancing.
- Access: Using public IP given by cloud provider.
- Load Balancing: Yes, across all pods and nodes.
- Drawback: Works only if you’re using a cloud platform that supports it.
kind: Service
apiVersion: v1
metadata:
name: nginx-LB
spec:
type: LoadBalancer
selector: # labels are used as selectors
project: services
component: frontend
environment: dev
version: "1.0.0"
ports:
- protocol: TCP
port: 80 # service port
targetPort: 80
nodePort: 30007
---
kind: Pod
apiVersion: v1
metadata:
name: nginx-LB
labels:
project: services
component: frontend
environment: dev
version: "1.0.0"
spec:
containers:
- name: nginx
image: nginx
To Run, Check & Delete Kubernetes Services Using kubectl
Create Service/Pod or Apply changes (if already created)
kubectl create -f filename.yaml
kubectl apply -f filename.yaml
Check all services
kubectl get svc
Check all pods
kubectl get pods
Check all Kubernetes resources
kubectl get all
Get detailed pod info
kubectl get pods -o wide
Delete using YAML file
kubectl delete -f filename.yaml
Delete a service
kubectl delete svc servicename
Delete a pod
kubectl delete pod podname
Tips
- Use
"ClusterIP
” for backend/frontend talking inside the cluster. - Use
"NodePort"
when you want to test from your browser or Postman. Us
e “LoadBalancer
” when your app is running in the cloud and needs public access.- Always add labels in Pods – they are like address tags for Services.
- Use
kubectl get
andkubectl describe
to see what’s happening in the cluster. - Don’t forget to clean up! Use
kubectl delete
to remove things after testing.
Thank You