liveBook · Manning

Service

As explained in Kubernetes in Action, Second Edition

A Service is a Kubernetes resource that provides a stable network endpoint for a set of application pods or manually specified destinations.

A Kubernetes Service gives clients one stable access point for an application, even when the pods running it change. Each pod has its own IP address, but pods can be replaced or scaled to multiple replicas. Clients connect to the Service’s IP address and port; the Service forwards each connection to an endpoint behind it. section 11.1 section 11.1.1

Give an application a stable address

Suppose application pods need to call several quote pods. Create a Service named quote with a selector matching those pods. The application can then call http://quote rather than track individual pod IP addresses. Repeated connections may reach different quote pods. section 11.1.1 section 11.1.3

Listing 11.1 YAML manifest for the quote Service object
apiVersion: v1    #1
kind: Service    #1
metadata:
  name: quote     #2
spec:
  type: ClusterIP      #3
  selector:          #4
    app: quote       #4
  ports:                 #5
  - name: http           #5
    port: 80             #5
    targetPort: 80       #5
    protocol: TCP        #5

This minimal YAML manifest defines a ClusterIP Service. Its selector chooses pods labeled app=quote; port 80 on the Service forwards to port 80 on a selected pod. section 11.1.2

A Service is a Kubernetes API object, separate from the Deployment that sets the desired number of pod replicas. Normally, a label selector determines its endpoints, which Kubernetes records in Endpoints and EndpointSlice objects. Readiness controls when matching pods can receive traffic. Without a selector, you can supply an Endpoints object yourself—for example, to give external destinations a name inside the cluster. section 3.3.4 section 11.3.3 section 11.7

Choose a Service type according to who needs to connect:

  • ClusterIP is the default and is reachable within the cluster. Pods can find it through cluster DNS or service environment variables. section 11.1.2 section 11.1.3
  • NodePort also opens a port on every cluster node. Connections to any node’s port can reach a pod backing the Service. section 11.2.1
  • LoadBalancer requests an additional external load balancer where the cluster supports one. An external IP may remain pending on a local cluster; its node port may still be usable. section 3.3.2 section 3.4

For a quick first deployment, run kubectl expose deployment kiada --type=LoadBalancer --port 8080, then kubectl get svc to inspect its type, addresses and ports. For a repeatable configuration, apply a YAML manifest instead. To test a ClusterIP Service, connect from a pod with kubectl exec and curl: it is not directly reachable from your machine. Be careful with kubectl port-forward svc/name—it finds a backing pod but bypasses the Service itself. section 3.3.2 section 11.1.2 section 11.1.3

How Kubernetes in Action, Second Edition covers it

How deep it goes
Works through creation, selectors, ports, discovery, exposure types, endpoints and readiness, without detailing the network implementation.
Examples
Uses quote and quiz application Services, a ClusterIP YAML manifest, kubectl commands and repeated curl requests.
What it assumes
Starts without requiring prior Kubernetes, container, Docker or Linux experience, then builds on pods and deployments.
What you can do after
Operate/configure it with kubectl and YAML to expose pods, inspect access addresses and choose internal or external access.
How it connects
Links Services to Deployments, Pods, label selectors, Endpoints, EndpointSlices, DNS and readiness probes.

Also explained in

Compare how 12 books explain service →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes in Action, Second Edition. The section links point to the parts of the book it draws on.