🐶
Kubernetes

Kubernetes Pod Rolling Restart Without YAML Changes

By Jan on 02/09/2025

Learn how to seamlessly roll out updates to your Kubernetes applications by triggering rolling restarts of your pods without modifying your deployment YAML files.

Kubernetes Pod Rolling Restart Without YAML Changes

Table of Contents

Introduction

Restarting deployments in Kubernetes is a common task, often required after configuration changes or updates. This guide outlines various methods to safely restart deployments and address ConfigMap or Secret changes, along with important considerations to minimize downtime and ensure a smooth restart process.

Step-by-Step Guide

  1. Using kubectl rollout restart (Kubernetes 1.15+) This is the simplest method if you're using Kubernetes 1.15 or later.

    kubectl rollout restart deployment/<deployment-name>
  2. Patching the deployment with a dummy annotation (before Kubernetes 1.15)

    kubectl patch deployment <deployment-name> -p '{"spec":{"template":{"metadata":{"annotations":{"date":"`date +'%s'`"}}}}}'

    This adds a temporary annotation with the current timestamp, triggering a rolling restart.

  3. Deleting Pods (not recommended)

    While you can delete pods to force their recreation, this is not ideal as it can lead to downtime if not managed carefully. It's better to use the rolling update mechanisms provided by Deployments.

For ConfigMap or Secret changes:

  • Use kubectl rollout restart with --from-deployment (Kubernetes 1.15+)

    kubectl rollout restart deployment/<deployment-name> --from-deployment=<previous-deployment-name>
  • Add a hash to the volume mount path:

    By adding a hash of the ConfigMap/Secret content to the mount path, you can force a remount and pod restart when the content changes.

Important Considerations:

  • Downtime: Ensure your application can handle the brief downtime during a rolling restart.
  • Resource Limits: Be mindful of resource limits during restarts, as new pods will be created before old ones are terminated.
  • Health Checks: Utilize readiness and liveness probes to ensure pods are healthy after a restart.

Code Example

This document provides code examples for restarting Kubernetes deployments and handling ConfigMap and Secret changes in various Kubernetes versions. It covers using kubectl rollout restart for deployments and handling ConfigMap/Secret changes by triggering restarts based on previous deployments or by adding hashes to volume mount paths. The document also includes important considerations such as potential downtime, resource limits, and the importance of health checks.

This document provides code examples for restarting Kubernetes deployments and handling changes to ConfigMaps and Secrets.

1. Restarting Deployments:

a) Using kubectl rollout restart (Kubernetes 1.15+)

This is the simplest and recommended method for Kubernetes versions 1.15 and later.

kubectl rollout restart deployment/<deployment-name>

Example:

kubectl rollout restart deployment/my-app-deployment

b) Patching the deployment with a dummy annotation (before Kubernetes 1.15)

For Kubernetes versions prior to 1.15, you can trigger a rolling restart by patching the deployment with a dummy annotation.

kubectl patch deployment <deployment-name> -p '{"spec":{"template":{"metadata":{"annotations":{"date":"`date +'%s'`"}}}}}'

Example:

kubectl patch deployment my-app-deployment -p '{"spec":{"template":{"metadata":{"annotations":{"date":"`date +'%s'`"}}}}}'

2. Handling ConfigMap/Secret Changes:

a) Using kubectl rollout restart with --from-deployment (Kubernetes 1.15+)

This method allows you to trigger a rolling restart based on changes in a previous deployment, which can be useful for ConfigMap/Secret updates.

kubectl rollout restart deployment/<deployment-name> --from-deployment=<previous-deployment-name>

Example:

kubectl rollout restart deployment/my-app-deployment --from-deployment=my-app-deployment-previous

b) Adding a hash to the volume mount path:

This method involves adding a hash of the ConfigMap/Secret content to the volume mount path in your deployment configuration. This forces a remount and pod restart when the content changes.

Example Deployment Configuration (YAML):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: my-app-image:latest
        volumeMounts:
        - name: my-config-volume
          mountPath: /app/config
          subPath: my-config.json
      volumes:
      - name: my-config-volume
        configMap:
          name: my-config
          # Add a hash of the ConfigMap content to the mountPath
          mountPath: $(kubectl get configmap my-config -o jsonpath='{.data.my-config\.json}' | md5sum)

Important Considerations:

  • Downtime: Be aware that rolling restarts can cause brief downtime for your application.
  • Resource Limits: Ensure your cluster has sufficient resources to handle the creation of new pods during a rolling restart.
  • Health Checks: Implement readiness and liveness probes in your application to ensure pods are healthy after a restart.

Additional Notes

General:

  • Alternatives to kubectl rollout restart: For clusters older than Kubernetes 1.15, consider using tools like kubectl rollout history and kubectl rollout undo to revert to a previous deployment state instead of manually patching.
  • Graceful Termination: Ensure your application handles SIGTERM signals gracefully to allow for in-flight requests to complete during a restart.
  • Deployment Strategies: Explore different deployment strategies like Blue/Green or Canary deployments for zero-downtime deployments and controlled rollouts.

ConfigMap and Secret Management:

  • Dynamic Updates: For applications that require dynamic configuration updates without restarts, consider using tools like confd or Spring Cloud Config Server.
  • Secrets Management: Avoid storing sensitive information directly in ConfigMaps. Use Kubernetes Secrets and consider dedicated secrets management solutions like HashiCorp Vault.

Troubleshooting:

  • kubectl rollout status: Monitor the rollout process and identify any issues.
  • Pod Logs: Examine pod logs for errors related to restarts or configuration changes.
  • Events: Check Kubernetes events for insights into deployment and pod behavior.

Best Practices:

  • Automation: Integrate deployment and restart processes into CI/CD pipelines for automated and reliable deployments.
  • Monitoring and Alerting: Set up monitoring and alerting for your deployments to detect and respond to issues promptly.
  • Version Control: Track changes to your deployment configurations and ConfigMaps/Secrets in version control for better change management.

Summary

This document outlines various methods for restarting deployments in Kubernetes, along with considerations for minimizing downtime and ensuring application health.

Methods for Restarting Deployments:

| Kubernetes Version | Method | Description

Conclusion

Choosing the right restart strategy depends on your Kubernetes version and specific needs. Prioritize kubectl rollout restart for its simplicity and effectiveness. When dealing with ConfigMap and Secret changes, implement solutions that trigger updates automatically while minimizing disruption. By understanding these techniques and considerations, you can ensure a smooth and efficient restart process for your Kubernetes deployments.

References

  • Best approach to restart pods when ConfigMap is updated via operator Best approach to restart pods when ConfigMap is updated via operator | Hi, I’m runnnig minikube cluster with a custom operator for a domain-specific application. This application requires some of its important configs to be kept in a .toml file. I have been successfully able to load these toml configs using a .yaml field via a Go-based operator created using operator-sdk. The application requires a restart whenever a change is made to the .toml config. (I have been successful upto the point of getting the configs updated on the volume-mounted config). My question...
  • kubernetes - kubectl - How to restart a deployment (or all ... kubernetes - kubectl - How to restart a deployment (or all ... | Oct 8, 2020 ... Does this answer your question? How to rolling restart pods without changing deployment yaml in kubernetes? – David Maze. Commented Oct 8, 2020 ...
  • Helm upgrade --install doesn't restart/recreate the pod if the image ... Helm upgrade --install doesn't restart/recreate the pod if the image ... | Output of helm version: 3.2.0 Output of kubectl version: latest Cloud Provider/Platform (AKS, GKE, Minikube etc.): GKE - Google Kubernetes Engine ISSUE Hey guys! I am coming today because I am faci...
  • Restart pods when configmap updates in Kubernetes? - Stack ... Restart pods when configmap updates in Kubernetes? - Stack ... | May 19, 2016 ... Is it possible, and if so how, to force a rolling restart of a deployment in Kubernetes without changing anything in the actual template?
  • kubectl rollout restart | Kubernetes kubectl rollout restart | Kubernetes | Synopsis Restart a resource. Resource rollout will be restarted. kubectl rollout restart RESOURCE Examples # Restart all deployments in the test-namespace namespace kubectl rollout restart deployment -n test-namespace # Restart a deployment kubectl rollout restart deployment/nginx # Restart a daemon set kubectl rollout restart daemonset/abc # Restart deployments with the app=nginx label kubectl rollout restart deployment --selector=app=nginx Options --allow-missing-template-keys     Default: true If true, ignore any errors in templates when a field or map key is missing in the template.
  • How to Restart Kubernetes Pods With Kubectl How to Restart Kubernetes Pods With Kubectl | There is no kubectl restart [podname] command for use with Kubernetes. Learn different ways to achieve a pod ‘restart’ with kubectl.
  • Kubernetes Rolling update / restart without changing the version of ... Kubernetes Rolling update / restart without changing the version of ... | With the default kubernetes we cannot maintain rolling update while keeping the same container version but there is a workaround for this,
  • Deployments | Kubernetes Deployments | Kubernetes | A Deployment manages a set of Pods to run an application workload, usually one that doesn't maintain state.
  • Kubernetes Rollout Step | Harness Developer Hub Kubernetes Rollout Step | Harness Developer Hub | This topic discusses the Kubernetes Rollout Step

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait