Interview Prep/Real-World Interview/Write-a-Kubernetes-manifest tasks

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.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-a-Kubernetes-manifest tasks