English | 简体中文 |
kube-recycle-bin(krb) is a Kubernetes resource recycling bin that can automatically recycle and quickly restore deleted resources.
In Kubernetes, resource deletion is an irreversible operation. While there are methods like Velero or etcd backup/restore that can help us recover deleted resources, have you ever felt that in practical scenarios, “using a sledgehammer to crack a nut” is excessive?
Then try kube-recycle-bin!
Since it is a recycling bin, its main functions are:
krb-cli recycle
command to create a RecyclePolicy
resource, specifying the resource types and namespaces to be recycled.krb-controller
watching for the creation, update, and deletion of RecyclePolicy
resources, automatically synchronizing the creation, update, and deletion of corresponding ValidatingWebhookConfiguration
resources.kube-apiserver
receives the deletion request for the specified resource and forwards the request to krb-webhook
through ValidatingWebhookConfiguration
.krb-webhook
parses the request and stores the deleted resource (in JSON format) into a new RecycleItem
resource object, completing the resource recycling.krb-cli restore
command to restore the recycled resource. After the resource is restored, the RecycleItem
resource object is automatically deleted.kubectl apply -f https://raw.githubusercontent.com/ketches/kube-recycle-bin/master/manifests/crds.yaml
krb-controller
and krb-webhook
kubectl apply -f https://raw.githubusercontent.com/ketches/kube-recycle-bin/master/manifests/deploy.yaml
Multiple installation methods are available:
go install
command:go install github.com/ketches/kube-recycle-bin/cmd/krb-cli@latest
curl -sSL https://github.com/ketches/kube-recycle-bin/raw/master/install_cli.sh | sh
Download the corresponding binary file for your operating system from the Release page, extract it, and move krb-cli
to a directory in your $PATH
.
Install from source code:
git clone https://github.com/ketches/kube-recycle-bin.git
cd kube-recycle-bin
make install
Note: The prerequisite is that krb-controller
and krb-webhook
have been successfully deployed.
Scenario: Automatically recycle deleted Deployment
, StatefulSet
, and Service
resources in the dev
and prod
namespaces.
# Create recycle policies
krb-cli recycle deployments statefulsets services -n dev,prod
# Check the created recycle policies
kubectl get rp
# First, create test resources
kubectl create deployment krb-test-nginx-deploy --image=nginx --replicas=0 -n dev
kubectl expose deployment krb-test-nginx-deploy --name krb-test-nginx-svc --port=80 --target-port=80 -n dev
# Delete test resources
kubectl delete deploy krb-test-nginx-deploy -n dev
kubectl delete svc krb-test-nginx-svc -n dev
# Check the recycle bin. Execute the following command to get the deleted resources, indicating that the recycling policy has taken effect.
krb-cli get ri
# View the recycled resource objects using the resource name obtained from the above command
krb-cli view krb-test-nginx-deploy-skk5c89b krb-test-nginx-svc-txv4vj6v
# Restore the recycled resource objects
krb-cli restore krb-test-nginx-deploy-skk5c89b krb-test-nginx-svc-txv4vj6v
# Check the restored resources
kubectl get deploy krb-test-nginx-deploy -n dev
kubectl get svc krb-test-nginx-svc -n dev