Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 37 ยท Networking and Routes

Services and Endpoints in OpenShift

Learn how OpenShift Services give pods a stable address: selectors, ClusterIP, headless services, EndpointSlices, cluster DNS names and how to debug an empty service.

Pods come and go. Every time a pod is rescheduled it gets a new IP address, so nothing can safely talk to a pod directly. A Service solves this: it is a stable virtual IP and DNS name that forwards traffic to whichever pods currently match a label selector. Every other networking feature in OpenShift, including routes, builds on top of services.

OpenShift uses the standard Kubernetes Service object, so everything here works in plain Kubernetes too. What OpenShift adds is the SDN or OVN-Kubernetes network plugin underneath, and the router that exposes services outside the cluster (covered in the next lessons).

OpenShift is not available on this site. Commands and YAML below are read-only. The runnable examples are small Python stand-ins that model the ideas (selectors, DNS names); they do not talk to a cluster.

Anatomy of a Service

YAML
apiVersion: v1
kind: Service
metadata:
  name: shop-api
  namespace: shop
spec:
  selector:
    app: shop-api        # pods with this label receive traffic
  ports:
    - name: http
      port: 80           # port of the service
      targetPort: 8080   # port inside the pod
      protocol: TCP

Create the same thing quickly from an existing deployment:

Terminal
oc expose deployment/shop-api --port=80 --target-port=8080
oc get svc shop-api
oc describe svc shop-api

The service gets a ClusterIP, an address that is only reachable inside the cluster. The important part is selector: the control plane watches pods, and every ready pod whose labels contain app: shop-api is added to the service's endpoints.

Service types

Type What it does Typical use
ClusterIP (default) Virtual IP inside the cluster Service to service traffic, backend for routes
NodePort Opens a port (30000-32767) on every node Rare; mostly testing
LoadBalancer Asks the cloud provider for an external load balancer Non-HTTP traffic such as TCP/UDP
ExternalName DNS alias (CNAME) to an outside hostname Pointing at a managed database
Headless (clusterIP: None) No virtual IP; DNS returns pod IPs StatefulSets, client-side balancing

For ordinary web apps you almost always use ClusterIP plus a route. On a bare-metal or cloud cluster, LoadBalancer services work only if the platform provides an implementation.

Endpoints and EndpointSlices

A service has no knowledge of pods. A controller creates EndpointSlice objects listing the IPs and ports of ready pods, and the cluster's proxy rules use them. When the list is empty, requests fail even though the service exists.

Terminal
oc get endpointslices -l kubernetes.io/service-name=shop-api
oc get endpoints shop-api

Only pods that pass their readiness probe are listed. A pod that is running but not ready receives no traffic, which is exactly why readiness probes matter during rollouts.

Service DNS names

Cluster DNS gives each service a name:

TEXT
<service>.<namespace>.svc.cluster.local

Inside the same project you can use the short name shop-api. From another project use shop-api.shop or the full form. Headless services additionally return one record per pod, and StatefulSet pods get stable names such as db-0.db.shop.svc.

The stand-in below builds those names and shows what DNS search domains do for short names.

Python Stand-in: service DNS names

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The resolver appends each search domain in order until one answers. A name like shop-api.shop first fails under billing.svc.cluster.local and then succeeds under svc.cluster.local. Using the explicit <service>.<namespace> form from other projects is the safe habit.

Selecting pods: a model

The selector is a subset match: a pod matches when every key/value in the selector appears in its labels. This tiny model makes it concrete and shows why a typo produces an empty service.

Python Stand-in: which pods does a service select?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Debugging a service with no traffic

Work from the service downward:

  1. oc get svc shop-api -o yaml and read spec.selector.
  2. oc get pods --show-labels and confirm the labels match exactly.
  3. oc get endpoints shop-api. Empty means no ready pod matches.
  4. oc describe pod <name> and look at readiness probe failures.
  5. Check targetPort: it must be the port the container really listens on.
  6. Test from inside: oc rsh deploy/shop-api curl -s localhost:8080/healthz, then from a second pod curl http://shop-api/.
flowchart LR
  C["Client pod"] -->|"shop-api.shop.svc"| S["Service (ClusterIP)"]
  S --> E["EndpointSlice"]
  E --> P1["Pod A (ready)"]
  E --> P2["Pod B (ready)"]
  E -. excluded .-> P3["Pod C (not ready)"]

A service forwards only to the ready pods listed in its EndpointSlice.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What makes a pod receive traffic from a ClusterIP service?

    Services use label selectors, and only pods that pass their readiness probe appear in the endpoints.
  2. 2. A service exists but oc get endpoints shows <none>. What is the most likely cause?

    An empty endpoint list means the selector matches no ready pod; compare the selector with oc get pods --show-labels.
  3. 3. Which DNS name reaches service db in project data from any project?

    The form is service.namespace.svc.cluster.local.
  4. 4. What does clusterIP: None create?

    Headless services skip the virtual IP and return the addresses of the individual pods.

Summary

  • A Service is a stable IP and DNS name in front of changing pods.
  • The selector picks pods; EndpointSlices list the ready ones.
  • ClusterIP is the default; routes use it as their backend.
  • Names follow <service>.<namespace>.svc.cluster.local.
  • Debug empty services by comparing selector, labels, readiness and targetPort.

Next: Routes in OpenShift