Kubernetes
pods
rolling restart
deployment
DevOps

How to rolling restart pods without changing deployment yaml in kubernetes?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

In a Kubernetes environment, managing pod lifecycles efficiently is crucial for maintaining application availability and performance. A rolling restart updates your application pods gracefully without downtime. While this is typically triggered by altering the deployment YAML file, there are strategies to achieve this without any modifications to the deployment manifest.

This article explores different methods for triggering a rolling restart of pods within a Kubernetes deployment without changing the deployment YAML.

Understanding Kubernetes Rolling Restart

A rolling restart applies updates gradually to instances of a pod within a deployment, ensuring that the application remains available during the restart process. This is achieved by killing existing pods and creating new ones incrementally.

Reasons for Rolling Restarts

  • Patch Vulnerabilities: There may be a need to restart pods to apply security patches.
  • Resource Issues: Restarting can clear resource (CPU/Memory) leaks.
  • Refreshing Configurations/Secrets: Sometimes applications need a restart to load new configurations or secrets.

Methods for Rolling Restart Without YAML Modification

1. Kubernetes kubectl rollout restart Command

The most straightforward way is to use the kubectl rollout restart command, which forces a restart of the pods in a deployment:

bash
kubectl rollout restart deployment <deployment-name>

This command works by incrementing the deployment's pod-template-hash, thereby triggering a redeployment of pods.

Example:

bash
kubectl rollout restart deployment my-app-deployment

2. Touch an Annotation

Changing annotations is another non-invasive way to restart pods, as updating an annotation doesn’t affect the application's logic.

Step-by-step Process:

  • Identify the deployment:
bash
  kubectl get deployments
  • Patch the deployment by updating an existing annotation or adding a new one:
bash
  kubectl patch deployment <deployment-name> \
  -p "{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"date\":\"$(date +%s)\"}}}}}"

This command updates the pod template’s metadata, which causes the deployment to redeploy with the same spec since the pod template has changed.

3. Manual Pod Deletion

While this approach should be used cautiously, manually deleting pods can trigger Kubernetes to spin up new ones as replacements.

Procedure:

  • Scale down the deployment to zero:
bash
  kubectl scale deployment <deployment-name> --replicas=0
  • Scale back up:
bash
  kubectl scale deployment <deployment-name> --replicas=<original-number-of-replicas>

Note: Scaling to zero may cause downtime as all pods are deleted before new ones start, making this less ideal for zero-downtime requirements.

Comparing Methods

markdown
1| Method | Command | Pros | Cons |
2| ------------------------------- | ----------------------------------------------------------- | ----------------------------------- | ------------------------------------- |
3| `kubectl rollout restart` | `kubectl rollout restart deployment <deployment-name>` | Simple and direct | Requires `kubectl v1.15` or higher |
4| Annotation Touch | `kubectl patch deployment <deployment-name> -p {...}` | Minimal impact | More involved than direct command |
5| Manual Pod Deletion | Scale down to zero and back up | Control over timing | Risk of downtime during scale down | ``` |
6
7## Conclusion
8
9Rolling restarts are essential for maintaining the smooth operation of applications in Kubernetes. This guide outlined direct methods to achieve this goal without changing the deployment YAML, which is a crucial skill for operators and developers aiming to maintain application uptime and responsiveness.
10
11Each method comes with its set of advantages and trade-offs. The choice depends on the specific needs of the application and operational constraints, such as downtime tolerance and ease of execution.
12
13Credit should be given to Kubernetes' robust design, allowing flexibility in managing and maintaining application resources efficiently. By mastering these techniques, you're better positioned to manage your Kubernetes workloads seamlessly.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.