Chapter 5. Working with Services

In this chapter, we discuss how pods communicate within the cluster, how applications discover each other, and how to expose pods so that they can be accessed from outside of the cluster.

The primitive we will be using here is called a Kubernetes service, as depicted in Figure 5-1.

The service concept
Figure 5-1. The Kubernetes service concept

A service provides a stable virtual IP (VIP) address for a set of pods. Though pods may come and go, services allow clients to reliably discover and connect to the containers running in the pods by using the VIP. The “virtual” in VIP means it’s not an actual IP address connected to a network interface; its purpose is purely to forward traffic to one or more pods. Keeping the mapping between the VIP and the pods up to date is the job of kube-proxy, a process that runs on every node on the cluster. This kube-proxy process queries the API server to learn about new services in the cluster and updates the node’s iptables rules (iptables) accordingly, to provide the necessary routing information.

5.1 Creating a Service to Expose Your Application


You want to provide a stable and reliable way to discover and access your application within the cluster.


Create a Kubernetes service for the pods that make up your application.

Assuming you created an nginx deployment with kubectl run nginx --image nginx, you can automatically ...

Get Kubernetes Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.