Learn how to revert changes made by "kubectl apply" and restore your Kubernetes resources to a previous state.
There is no single command to directly reverse the effects of kubectl apply -f <file>
. Kubernetes operates on a desired state model, aiming to align the cluster's state with your configuration files. Instead of a direct "undo," you'll use different approaches depending on what you want to achieve. Let's explore how to handle various "undo" scenarios in Kubernetes.
There is no direct "reverse" for kubectl apply -f <file>
. Kubernetes aims to converge the desired state (your config) with the actual state (the cluster).
Here's how to handle different "undo" scenarios:
Removing resources:
kubectl delete -f <file>
apply
.Rolling back deployments:
kubectl rollout undo deployment/<deployment-name>
kubectl rollout undo deployment/<deployment-name> --to-revision=3
Uninstalling complex applications:
No inverse for individual resources:
kubectl apply
doesn't track history for individual resource changes.Important Considerations:
This document provides examples of how to undo kubectl apply actions in various scenarios. It covers removing resources defined in a YAML file, rolling back deployments to previous revisions, uninstalling Helm charts, and addressing the lack of a direct undo for individual resource changes like scaling. The document emphasizes the importance of careful deployment planning and having a rollback strategy.
This example demonstrates different "undo" scenarios for kubectl apply
.
Scenario: You have a file named deployment.yaml
that defines a Deployment and a Service:
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: nginx:latest
---
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 80
1. Removing Resources:
# Apply the resources
kubectl apply -f deployment.yaml
# Delete the resources defined in the file
kubectl delete -f deployment.yaml
2. Rolling Back a Deployment:
# Assume you've updated the deployment.yaml and applied it multiple times.
# Roll back to the previous deployment revision
kubectl rollout undo deployment/my-app
# Roll back to a specific revision (e.g., revision 3)
kubectl rollout undo deployment/my-app --to-revision=3
3. Uninstalling a Helm Chart (Example):
# Assuming you installed an app called "my-chart" using Helm
# Uninstall the Helm chart
helm uninstall my-chart
4. No Inverse for Individual Resource Changes:
# Let's say you manually scaled the deployment using:
kubectl scale deployment my-app --replicas=5
# There's no direct "undo" for this specific command.
# To revert, modify the replicas count in deployment.yaml back to 3 and re-apply:
# Edit deployment.yaml and set replicas: 3
# Re-apply the configuration
kubectl apply -f deployment.yaml
Important: This is a simplified example. Real-world scenarios might involve more complex resources and dependencies. Always plan your deployments carefully and have a rollback strategy in place.
kubectl apply
is idempotent. Reapplying the same configuration multiple times won't have additional side effects beyond reaching the desired state.kubectl diff -f <file>
before applying changes. This shows you a preview of what will be added, modified, or deleted.kubectl delete -f <file> --dry-run=client
or --dry-run=server
to simulate the operation without actually deleting anything.-n <namespace>
flag with kubectl delete
to remove them from the correct namespace.Scenario | Solution | Notes |
---|---|---|
Remove resources defined in a file | kubectl delete -f <file> |
Effectively undoes the apply command. |
Rollback a deployment |
kubectl rollout undo deployment/<deployment-name> kubectl rollout undo deployment/<deployment-name> --to-revision=3
|
Reverts to a previous deployment version. Can specify a specific revision. |
Uninstall complex applications (Helm, etc.) | Use the package manager's uninstall mechanism. | |
Undo changes to individual resources | No direct inverse. Modify the configuration file to the desired state and re-apply with kubectl apply -f <file> . |
kubectl apply doesn't track individual resource change history. |
Key Points:
In conclusion, Kubernetes doesn't offer a single "undo" command for kubectl apply -f <file>
. Its declarative approach focuses on achieving the desired state outlined in your configuration files. To reverse changes, you'll need to adopt different strategies based on the specific scenario: deleting resources with kubectl delete -f <file>
, rolling back deployments to previous versions, or leveraging package managers' uninstall mechanisms. Remember that understanding resource dependencies, focusing on the end state, and having backups are crucial for successful rollbacks and configuration management in Kubernetes. By mastering these concepts and techniques, you can confidently manage your applications and navigate the intricacies of state reconciliation in Kubernetes.
apply -f
- Stack ... | Sep 13, 2019 ... I have applied a config file like this kubectl apply -f deploy/mysql.yml How can I unapply this specific config?