liveBook · Manning

Service

As explained in Kubernetes for Developers

A Kubernetes Service is a stable networking abstraction that provides connectivity to a group of Pods.

A Deployment can run several copies of an application, but each Pod has its own internal IP address. A Service gives those Pods one stable access point. It can connect applications inside the cluster or expose an application to users outside it. section 3.2.5 section 1.2.1

For a typical application:

  • A Deployment manages the Pods; a Service selects the Pods that receive traffic using their labels. section 3.1.2 section 3.5
  • The Service balances requests across running Pods. Its address remains useful even when a Deployment has fewer available replicas or temporarily has extra ones during a rollout. section 3.2.5
  • Use a ClusterIP Service for communication within the cluster, or a LoadBalancer Service to make an application available externally. section 7.1.1 section 3.2.5
Listing 3.3 Chapter03/3.2_DeployingToKubernetes/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: timeserver
spec:
  selector:               #1
    pod: timeserver-pod   #1
  ports:
  - port: 80              #2
    targetPort: 80        #3
    protocol: TCP         #4
  type: LoadBalancer      #5

This Service YAML selects Pods by label and exposes port 80. The port field is the Service port; targetPort is the port receiving traffic on each Pod. Its LoadBalancer type is intended for external access. section 3.2.5

Publish and check an application

Create the Service with `kubectl create -f service.yaml`, then use `kubectl get service` to find its address. For a LoadBalancer Service with an external IP, send an HTTP request to that IP to check the application. section 3.2.5

An ordinary Service has a virtual IP, not an IP tied to one Pod or node. Kubernetes keeps the routing information current and directs a request to a Pod backing the Service. This lets an application call an internal Service without choosing a particular replica. section 7.1.1

A Service can also be the cutover point in a blue/green rollout: one Service alternates between Deployments running different application versions. The old version can remain available until the new one has been validated. section 4.2.3

How Kubernetes for Developers covers it

How deep it goes
Works through Service configuration and request routing, including selectors, ports, virtual IPs, and node networking.
Examples
Uses a Service YAML file, kubectl commands, an HTTP request, and an internal request routed to a Pod.
What it assumes
Introduces Pods and Deployments as the application building blocks before using a Service for networking.
What you can do after
Operate/configure it with YAML and kubectl to expose a Deployment or give cluster applications an internal endpoint.
How it connects
Connects Pods, Deployments, labels, load balancing, ClusterIP, LoadBalancer, and blue/green rollouts.

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 for Developers. The section links point to the parts of the book it draws on.