How to Restart a Kubernetes Pod Effectively

To restart a pod in Kubernetes, you typically delete the pod, and the associated controller (like a Deployment) will automatically recreate it. This ensures a fresh start for the pod without downtime for your application. Use the following command to delete a pod:

kubectl delete pod <pod-name>

Alternatively, you can patch the Deployment to trigger a restart by updating an arbitrary annotation:

kubectl patch deployment <deployment-name> -p '{"spec":{"template":{"metadata":{"annotations":{"kubectl.kubernetes.io/restartedAt":"$(date +"%Y-%m-%dT%H:%M:%SZ")"}}}}}'

This method is preferred for zero-downtime restarts in production environments.