Overview and What You Will Learn
A Kubernetes Service gives a changing set of pods one stable virtual IP address and DNS name, and spreads traffic across the healthy ones. The type field decides who can reach it: ClusterIP is reachable only inside the cluster, NodePort opens a port on every node, LoadBalancer adds a cloud load balancer in front, and ExternalName is a DNS alias to something outside the cluster.
In this guide you will build each type, trace how traffic flows from a client to a pod, and learn a short checklist for debugging a Service that does not work.
By the end you will be able to:
- Choose the right Service type for internal, test, and public traffic
- Explain how ClusterIP, NodePort, and LoadBalancer build on each other
- Create each type with a working manifest and verify it with
kubectl - Use a headless Service for direct pod addressing
- Debug an empty or unreachable Service in a fixed order
Why This Matters in Production
Pods get a new IP address every time they are recreated, so no client should ever depend on a pod IP. A Service is the stable address clients use instead, and it keeps working through rollouts, node failures, and autoscaling.
The Service type is also a security decision. A wrong type can expose an internal API to the internet, and a wrong selector can quietly send traffic nowhere. Platform teams at companies like Swiggy or Shopify default every service to ClusterIP and expose only a small number of entry points on purpose.
Core Principles
The first three types are layers, each including the one before it. A LoadBalancer Service also allocates a NodePort and a ClusterIP, and a NodePort Service also has a ClusterIP. ExternalName is different: it has no selector and no proxying, it only returns a DNS alias.
The ClusterIP is virtual: no network interface owns it. On each node, kube-proxy programs rules that rewrite traffic sent to the Service IP so it lands on one of the backing pods. The pods behind a Service are tracked by EndpointSlices, which list only pods that are ready.
When to use each type:
- ClusterIP: the default, for all service-to-service traffic inside the cluster.
- NodePort: a fixed port on every node in the 30000-32767 range, for tests, on-premises clusters, or as the target of your own load balancer.
- LoadBalancer: a managed cloud load balancer with its own address, for public or private entry points.
- ExternalName: a DNS alias, so pods can use an in-cluster name for an external hostname.
- Headless (
clusterIP: None): no virtual IP, DNS returns the pod IPs directly, used for StatefulSets and client-side load balancing.
Detailed Step-by-Step Practical Lab
This lab uses a namespace named service-lab and a small nginx Deployment as the backend, so every command works on any cluster.
Step 1 - Create a Namespace and a Backend Deployment
A Service needs pods to route to. The Deployment labels its pods app: payments-api, which the Services below select. The readiness probe matters because only ready pods receive traffic.
apiVersion: v1kind: Namespacemetadata: name: service-labapiVersion: apps/v1kind: Deploymentmetadata: name: payments-api namespace: service-labspec: replicas: 2 selector: matchLabels: app: payments-api template: metadata: labels: app: payments-api spec: containers: - name: payments-api image: nginx:1.27 ports: - containerPort: 80 readinessProbe: httpGet: path: / port: 80kubectl apply -f backend.yamlkubectl get pods -n service-lab -o wideStep 2 - ClusterIP for Internal Traffic
ClusterIP is the default type, so type can be left out. Clients call the Service port, and traffic is forwarded to the container's targetPort.
# clusterip-service.yamlapiVersion: v1kind: Servicemetadata: name: payments-api namespace: service-labspec: type: ClusterIP selector: app: payments-api # routes to ready pods with this label ports: - name: http port: 80 # port clients call on the Service targetPort: 80 # port the container listens onkubectl apply -f clusterip-service.yamlkubectl get service payments-api -n service-lab# NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE# payments-api ClusterIP 10.96.45.200 <none> 80/TCP 10s # Call it by DNS name from another podkubectl run curl-test --image=curlimages/curl --restart=Never --rm -it -n service-lab -- \ curl -s -o /dev/null -w "%{http_code}\n" http://payments-api.service-lab.svc.cluster.local/# 200 # See the pods behind the Servicekubectl get endpointslices -n service-lab -l kubernetes.io/service-name=payments-apiInside the same namespace the short name payments-api also works. Across namespaces use payments-api.service-lab.
RememberThe older
kubectl get endpointscommand still works on many clusters, but the Endpoints API is deprecated in favor of EndpointSlices, so prefer the EndpointSlice form.
Step 3 - NodePort for Tests and On-Premises Clusters
NodePort opens the same port on every node and forwards to the Service. Any node IP works, even if the pod runs on a different node.
# nodeport-service.yamlapiVersion: v1kind: Servicemetadata: name: payments-api-nodeport namespace: service-labspec: type: NodePort selector: app: payments-api ports: - port: 80 targetPort: 80 nodePort: 30080 # optional; if omitted Kubernetes picks one in 30000-32767kubectl apply -f nodeport-service.yamlkubectl get nodes -o wide# NAME INTERNAL-IP EXTERNAL-IP# mumbai-worker-1 10.0.1.20 <none> # From inside the network (same VPC or VPN)curl http://10.0.1.20:30080/Whether you can reach it from the internet depends on the node having a public address and the firewall or security group allowing port 30080. A NodePort is open on every node, including nodes you did not expect, so restrict it at the network level.
SecurityA NodePort is reachable on all nodes by anyone who can route to them. Avoid it as a public entry point. Use a LoadBalancer or an Ingress or Gateway in front, and keep node ports closed to the internet.
Step 4 - LoadBalancer for External Access
A LoadBalancer Service asks the cloud provider for a load balancer and fills in its address when ready. On AWS this is a Network Load Balancer (or a Classic one with the older provider), not an Application Load Balancer. Layer 7 routing on AWS comes from an Ingress or Gateway.
# loadbalancer-service.yamlapiVersion: v1kind: Servicemetadata: name: api-gateway namespace: service-lab annotations: # With the AWS Load Balancer Controller installed (annotation names can vary by version) service.beta.kubernetes.io/aws-load-balancer-type: "external" service.beta.kubernetes.io/aws-load-balancer-nlb-target-type: "ip" service.beta.kubernetes.io/aws-load-balancer-scheme: "internet-facing"spec: type: LoadBalancer externalTrafficPolicy: Local # keep the client source IP, skip an extra hop loadBalancerSourceRanges: - 203.0.113.0/24 # only these client ranges may connect selector: app: payments-api ports: - name: http port: 80 targetPort: 80kubectl apply -f loadbalancer-service.yamlkubectl get service api-gateway -n service-lab --watch# NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S)# api-gateway LoadBalancer 10.96.22.100 k8s-abc123.elb.ap-south-1.amazonaws.com 80:31204/TCPThe EXTERNAL-IP shows <pending> until provisioning finishes, usually within a few minutes. On AWS it is a DNS hostname rather than an IP address, so point DNS at it with a CNAME or alias. The 80:31204/TCP part shows the NodePort that was allocated automatically.
TipEvery LoadBalancer Service creates its own cloud load balancer, which adds cost and management overhead quickly. For many HTTP services, run one Ingress controller or Gateway API implementation behind a single LoadBalancer and route to ClusterIP Services by host and path.
Step 5 - ExternalName to Alias an External Hostname
An ExternalName Service has no selector and no proxy. It makes cluster DNS return a CNAME to the hostname you give, so pods can use a stable in-cluster name for something outside, such as a managed database during a migration.
# externalname-service.yamlapiVersion: v1kind: Servicemetadata: name: postgres-db namespace: service-labspec: type: ExternalName externalName: mumbai-prod-db.c7xyz.ap-south-1.rds.amazonaws.comkubectl apply -f externalname-service.yamlkubectl run dns-test --image=busybox:1.36 --restart=Never --rm -it -n service-lab -- \ nslookup postgres-db.service-lab.svc.cluster.local# postgres-db.service-lab.svc.cluster.local canonical name = mumbai-prod-db.c7xyz...rds.amazonaws.comCommon MistakeExpecting ExternalName to proxy traffic or to work with an IP address. It only returns a DNS alias, and the value must be a hostname. TLS clients that verify the server name may also fail, because they connect using the alias but the certificate is issued for the real hostname.
Step 6 - Headless Service for Direct Pod Addressing
Setting clusterIP: None creates a headless Service. DNS returns the IP of every ready pod instead of one virtual IP, so clients can pick pods themselves. StatefulSets use this to give each pod a stable DNS name.
# headless-service.yamlapiVersion: v1kind: Servicemetadata: name: payments-api-headless namespace: service-labspec: clusterIP: None selector: app: payments-api ports: - name: http port: 80kubectl apply -f headless-service.yamlkubectl run dns-test --image=busybox:1.36 --restart=Never --rm -it -n service-lab -- \ nslookup payments-api-headless.service-lab.svc.cluster.local# Returns one A record per ready podStep 7 - Debug a Service That Does Not Work
Check the layers in order, from the Service to the pod. Most failures are found in the first two commands.
# 1. Does the Service select any pods? (an empty list means a label mismatch)kubectl get endpointslices -n service-lab -l kubernetes.io/service-name=payments-apikubectl get service payments-api -n service-lab -o jsonpath='{.spec.selector}'kubectl get pods -n service-lab --show-labels # 2. Are the pods ready? (unready pods are removed from the Service)kubectl get pods -n service-lab # 3. Is the targetPort the port the container really listens on?kubectl describe service payments-api -n service-lab # 4. Does DNS resolve?kubectl run dns-test --image=busybox:1.36 --restart=Never --rm -it -n service-lab -- \ nslookup payments-api # 5. Test the pod directly, bypassing the Servicekubectl port-forward pod/<pod-name> 8080:80 -n service-labIf the Service and pods look right but traffic is still blocked, check for a NetworkPolicy that denies the ingress path. Clean up the lab with:
kubectl delete namespace service-labProduction Best Practices & Common Pitfalls
- Default to ClusterIP. Expose a service outside the cluster only when it needs to be public, and do it deliberately.
- Name your ports. Names are required when a Service has more than one port, and they make manifests and probes easier to read. Set
appProtocolwhere tools such as service meshes use it to detect the protocol. - Restrict public load balancers. Use
loadBalancerSourceRanges, security groups, or private (internal) load balancers for anything that is not meant for the whole internet. - Use
externalTrafficPolicy: Localwhen you need client IPs. The trade-off is that nodes without a local pod fail the load balancer health check, which is the desired behavior. - Prefer a single entry point for HTTP. One Ingress or Gateway API implementation behind one LoadBalancer is cheaper and easier to secure than many LoadBalancers.
- Treat
sessionAffinity: ClientIPas a stopgap. It pins a client to a pod for a timeout, but source IPs can be hidden behind NAT. Store session state outside the pod when you can. - Always check endpoints after creating a Service. An empty EndpointSlice means no traffic reaches your pods.
Common MistakeA
selectorthat does not match the pod labels. The Service is created without error but has no endpoints, so connections fail or hang. Compare the Service selector tokubectl get pods --show-labelsfirst.
Quick Reference & Troubleshooting Commands
| Command | Purpose |
|---|---|
kubectl get services -n <ns> |
List Services and their types |
kubectl get endpointslices -l kubernetes.io/service-name=<svc> |
Show pods behind a Service |
kubectl describe service <svc> -n <ns> |
Selector, ports, and events |
kubectl get service <svc> -o jsonpath='{.spec.clusterIP}' |
Read the ClusterIP |
kubectl port-forward svc/<svc> 8080:80 |
Test a Service from your machine |
kubectl get pods --show-labels |
Compare pod labels to the selector |
| Symptom | Likely Cause | Fix |
|---|---|---|
| Empty EndpointSlice | Selector does not match pod labels | Fix the selector or labels |
| Connection refused | Wrong targetPort |
Set it to the container's real port |
| Pods exist but no traffic | Pods not ready | Fix the readiness probe or the app |
EXTERNAL-IP stays <pending> |
No cloud controller or quota | Check the cloud controller and events |
| NodePort unreachable | Firewall or security group | Open the port to the intended sources only |
| Name does not resolve | Wrong namespace or typo | Use <svc>.<namespace> from other namespaces |