Skip to main content

Understanding Kubernetes Service Types

Learn the four Kubernetes Service types, ClusterIP, NodePort, LoadBalancer, and ExternalName, with YAML examples and commands to debug connectivity.

52 Terms

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.

flowchart TD A(["Client"]):::slate B["Cloud load balancer"]:::orange C["NodePort on every node"]:::blue D["ClusterIP virtual IP"]:::purple E["Pods with matching labels"]:::green A --> B --> C --> D --> E classDef slate fill:#475569,color:#ffffff classDef orange fill:#F97316,color:#ffffff classDef blue fill:#326CE5,color:#ffffff classDef purple fill:#8B5CF6,color:#ffffff classDef green fill:#10B981,color:#ffffff

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:

  1. ClusterIP: the default, for all service-to-service traffic inside the cluster.
  2. 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.
  3. LoadBalancer: a managed cloud load balancer with its own address, for public or private entry points.
  4. ExternalName: a DNS alias, so pods can use an in-cluster name for an external hostname.
  5. 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.

YAML
apiVersion: v1
kind: Namespace
metadata:
name: service-lab
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: payments-api
namespace: service-lab
spec:
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: 80
Bash
kubectl apply -f backend.yaml
kubectl get pods -n service-lab -o wide
Step 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.

YAML
# clusterip-service.yaml
apiVersion: v1
kind: Service
metadata:
name: payments-api
namespace: service-lab
spec:
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 on
Bash
kubectl apply -f clusterip-service.yaml
kubectl 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 pod
kubectl 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 Service
kubectl get endpointslices -n service-lab -l kubernetes.io/service-name=payments-api

Inside the same namespace the short name payments-api also works. Across namespaces use payments-api.service-lab.

Remember

The older kubectl get endpoints command 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.

YAML
# nodeport-service.yaml
apiVersion: v1
kind: Service
metadata:
name: payments-api-nodeport
namespace: service-lab
spec:
type: NodePort
selector:
app: payments-api
ports:
- port: 80
targetPort: 80
nodePort: 30080 # optional; if omitted Kubernetes picks one in 30000-32767
Bash
kubectl apply -f nodeport-service.yaml
kubectl 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.

Security

A 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.

YAML
# loadbalancer-service.yaml
apiVersion: v1
kind: Service
metadata:
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: 80
Bash
kubectl apply -f loadbalancer-service.yaml
kubectl 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/TCP

The 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.

Tip

Every 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.

flowchart LR P["Pod"]:::blue S["Service postgres-db"]:::purple D["Cluster DNS CNAME"]:::orange R[("RDS endpoint")]:::green P --> S --> D --> R classDef blue fill:#326CE5,color:#ffffff classDef purple fill:#8B5CF6,color:#ffffff classDef orange fill:#F97316,color:#ffffff classDef green fill:#10B981,color:#ffffff
YAML
# externalname-service.yaml
apiVersion: v1
kind: Service
metadata:
name: postgres-db
namespace: service-lab
spec:
type: ExternalName
externalName: mumbai-prod-db.c7xyz.ap-south-1.rds.amazonaws.com
Bash
kubectl apply -f externalname-service.yaml
kubectl 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.com
Common Mistake

Expecting 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.

YAML
# headless-service.yaml
apiVersion: v1
kind: Service
metadata:
name: payments-api-headless
namespace: service-lab
spec:
clusterIP: None
selector:
app: payments-api
ports:
- name: http
port: 80
Bash
kubectl apply -f headless-service.yaml
kubectl 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 pod
Step 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.

Bash
# 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-api
kubectl 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 Service
kubectl port-forward pod/<pod-name> 8080:80 -n service-lab

If 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:

Bash
kubectl delete namespace service-lab

Production 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 appProtocol where 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: Local when 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: ClientIP as 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 Mistake

A selector that 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 to kubectl get pods --show-labels first.

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

Resources

Velero vs etcd Snapshot: Which Do You Need?

Velero vs etcd Snapshot: Which Do You Need?

Velero and etcd snapshots protect different layers of a Kubernetes cluster. See what each backs up, what each misses, and why most DR needs both.

11 min read•Aug 2026
Istio Ambient vs Linkerd in 2026: Which Mesh?

Istio Ambient vs Linkerd in 2026: Which Mesh?

Istio Ambient cuts the sidecar tax at L4, but L7 adds waypoint hops, and Linkerd stable builds now come from Buoyant. Choose with tests.

11 min read•Aug 2026
Nginx Ingress vs Traefik vs Gateway API in 2026

Nginx Ingress vs Traefik vs Gateway API in 2026

Ingress-NGINX is retired. Compare Traefik and Gateway API controllers as replacements, plus a safe migration plan that avoids outages.

10 min read•Aug 2026
OPA Gatekeeper vs Kyverno: Policy Engine in 2026

OPA Gatekeeper vs Kyverno: Policy Engine in 2026

OPA Gatekeeper vs Kyverno compared for 2026: Rego vs YAML, mutation, reporting, and operations, plus how to pick a Kubernetes policy engine.

10 min read•Aug 2026
Helm vs Kustomize in 2026: Templating vs Patching

Helm vs Kustomize in 2026: Templating vs Patching

Helm vs Kustomize compared for 2026 - templating vs patching, Helm 4's new features, and why most production teams end up running both.

5 min read•Aug 2026
Prometheus vs Datadog vs New Relic: Real Costs

Prometheus vs Datadog vs New Relic: Real Costs

Prometheus, Datadog, and New Relic compared for 2026 - real pricing at scale, hidden cost drivers, and which fits a Kubernetes-heavy stack.

5 min read•Aug 2026
Cluster Autoscaler vs Karpenter for EKS in 2026

Cluster Autoscaler vs Karpenter for EKS in 2026

Cluster Autoscaler vs Karpenter on EKS: scale-up speed, bin-packing, Spot handling, Auto Mode limits and a safe migration path, with v1 NodePool YAML.

11 min read•Aug 2026
GKE vs EKS vs AKS in 2026: Which Fits Your Team?

GKE vs EKS vs AKS in 2026: Which Fits Your Team?

GKE, EKS, and AKS compared for 2026 - control plane pricing, Autopilot vs Karpenter vs Node Auto Provisioning, and which platform actually fits your team.

5 min read•Aug 2026
K3s vs K8s vs MicroK8s: Which Distro in 2026?

K3s vs K8s vs MicroK8s: Which Distro in 2026?

Compare K3s, upstream Kubernetes and MicroK8s on footprint, datastores, HA and updates, and pick the right fit for edge, dev or production.

10 min read•Aug 2026
Canary Deployments with Argo Rollouts & Flagger

Canary Deployments with Argo Rollouts & Flagger

Ship to 5% of users first and auto-rollback on bad metrics: canary deployments on Kubernetes with Argo Rollouts, Flagger, and Gateway API.

11 min read•Jun 2026
OpenTelemetry Explained: Metrics, Logs, Traces

OpenTelemetry Explained: Metrics, Logs, Traces

OpenTelemetry in practice: instrument a Node.js service, run a production Collector, sample traces and link metrics, logs and traces in Grafana.

11 min read•Jun 2026
Kubernetes Cost Optimization Without SLO Risk

Kubernetes Cost Optimization Without SLO Risk

Kubernetes clusters average about 8% CPU use. Cut waste safely with request rightsizing, autoscaling, spot, and OpenCost, keeping SLOs.

11 min read•Jun 2026
ArgoCD vs FluxCD: GitOps for Kubernetes in 2026

ArgoCD vs FluxCD: GitOps for Kubernetes in 2026

ArgoCD vs FluxCD compared for 2026: UI, multi-tenancy, Helm, image automation and pruning risks, with a clear guide to picking your GitOps engine.

10 min read•Jun 2026

Explore More in Kubernetes Networking and Traffic Management

All 6 Topics

Frequently Asked Questions

Is Understanding Kubernetes Service Types free to learn on DevOps Network?

Yes - this topic, like everything on DevOps Network, is 100% free with no paywall or sign-up gate.

What does the Understanding Kubernetes Service Types topic cover?

Learn the four Kubernetes Service types, ClusterIP, NodePort, LoadBalancer, and ExternalName, with YAML examples and commands to debug connectivity.

Found a mistake, an outdated step or a broken link?