šŸ¶
Kubernetes

Kubernetes: Cleaning Up Replica Sets After Deployment Updates

By Jan on 01/21/2025

Learn how to clean up old Replica Sets in Kubernetes when updating deployments to improve cluster performance and manage resources effectively.

Kubernetes: Cleaning Up Replica Sets After Deployment Updates

Table of Contents

Introduction

In Kubernetes, Deployments provide a declarative way to manage your application's lifecycle. They ensure that a specified number of Pods, representing your application instances, are running at any given time. Deployments achieve this by using ReplicaSets, which are responsible for maintaining the desired number of Pods. When you update a Deployment, it doesn't directly modify the existing Pods. Instead, it creates a new ReplicaSet with the updated configuration and gradually replaces the old Pods with new ones managed by the new ReplicaSet.

Step-by-Step Guide

Kubernetes Deployments use ReplicaSets to manage your Pods. When you update a Deployment, it creates a new ReplicaSet and scales it up while scaling down the old one.

To control how many old ReplicaSets are kept, use .spec.revisionHistoryLimit in your Deployment definition.

apiVersion: apps/v1
kind: Deployment
...
spec:
  revisionHistoryLimit: 3 

This example keeps a maximum of 3 old ReplicaSets. Setting it to 0 will remove all old ReplicaSets immediately.

However, simply deleting ReplicaSets directly is not recommended. Kubernetes manages these for rollback functionality and history. If you're experiencing issues with old ReplicaSets not being cleaned up, ensure your Deployment configuration is correct and that there are no other conflicting factors in your cluster.

Code Example

This code provides an example of a Kubernetes Deployment configured to maintain a revision history limit of 3. The deployment.yaml file defines the deployment, while the update_deployment.sh script simulates updates to the deployment, showcasing how old ReplicaSets are managed based on the revision history limit. The script updates the deployment five times, each time updating the Nginx image tag and listing the associated ReplicaSets. The output demonstrates that Kubernetes retains a maximum of three old ReplicaSets, deleting older ones as new updates are applied.

This example demonstrates a Kubernetes Deployment with a revisionHistoryLimit set to 3. It also includes a script to simulate updates and demonstrate how old ReplicaSets are managed.

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  revisionHistoryLimit: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest

update_deployment.sh

#!/bin/bash

for i in {1..5}; do
  echo "Updating deployment to version $i..."
  kubectl set image deployment/nginx-deployment nginx=nginx:$i --record
  sleep 5
  kubectl get replicasets -l app=nginx
  echo "-------------------------"
done

Explanation:

  1. deployment.yaml: This file defines a Deployment named nginx-deployment with revisionHistoryLimit set to 3. This means Kubernetes will keep a maximum of 3 old ReplicaSets after each update.
  2. update_deployment.sh: This script simulates updating the Deployment 5 times.
    • It uses kubectl set image to update the Nginx image tag, effectively creating a new revision each time.
    • The --record flag annotates the Deployment with information about each update.
    • It then uses kubectl get replicasets to list the ReplicaSets associated with the Deployment, showcasing how old ReplicaSets are managed based on the revisionHistoryLimit.

Running the example:

  1. Save the deployment.yaml file.
  2. Save the update_deployment.sh script and make it executable with chmod +x update_deployment.sh.
  3. Apply the Deployment: kubectl apply -f deployment.yaml
  4. Run the update script: ./update_deployment.sh

Output:

The script will output the list of ReplicaSets after each update. You will observe that Kubernetes keeps a maximum of 3 old ReplicaSets, deleting older ones as new updates are applied.

Important Note:

While this example demonstrates how revisionHistoryLimit works, remember that directly deleting ReplicaSets is not recommended. Let Kubernetes manage them for rollback functionality and history. If you encounter issues with old ReplicaSets not being cleaned up, review your Deployment configuration and investigate potential conflicts within your cluster.

Additional Notes

Understanding the Relationship:

  • Deployments are like blueprints for your desired state. They tell Kubernetes what you want running, not how to run it.
  • ReplicaSets are the workers that translate the Deployment's blueprint into actual Pods. They handle scaling and ensuring the desired number of Pods exist.
  • Revision History is managed through old ReplicaSets. This allows for easy rollbacks to previous versions of your application.

Revision History Limit:

  • Purpose: Prevents unlimited accumulation of old ReplicaSets, which can consume resources and clutter your cluster.
  • Trade-off: A lower limit saves resources but reduces rollback options. A higher limit provides more rollback flexibility but uses more storage.
  • Best Practices:
    • Start with a reasonable default (e.g., 3-5) and adjust based on your application's update frequency and rollback needs.
    • Consider external tools for long-term historical data if you need to retain information beyond the limit.

When to Investigate Potential Issues:

  • **Old ReplicaSets persisting despite revisionHistoryLimit: **
    • Verify the Deployment configuration is applied correctly (check for typos, syntax errors).
    • Investigate potential conflicts with other controllers or custom resource definitions (CRDs) that might be managing the ReplicaSets.
    • Check for errors or warnings in the Deployment and ReplicaSet events.
  • Resource constraints: Ensure your cluster has enough resources to create new ReplicaSets and Pods during updates. Insufficient resources can lead to unexpected behavior.

Important Considerations:

  • Directly deleting ReplicaSets is generally discouraged. Kubernetes uses them for rollbacks and history. Manual deletion can disrupt these functionalities.
  • Resource cleanup: While Kubernetes manages ReplicaSets, consider periodically cleaning up unused Deployments and other resources to maintain a clean and efficient cluster.
  • Monitoring: Monitor your Deployments and ReplicaSets to ensure they are functioning as expected and to identify any potential issues early on.

By understanding the interplay between Deployments, ReplicaSets, and revision history, you can effectively manage your application deployments and rollbacks in Kubernetes.

Summary

Feature Description
ReplicaSet Management Deployments utilize ReplicaSets to manage Pods. Updates involve creating new ReplicaSets, scaling them up, and scaling down old ones.
Revision History Limit Control the number of old ReplicaSets retained using .spec.revisionHistoryLimit in the Deployment definition.
Example: spec: revisionHistoryLimit: 3 (Keeps a maximum of 3 old ReplicaSets)
Setting to 0 spec: revisionHistoryLimit: 0 (Removes all old ReplicaSets immediately)
Caution Directly deleting ReplicaSets is discouraged as they are crucial for rollback and history functionalities.
Troubleshooting If old ReplicaSets persist, verify the Deployment configuration and check for conflicts within the cluster.

Conclusion

Kubernetes Deployments and their management of ReplicaSets are fundamental for deploying and updating applications. By using revision history limits, you can control the footprint of old ReplicaSets. Remember that directly deleting ReplicaSets is discouraged as it can interfere with Kubernetes' rollback and history mechanisms. If you encounter issues with old ReplicaSets not being cleaned up, carefully review your Deployment configuration and investigate potential conflicts within your cluster. Understanding these components and their interactions is crucial for maintaining a healthy and efficient Kubernetes environment.

References

Were You Able to Follow the Instructions?

šŸ˜Love it!
šŸ˜ŠYes
šŸ˜Meh-gical
šŸ˜žNo
šŸ¤®Clickbait