Show contents
Chapter 2 of 7 · 20 min
Services, ConfigMaps and Secrets
By the end of this chapter, you'll be able to
- Explain why you never reach a pod directly by its address
- Expose pods with a Service and choose its type
- Separate configuration from the image with a ConfigMap
- Use a Secret while knowing what it protects, and what it does not
The Service: a stable address
A Service gives a stable address and name to a group of pods, chosen by a label selector. It spreads requests across the ready pods, and automatically follows their replacements.
apiVersion: v1
kind: Service
metadata:
name: payment-api
spec:
selector:
app: payment-api
ports:
- port: 80
targetPort: 8000
Inside the cluster, other applications now reach the API at http://payment-api: an internal DNS turns this name into the Service's address. The Service receives requests on port 80 and forwards them to port 8000 of one of the pods.
| Type | Reachable from | Use |
|---|---|---|
ClusterIP (default) | inside the cluster | communication between services |
NodePort | a port on every node | tests, small clusters |
LoadBalancer | a public address provided by the cloud | exposing a service on the Internet |
In production, websites are often exposed through an Ingress: a single HTTP entry point that sends each domain name or path to the right Service.
The request reaches one of them. If this pod is replaced tomorrow, the client will see no difference.
Text description of the animation
- The mobile app sends a transfer request.
- The Ingress recognises the domain name and picks the Service.
- The Service knows the up-to-date list of pods carrying the label app: payment-api.
- The request reaches one of them. If this pod is replaced tomorrow, the client will see no difference.
The ConfigMap: configuration outside the image
The same image must run in testing and in production, with different settings. These settings go in a ConfigMap, which is then injected into the containers as environment variables:
apiVersion: v1
kind: ConfigMap
metadata:
name: payment-api-config
data:
CURRENCY: "XOF"
TRANSFER_LIMIT: "500000"
LOG_LEVEL: "info"
---
# in the Deployment's pod template:
containers:
- name: api
image: registry.example.com/payment-api:1.4.2
envFrom:
- configMapRef:
name: payment-api-config
In the application's code, you simply read these variables:
import os
os.environ.setdefault("CURRENCY", "XOF")
os.environ.setdefault("TRANSFER_LIMIT", "500000")
currency = os.environ["CURRENCY"]
limit = int(os.environ["TRANSFER_LIMIT"])
print(f"Limit per transfer: {limit} {currency}")
Limit per transfer: 500000 XOF
The setdefault calls are only there to run the example here: in the cluster, Kubernetes fills os.environ.
The Secret: for sensitive data
Passwords, API keys, certificates: they go in a Secret, used like a ConfigMap. The difference: Kubernetes can restrict who can access it, avoid displaying it, and encrypt it in its database if the administrator has set this up.
apiVersion: v1
kind: Secret
metadata:
name: payment-api-secrets
type: Opaque
stringData:
PROVIDER_KEY: "replace-me-with-the-real-key"
Mini-lab: one configuration per environmentPython 3.13
Output
Click “Run”: Python will load in your browser (about 10 MB the first time).Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Your turn
Worked exercise
Worked exercise: which pods does the Service send traffic to?
A Service selects the pods whose labels contain all the pairs of its selector. Write targets(selector, pods) that returns the names of the selected and ready pods, then test it.
pods = [
{"name": "api-7f9-a", "labels": {"app": "payment-api", "version": "1.4.2"}, "ready": True},
{"name": "api-7f9-b", "labels": {"app": "payment-api", "version": "1.4.2"}, "ready": False},
{"name": "api-test", "labels": {"app": "payment-api-test"}, "ready": True},
{"name": "api-c31-a", "labels": {"app": "payment-api", "version": "1.5.0"}, "ready": True},
]
selector = {"app": "payment-api"}
Hint 1
key, value of the selector, pod["labels"].get(key) == value. The all() function tests a condition on every pair.Hint 2
pod["ready"]: a Service sends no traffic to a pod that is not ready.Show the solution and common mistakes
def targets(selector, pods):
return [
p["name"] for p in pods
if p["ready"] and all(p["labels"].get(k) == v for k, v in selector.items())
]
pods = [
{"name": "api-7f9-a", "labels": {"app": "payment-api", "version": "1.4.2"}, "ready": True},
{"name": "api-7f9-b", "labels": {"app": "payment-api", "version": "1.4.2"}, "ready": False},
{"name": "api-test", "labels": {"app": "payment-api-test"}, "ready": True},
{"name": "api-c31-a", "labels": {"app": "payment-api", "version": "1.5.0"}, "ready": True},
]
print(targets({"app": "payment-api"}, pods))
print(targets({"app": "payment-api", "version": "1.5.0"}, pods))
['api-7f9-a', 'api-c31-a']
['api-c31-a']
The api-7f9-b pod matches the selector but is not ready: it receives nothing. api-test has a similar but different label: it is not selected.
Common mistakes:
- Testing
"payment-api" in value: "payment-api-test" would contain "payment-api", and a test pod would receive production traffic. Labels are compared exactly. - Forgetting the "ready" state: during start-up, requests would go to a pod that cannot answer yet.
Summary
- 1You never reach a pod by its address: it changes with every replacement.
- 2A Service gives a stable name and address to pods chosen by labels, and spreads traffic across the ready pods.
- 3ClusterIP for inside the cluster, LoadBalancer or Ingress for the outside.
- 4A ConfigMap separates configuration from the image; a Secret holds sensitive data.
- 5A Secret is base64-encoded, not encrypted: protect access to it and never commit it.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.