Deployments, Services, Ingress, ConfigMaps & Secrets
Deploy the FastAPI service with config, secrets and an ingress.
By Invicly Tech, , in Full DevOps with Python. About 13 minutes to read.
In short
A Deployment keeps a set number of identical Pods running and rolls out new versions. A Service gives them a stable address, an Ingress routes outside traffic in, and ConfigMaps and Secrets supply settings.
Explained simply
Running a single Pod by hand is fine for a test, but if it dies, nothing brings it back. For a real app you use a Deployment: you say which image and how many copies (replicas), and Kubernetes keeps exactly that many running, replacing any that crash or disappear with their node.
Pods come and go, and each new one gets a new IP address. A Service solves this. It gives your app one stable name and address inside the cluster, and spreads requests across all healthy Pods that carry the right label.
To let people outside the cluster reach the app over HTTP, you add an Ingress. It holds rules like send api.example.com to the api Service. A program called an ingress controller reads these rules and does the actual routing.
Settings should not be baked into the image. A ConfigMap holds normal settings like LOG_LEVEL, and a Secret holds sensitive ones like database passwords. Kubernetes injects them into the containers as environment variables or files, so the same image runs in dev and prod.
Press Play on a diagram to walk through it one step at a time.
- 1Ingress
Routes api.example.com to a Service; needs an ingress controller.
- 2Service
Stable name and IP; selects Pods by label app=api.
- 3Deployment
Desired replicas and image; manages ReplicaSets for rollouts.
- 4Pods
FastAPI containers with readiness and liveness probes.
- 5ConfigMap and Secret
Environment variables and files injected into Pods.
- Ingress api
- host api.example.com
- backend: Service api port 80
- Service api
- selector app=api
- port 80 to targetPort 8000
- Deployment api
- ReplicaSet api-7c9f
- Pods labeled app=api
- ConfigMap api-config
- LOG_LEVEL
- WORKERS
- Secret api-secrets
- DATABASE_URL
- JWT_SECRET
- Default type
- Reachable only inside the cluster
- Used for app to app calls
- Adds a port in 30000 to 32767
- Open on every node's IP
- Handy for kind and testing
- Adds a cloud load balancer
- Gets an external address
- One per Service, so it costs more
In depth
Deployments, ReplicaSets and rolling updates
A Deployment does not manage Pods directly. It creates a ReplicaSet, and the ReplicaSet keeps the right number of Pods alive. When you change anything in the Pod template, such as the image tag or an environment variable, the Deployment creates a new ReplicaSet and slowly moves Pods from the old one to the new one. Changing only replicas just scales the current ReplicaSet and is not a rollout.
The default strategy is RollingUpdate with maxSurge 25% and maxUnavailable 25%. With 4 replicas, Kubernetes may run up to 5 Pods during the update and must keep at least 3 available. A new Pod counts as available only after its readiness probe passes, so a broken version stops the rollout instead of taking the site down.
Old ReplicaSets are kept (10 by default, set by revisionHistoryLimit), scaled to zero. That is what makes kubectl rollout undo fast: it just scales an old ReplicaSet back up.
- kubectl rollout status deployment/api waits until the rollout finishes or fails
- kubectl rollout history deployment/api lists revisions
- strategy Recreate stops all old Pods first; use it only if two versions cannot run together
- 1Template changes
New image tag creates ReplicaSet v2
- 2Surge
Start one v2 Pod
- 3Wait for ready
Readiness probe must pass
- 4Scale down
Remove one v1 Pod
- 5Repeat
Until v2 has 3 and v1 has 0
How a Service really routes traffic
A Service has a selector, like app: api. Kubernetes continuously finds the Pods whose labels match and are Ready, and writes their IPs into EndpointSlice objects. The Service also gets a virtual IP (the ClusterIP) and a DNS name from CoreDNS: api in the same namespace, or api.prod.svc.cluster.local in full.
The ClusterIP is not a real machine. kube-proxy on every node turns it into packet rules, so when a client Pod sends a packet to the Service IP, the node rewrites it to one Pod IP chosen from the endpoints. No extra hop, no proxy process in the path.
Two port fields confuse people. port is what clients connect to on the Service; targetPort is the port the container listens on. A Service can listen on 80 and forward to 8000. Service types build on each other: ClusterIP (default, inside only), NodePort (also opens a port in 30000 to 32767 on every node), and LoadBalancer (also asks the cloud for an external load balancer).
- Client Pod to CoreDNS: What is the IP of api?1What is the IP of api?
- CoreDNS to Client Pod: ClusterIP 10.96.40.122ClusterIP 10.96.40.12
- Client Pod to Node rules: Packet to 10.96.40.12:803Packet to 10.96.40.12:80
- Node rules to API Pod: Rewritten to 10.244.1.7:80004Rewritten to 10.244.1.7:8000
- API Pod to Client Pod: 200 OK5200 OK
Ingress and ingress controllers
An Ingress is only a set of HTTP rules: host names, URL paths, and which Service and port each one goes to, plus optional TLS using a certificate stored in a Secret. On its own it does nothing. An ingress controller, such as the NGINX, Traefik or HAProxy controller or a cloud one like the AWS Load Balancer Controller, watches Ingress objects and configures a real proxy. You choose which controller handles an Ingress with ingressClassName.
Each path has a pathType. Prefix matches by path segments, so /api matches /api and /api/users but not /apiv2. Exact matches only that one path. One Ingress, and therefore one external load balancer, can serve many Services, which is much cheaper than one LoadBalancer Service per app.
The newer Gateway API is the official successor to Ingress. It splits the job into GatewayClass, Gateway and HTTPRoute objects and supports features like header matching and traffic splitting without controller-specific annotations. Many teams are moving to it, but Ingress is still everywhere.
ConfigMaps and Secrets in practice
You can use a ConfigMap or Secret in two ways. As environment variables (env with valueFrom, or envFrom for all keys at once), or as files by mounting it as a volume, where each key becomes a file. The difference matters when values change: environment variables are read only when the container starts, so you must restart Pods (kubectl rollout restart deployment/api). Mounted files are updated in place after a short delay, unless you mounted with subPath.
A Secret's data is base64 encoded, which is not encryption: anyone who can read the Secret can decode it. Protect Secrets with RBAC so only the app's namespace and admins can read them, turn on encryption at rest for etcd (managed clusters such as EKS can use a KMS key), and keep the real values out of Git. Common patterns are creating Secrets from the CI system, or syncing them from AWS Secrets Manager with the External Secrets Operator.
- kubectl create secret generic api-secrets --from-literal=DB_PASSWORD=... creates one without YAML
- stringData lets you write plain values in YAML; Kubernetes encodes them
- Mark a ConfigMap or Secret immutable: true if it should never change
Probes: readiness, liveness and startup
The kubelet can run three kinds of health checks on each container, using an HTTP GET, a TCP connection, a gRPC call or a command. They have different jobs. Readiness decides if the Pod receives traffic: while it fails, the Pod is removed from Service endpoints but not restarted. Liveness decides if the container is broken: after it fails failureThreshold times in a row, the kubelet kills and restarts it. Startup protects slow starters: until it succeeds, liveness and readiness are not run.
The defaults are periodSeconds 10, timeoutSeconds 1 and failureThreshold 3. So a hung app is restarted about 30 seconds after liveness starts failing.
For a FastAPI service, a readiness endpoint may check that the database pool is usable, so traffic stops during an outage. The liveness endpoint should only prove the process can answer, with no calls to other services; otherwise one database blip restarts every Pod at once. Also set resources.requests for CPU and memory, which the scheduler uses to place Pods.
- Can it take traffic now?
- Failing: removed from the Service
- Container keeps running
- May check dependencies
- Is the process stuck?
- Failing: container restarted
- Keep it cheap and local
- Never check the database
- Has it finished starting?
- Holds back the other probes
- For slow boots or migrations
- Runs only until first success
Learn it in 6 short steps
About 1h 10m each. Track them with a timer in your plan.
- 1Write a Deployment for the FastAPI app~1h 10m
- 2Expose it with a Service~1h 10m
- 3Route traffic with an Ingress~1h 10m
- 4Inject config with ConfigMaps~1h 10m
- 5Store credentials in Secrets~1h 10m
- 6Add readiness and liveness probes~1h 10m
Key points
- Labels connect everything: the Service selector must match the Pod template labels.
- Readiness probes keep a Pod out of the Service until it can handle traffic.
- Liveness probes restart a container that is stuck or deadlocked.
- Secrets are only base64 encoded by default; restrict access and enable encryption.
Key terms
Tap a card to flip it. 0/6 known
Examples
apiVersion: apps/v1
kind: Deployment
metadata: { name: api }
spec:
replicas: 3
selector: { matchLabels: { app: api } }
template:
metadata: { labels: { app: api } }
spec:
containers:
- name: api
image: ghcr.io/acme/api:1.4.0
ports: [{ containerPort: 8000 }]
envFrom: [{ configMapRef: { name: api-config } }, { secretRef: { name: api-secrets } }]
readinessProbe: { httpGet: { path: /health, port: 8000 } }Three replicas with config, secrets and a readiness probe.
Expose the FastAPI Deployment with a Service and an Ingress
The Deployment from the explainer labels its Pods app: api and listens on 8000. Step 1: a ClusterIP Service selects app: api and maps port 80 to targetPort 8000. Step 2: an Ingress sends every path on api.example.com to that Service. Step 3: apply both and check kubectl get endpointslices -l kubernetes.io/service-name=api; if no addresses appear, the selector does not match the Pod labels or the Pods are not ready. Step 4: in kind, test with kubectl port-forward svc/api 8080:80 and curl localhost:8080/health.
apiVersion: v1
kind: Service
metadata: { name: api }
spec:
selector: { app: api }
ports: [{ port: 80, targetPort: 8000 }]
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata: { name: api }
spec:
ingressClassName: nginx
rules:
- host: api.example.com
http: { paths: [{ path: /, pathType: Prefix, backend: { service: { name: api, port: { number: 80 } } } }] }Change config, ship a new image, and roll back
Step 1: update the ConfigMap from the command line; piping a client dry run into apply creates or updates it. Step 2: because the app reads config from environment variables, restart the Pods with a rollout. Step 3: release 1.4.1 by changing the image, then watch the rollout. Step 4: the new Pods never become ready, so the rollout stalls while the old Pods keep serving. Step 5: roll back to the previous revision and confirm.
kubectl create configmap api-config --from-literal=LOG_LEVEL=debug \
--dry-run=client -o yaml | kubectl apply -f -
kubectl rollout restart deployment/api
kubectl set image deployment/api api=ghcr.io/acme/api:1.4.1
kubectl rollout status deployment/api --timeout=120s # times out: Pods not ready
kubectl get pods -l app=api # new Pods 0/1 READY
kubectl rollout undo deployment/api
kubectl rollout history deployment/apiQuick reference
- kubectl create deployment api --image=img:tag --dry-run=client -o yaml writes a starter manifest
- kubectl port-forward svc/api 8080:80 tests a Service from your laptop
- Service DNS name: service.namespace.svc.cluster.local
- kubectl rollout undo deployment/api returns to the previous revision
- Env vars from ConfigMaps change only after a Pod restart; mounted files update by themselves
- echo value | base64 -d decodes a Secret value; base64 is not encryption
- No endpoints for a Service means a label mismatch or Pods that are not ready
- Probe defaults: periodSeconds 10, timeoutSeconds 1, failureThreshold 3
Common mistakes
- Service selector labels that do not match the Pods, so it has no endpoints.
- A liveness probe that checks the database, restarting every Pod when the DB blips.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Create an nginx Deployment with 2 replicas, expose it with a ClusterIP Service, and reach it from your laptop.
- Exercise 2medium
Store DB_PASSWORD in a Secret and LOG_LEVEL in a ConfigMap, inject both into the web Deployment as environment variables, and verify inside a Pod.
- Exercise 3hard
Add readiness and liveness probes to the FastAPI Deployment, then make readiness fail on purpose and show that the Pod leaves the Service without being restarted.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Should I use a LoadBalancer Service or an Ingress?
- For HTTP apps, an Ingress (or Gateway API route) lets many Services share one load balancer with host and path rules. A LoadBalancer Service fits non-HTTP traffic or a single app.
- Are Kubernetes Secrets secure?
- Only partly. They are base64 encoded, not encrypted, so you need RBAC, encryption at rest and a way to keep the real values out of Git, such as External Secrets Operator.
- Why is my Service not sending traffic to my Pods?
- Usually the Service selector does not match the Pod labels, or the Pods are not passing readiness. Check kubectl get endpointslices for the Service and kubectl describe pod for probe failures.
- What is the difference between a Deployment and a StatefulSet?
- A Deployment runs interchangeable Pods with random names. A StatefulSet gives each Pod a stable name and its own persistent volume, which databases and queues need.