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:
- Recycle: Supports recycling all Kubernetes resource types and allows specifying namespaces.
- Restore: 100% restoration of recycled resources.
- Use the
krb-cli recycle
command to create aRecyclePolicy
resource, specifying the resource types and namespaces to be recycled. - The
krb-controller
watching for the creation, update, and deletion ofRecyclePolicy
resources, automatically synchronizing the creation, update, and deletion of correspondingValidatingWebhookConfiguration
resources. - The
kube-apiserver
receives the deletion request for the specified resource and forwards the request tokrb-webhook
throughValidatingWebhookConfiguration
. - The
krb-webhook
parses the request and stores the deleted resource (in JSON format) into a newRecycleItem
resource object, completing the resource recycling. - Use the
krb-cli restore
command to restore the recycled resource. After the resource is restored, theRecycleItem
resource object is automatically deleted.
- Install CRDs
kubectl apply -f https://github.com/raw/ketches/kube-recycle-bin/master/manifests/crds.yaml
- Deploy
krb-controller
andkrb-webhook
kubectl apply -f https://github.com/raw/ketches/kube-recycle-bin/master/manifests/deploy.yaml
Multiple installation methods are available:
- Install using
go install
command:
go install github.com/ketches/kube-recycle-bin/cmd/krb-cli@latest
- Use the script (suitable for Linux and MacOS):
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 a recycling policy
# Create recycle policies
krb-cli recycle deployments statefulsets services -n dev,prod
# Check the created recycle policies
kubectl get rp
- Restore the recycled resource
# 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