## Intro

Kubernetes Pod CI/CD automation requires turning an observed problem into a verified result. Operators must identify the installed version, deployment topology, prerequisites, and the exact component under inspection before making any change. This article connects the core workflow—automation, deployment, pipeline, and rollback—to concrete commands, expected output, failure signals, and recovery decisions.

The focus is operational safety: observe before changing, limit the blast radius, use placeholders instead of secrets, verify the outcome, and document how to recover if the expected state is not reached. Each section includes a real command, snippet, or checked output so you can apply the pattern directly. The common pitfalls section later helps you avoid mistakes that cause silent failures or unrecoverable states.

## Version and Environment Inventory

Before touching a cluster, record what you are running and where. This inventory is read-only: it establishes a baseline and prevents changes based on wrong assumptions about versions or topology.

Run these commands and save the output with a timestamp:

```bash
kubectl version --short
kubectl cluster-info
kubectl get nodes -o wide
kubectl get pods -A -o wide
```

Expected output sample:

```text
Client Version: v1.29.2
K8s Master Version: v1.29.2
Kubernetes control plane is running at https://192.168.49.2:8443
CoreDNS is running at https://192.168.49.2:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

NAME       STATUS   ROLES           AGE   VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE                                      KERNEL-VERSION   CONTAINER-RUNTIME
minikube   Ready    control-plane   31d   v1.29.2   192.168.49.2   <none>        Ubuntu 22.04.3 LTS                             6.5.0-21-generic   docker://26.0.0

NAMESPACE     NAME                              READY   STATUS    RESTARTS        AGE   IP             NODE       NOMINATED NODE   READINESS GATES
kube-system   coredns-5dd5756b68-7js8t          1/1     Running   3 (2d14h ago)   31d   10.244.0.2     minikube   <none>           <none>
```

For a specific application, inspect its Deployment and Pods:

```bash
kubectl get deploy,pods -n myapp
```

Output:

```text
NAME                              READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/myapp-backend     1/1     1            1           5d2h

NAME                                 READY   STATUS    RESTARTS   AGE
pod/myapp-backend-7f9b5c8d6c-4g7h2   1/1     Running   0          3h15m
```

Check resource requests and limits because CI/CD jobs can consume more capacity than expected. Use `kubectl describe` to see events and resource blocks:

```bash
kubectl describe pod myapp-backend-7f9b5c8d6c-4g7h2 -n myapp
```

Look for events like `FailedScheduling` or `Insufficient memory`. If a previous release failed, inspect the old Pod before deleting anything:

```bash
kubectl logs myapp-backend-7f9b5c8d6c-4g7h2 --previous -n myapp
kubectl rollout status deployment/myapp-backend -n myapp
```

Keep the local test small. Apply one manifest in a local environment like minikube or kind first. After verification, move to staging and production with the same commands.

## Safe Configuration Path

Configuration changes are the highest-risk part of CI/CD. A misconfigured environment variable or missing resource limit can break rolling updates or expose data. Follow a strict path: observe, test offline, apply a minimal diff, verify, and document rollback.

Start by exporting the current Deployment manifest as a backup:

```bash
kubectl get deploy myapp-backend -n myapp -o yaml > myapp-backend.backup.yaml
```

Edit the manifest with a small change, for example updating an image tag and adding a resource request:

Before:

```yaml
      containers:
      - name: backend
        image: myapp/backend:1.0.1
        ports:
        - containerPort: 8080
```

After:

```yaml
      containers:
      - name: backend
        image: myapp/backend:1.0.2
        ports:
        - containerPort: 8080
        resources:
          requests:
            memory: "128Mi"
            cpu: "100m"
          limits:
            memory: "256Mi"
            cpu: "200m"
```

Apply with `kubectl apply` and immediately watch the rollout:

```bash
kubectl apply -f rollout.yaml -n myapp
kubectl rollout status deployment/myapp-backend -n myapp --timeout=120s
```

Expected output on success:

```text
deployment "myapp-backend" successfully rolled out
```

If the rollout hangs or fails, check Pod conditions and Events:

```bash
kubectl get pods -n myapp -l app=myapp-backend -w
kubectl describe pod myapp-backend-6b4d9699f6-9z2b2 -n myapp
```

Look for `FailedScheduling`, `CrashLoopBackOff`, or `ImagePullBackOff`. Test new configurations in a namespace or a local cluster before applying to production. Use `--dry-run=client` and `--validate=false` intentionally only to preview changes:

```bash
kubectl apply -f rollout.yaml -n myapp --dry-run=client
```

Never pass secrets in plain text inside manifests. Use environment variables referenced from a Secret with placeholders or managed identity. For example:

```yaml
env:
- name: DATABASE_PASSWORD
  valueFrom:
    secretKeyRef:
      name: myapp-db-secret
      key: password
```

Store actual secrets in the cluster Secret, not in the manifest repository. If you must test with a local value, use a placeholder and never commit it.

## Verification and Diagnostics

Verification goes beyond rollout status. You need to test that the new Pod actually serves traffic, handles basic requests, and fails into a visible state if broken.

Execute commands inside the running Pod without exposing it publicly:

```bash
kubectl exec -it myapp-backend-6b4d9699f6-9z2b2 -n myapp -- wget -O- http://localhost:8080/health
```

Expected output:

```text
OK
```

Check logs from the current container:

```bash
kubectl logs myapp-backend-6b4d9699f6-9z2b2 -n myapp --tail=50
```

Look for startup errors, missing environment variables, or connection refusals. Use port-forward to test the service locally:

```bash
kubectl port-forward svc/myapp-backend -n myapp 8080:80
```

Then from another terminal:

```bash
curl -v http://localhost:8080/health
```

Expected successful response includes HTTP 200 or a defined health JSON.

Diagnostics for a failing rollout:

```bash
kubectl get events -n myapp --sort-by=.lastTimestamp | tail -20
```

This reveals why the Deployment is not progressing: incorrect image tag (ErrImagePull), missing ConfigMap (CreateContainerConfigError), or resource quota exceeded. For any crash loop, inspect the previous container logs:

```bash
kubectl logs myapp-backend-6b4d9699f6-9z2b2 --previous -n myapp
```

Capture the exit code and the last lines. Common exit code 137 often indicates OOMKilled; check memory limits and adjust. Use `kubectl describe pod` to confirm:

```text
Last State:     Terminated
  Reason:       OOMKilled
  Exit Code:    137
```

For automated pipelines, include these checks as stages after deployment. A pipeline that only runs `kubectl apply` and exits success is incomplete; add a verification stage that runs `kubectl rollout status` and a curl against the service.

## Failure Modes and Recovery

Deployments fail in predictable ways. Recognizing the pattern and executing a known recovery removes panic.

**Image pull failure.** Symptom: `kubectl get pods` shows `ErrImagePull` or `ImagePullBackOff`. Cause: wrong tag, missing secret, network restriction. Recovery: check the image name, verify pull secret exists, or push the image to a reachable registry. To fix the tag, update the deployment with the correct image and re-apply.

**CrashLoopBackOff.** Symptom: Pod repeatedly restarts; `kubectl describe pod` shows `State: Waiting` with reason `CrashLoopBackOff`. Cause: application crashes at startup, missing config, or port conflict. Recovery: inspect logs with `--previous` and fix the underlying issue. If config is missing, add the ConfigMap or Secret and re-apply.

**Rollout stuck.** Symptom: `kubectl rollout status` times out; some Pods remain `Pending` or `Terminating`. Cause: insufficient resources, node selector mismatch, or PodDisruptionBudget. Recovery: check Events and resource requests; scale down or adjust the manifest. If you cannot resolve immediately, roll back.

**Rollback command.** Kubernetes keeps the previous ReplicaSets for a Deployment. To roll back to the last known good revision:

```bash
kubectl rollout undo deployment/myapp-backend -n myapp
```

Verify the rollback:

```bash
kubectl rollout status deployment/myapp-backend -n myapp
```

If you need a specific revision, list history:

```bash
kubectl rollout history deployment/myapp-backend -n myapp
```

Output:

```text
deployment.apps/myapp-backend
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
```

Then rollback to revision 1:

```bash
kubectl rollout undo deployment/myapp-backend -n myapp --to-revision=1
```

Always document the rollback command with the change request. After recovery, run the same verification as a normal deployment, including the health endpoint and logs.

## Operations Checklist

Adopt this checklist as a baseline for every CI/CD operation that touches Pods. Assign each item to a single owner and revisit at least weekly during active development; after each incident, review the checklist within 24 hours.

| Step | Action | Owner | Verification |
|------|--------|-------|--------------|
| 1 | Record current version and topology with `kubectl version` and `kubectl get nodes` | Priya Shah, Engineering Lead | Command output stored in runbook; reviewed weekly |
| 2 | Export current manifests as backup before any change | Alex Chen, DevOps Engineer | `kubectl get deploy -o yaml > backup.yaml` saved in git |
| 3 | Apply manifest with a single change and set `--dry-run=client` first | Alex Chen, DevOps Engineer | Dry-run output reviewed; no errors |
| 4 | Monitor rollout with `kubectl rollout status --timeout=120s` | Alex Chen, DevOps Engineer | Command returns "successfully rolled out" |
| 5 | Verify Pod health via `kubectl exec` or port-forward and curl | Sam Okafor, QA Engineer | HTTP 200 or expected response |
| 6 | Check logs for warnings or errors | Sam Okafor, QA Engineer | Log tail reviewed and stored |
| 7 | Test rollback path in staging or local cluster | Priya Shah, Engineering Lead | `kubectl rollout undo` succeeds and Pod recovers |

Revisit each item in the weekly deployment review. If a step failed or was skipped during an incident, update the checklist and the runbook.

## Common Pitfalls and How to Avoid Them

CI/CD automation with Pods breaks in subtle ways. These are the most frequent mistakes and how to prevent or recover from them.

**Pitfall 1: Applying changes without a backup.**
Why: In urgency, operators modify a live Pod directly with `kubectl edit pod`, and the change is lost or causes drift. How to avoid: always export the manifest before any change. If you need to patch, use `kubectl patch deployment`, not direct Pod edits. Recovery: if the Pod was deleted, recreate from the latest Deployment manifest.

**Pitfall 2: Hardcoding secrets in CI/CD scripts or manifests.**
Why: Developers copy-paste a password into a YAML for quick testing and forget to remove it before push. How to avoid: use environment variables referencing a Secret; use a `.gitignore` for any temporary file with secrets; use a CI secrets manager. Recovery: immediately rotate the leaked credential and replace the manifest with a Secret reference.

**Pitfall 3: No readiness or liveness probes.**
Why: Deployment may report success but the Pod is not actually serving traffic, causing silent outages. How to avoid: define `readinessProbe` and `livenessProbe` in the container spec. Example:

```yaml
readinessProbe:
  httpGet:
    path: /health
    port: 8080
  initialDelaySeconds: 5
  periodSeconds: 10
livenessProbe:
  httpGet:
    path: /health
    port: 8080
  initialDelaySeconds: 15
  periodSeconds: 20
```

Recovery: add probes to the Deployment and re-apply.

**Pitfall 4: Not setting resource requests and limits.**
Why: Without limits, a misbehaving Pod can consume all node resources and kill other workloads. How to avoid: always set requests and limits for every container. Use the Kubernetes metrics server and `kubectl top pods` to tune values. Recovery: add or adjust resources and apply.

**Pitfall 5: Assuming `kubectl apply` means successful deployment.**
Why: The command returns after the object is created, not after the rollout is complete. How to avoid: always run `kubectl rollout status` after apply and fail the pipeline if the exit code is non-zero. Recovery: if the pipeline missed this, manually check and roll back if needed.

**Pitfall 6: Rolling out changes that are too large.**
Why: A single manifest with many changes makes it hard to identify what caused a failure. How to avoid: split changes into small, logically independent manifests and apply them one at a time. Recovery: if a large change fails, use `kubectl rollout undo` and re-apply smaller changes.

## Conclusion

Operational Kubernetes Pod CI/CD is a discipline of small, observable, reversible steps. The commands and examples in this guide cover the full cycle: inventory your environment, change configuration safely, verify the result, and recover on failure. Each step includes a concrete command or output to match against.

Start with one low-risk verification: export your current Deployment as backup, apply a single change to a local cluster, and run `kubectl rollout status` plus a health check. Record the expected output and the rollback command. Make the checklist part of every release, with a single owner per item and a weekly review.

A reliable workflow makes failure visible, protects sensitive values, limits changes to the intended resource, and defines recovery before an incident forces the decision. Adopt these patterns and evolve them as your cluster and pipeline grow.