Write a Deployment and Service for an Nginx app with 3 replicas, resources and probes.
BeginnerHands-onWrite-a-Kubernetes-manifest tasks
Answer
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
labels: { app: web }
spec:
replicas: 3
selector:
matchLabels: { app: web }
template:
metadata:
labels: { app: web }
spec:
containers:
- name: nginx
image: nginx:1.27
ports:
- containerPort: 80
resources:
requests: { cpu: 100m, memory: 128Mi }
limits: { memory: 256Mi }
readinessProbe:
httpGet: { path: /, port: 80 }
periodSeconds: 5
livenessProbe:
httpGet: { path: /, port: 80 }
initialDelaySeconds: 10
periodSeconds: 10
---
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector: { app: web }
ports:
- port: 80
targetPort: 80
Key trick → The Service selector and the Deployment's selector.matchLabels must both match the pod template labels, otherwise the Service has no endpoints.
More on Write-a-Kubernetes-manifest tasks
Add an HPA that scales the Deployment between 3 and 15 pods at 70% CPU.BeginnerFix this broken Deployment YAML.BeginnerWrite a ConfigMap and Secret and inject them as environment variables and as a mounted file.IntermediateWrite an Ingress that routes `/api` to the api service and `/` to the web service with TLS.IntermediateWrite a pod spec with a hardened securityContext.IntermediateWrite a CronJob that runs a backup every night at 02:00 and keeps limited history.Intermediate