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 recyclecommand to create aRecyclePolicyresource, specifying the resource types and namespaces to be recycled. - The
krb-controllerwatching for the creation, update, and deletion ofRecyclePolicyresources, automatically synchronizing the creation, update, and deletion of correspondingValidatingWebhookConfigurationresources. - The
kube-apiserverreceives the deletion request for the specified resource and forwards the request tokrb-webhookthroughValidatingWebhookConfiguration. - The
krb-webhookparses the request and stores the deleted resource (in JSON format) into a newRecycleItemresource object, completing the resource recycling. - Use the
krb-cli restorecommand to restore the recycled resource. After the resource is restored, theRecycleItemresource object is automatically deleted.
- Install CRDs
kubectl apply -f https://raw.githubusercontent.com/ketches/kube-recycle-bin/master/manifests/crds.yaml- Deploy
krb-controllerandkrb-webhook
kubectl apply -f https://raw.githubusercontent.com/ketches/kube-recycle-bin/master/manifests/deploy.yamlMultiple installation methods are available:
- Install using
go installcommand:
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-clito a directory in your$PATH. -
Install from source code:
git clone https://github.com/ketches/kube-recycle-bin.git
cd kube-recycle-bin
make installNote: 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