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 resource 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 VIPs 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

Problem

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

Solution

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

Assuming you created an nginx deployment with kubectl create deployment nginx --image nginx:1.25.2 ...

Get Kubernetes Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.