Docker & Kubernetes
Web Deployment
Write a Deployment for the web application.
The web and API Deployments
The API runs with two replicas, takes its settings from a ConfigMap and the password from a Secret, and has a readiness check. Web (nginx) has three replicas and forwards requests to http://api:8000:
# 20-api.yaml (summary)
spec:
replicas: 2
template:
spec:
containers:
- name: api
image: ghcr.io/ali/magaza-api:1.0
envFrom:
- configMapRef: {name: api-ayar}
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef: {name: db-gizli, key: POSTGRES_PASSWORD}
readinessProbe:
httpGet: {path: /saglik, port: 8000}
Exposing the web service
Only web is exposed to the outside world. In Minikube, NodePort is enough; in the cloud, a LoadBalancer or (later) an Ingress is used:
apiVersion: v1
kind: Service
metadata:
name: web
namespace: magaza
spec:
type: NodePort
selector:
app: web
ports:
- port: 80
targetPort: 80
nodePort: 30080
minikube service web -n magaza --url
Exercise The API deployment
Write a Deployment YAML: name api, namespace magaza, replicas: 2; selector and template label app: api; container named api, image ghcr.io/ali/magaza-api:1.0; the api-ayar ConfigMap under envFrom.
Show example solution
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
namespace: magaza
spec:
replicas: 2
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
containers:
- name: api
image: ghcr.io/ali/magaza-api:1.0
envFrom:
- configMapRef:
name: api-ayar
Test yourself
-
If the readiness check fails as long as the API can't connect to the database, what happens?
- Web crashes
- The API pods get no traffic but aren't restarted
- The API pods are deleted
- The database is restarted
Show answer
The API pods get no traffic but aren't restarted — A readiness failure only removes it from the Service endpoints.
-
Take a single key from a Secret.
valueFrom:
____: {name: db-gizli, key: POSTGRES_PASSWORD}
Show answer
valueFrom:
secretKeyRef: {name: db-gizli, key: POSTGRES_PASSWORD}
-
Match.
- api replicas: 2
- Redundant API
- readinessProbe
- No traffic until ready
- web NodePort
- Single external entry
- {name: x}
- YAML inline map
Next lesson: ConfigMap and Secret