|
1 | 1 | # kube-recycle-bin
|
2 |
| -A recycle bin for deleted Kubernetes resources. |
| 2 | + |
| 3 | +English | [简体中文](README_zh-CN.md) |
| 4 | + |
| 5 | +kube-recycle-bin(krb) is a Kubernetes resource recycling bin that can automatically recycle and quickly restore deleted resources. |
| 6 | + |
| 7 | +> 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? |
| 8 | +> |
| 9 | +> Then try kube-recycle-bin! |
| 10 | +
|
| 11 | +## Features |
| 12 | + |
| 13 | +Since it is a recycling bin, its main functions are: |
| 14 | + |
| 15 | +1. Recycle: Supports recycling all Kubernetes resource types and allows specifying namespaces. |
| 16 | +2. Restore: 100% restoration of recycled resources. |
| 17 | + |
| 18 | +## Principle |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +1. Use the `krb-cli recycle` command to create a `RecyclePolicy` resource, specifying the resource types and namespaces to be recycled. |
| 23 | +2. The `krb-controller` watching for the creation, update, and deletion of `RecyclePolicy` resources, automatically synchronizing the creation, update, and deletion of corresponding `ValidatingWebhookConfiguration` resources. |
| 24 | +3. The `kube-apiserver` receives the deletion request for the specified resource and forwards the request to `krb-webhook` through `ValidatingWebhookConfiguration`. |
| 25 | +4. The `krb-webhook` parses the request and stores the deleted resource (in JSON format) into a new `RecycleItem` resource object, completing the resource recycling. |
| 26 | +5. Use the `krb-cli restore` command to restore the recycled resource. After the resource is restored, the `RecycleItem` resource object is automatically deleted. |
| 27 | + |
| 28 | +## Deploy |
| 29 | + |
| 30 | +1. Install CRDs |
| 31 | + |
| 32 | +```bash |
| 33 | +kubectl apply -f https://raw.githubusercontent.com/ketches/kube-recycle-bin/master/manifests/crds.yaml |
| 34 | +``` |
| 35 | + |
| 36 | +2. Deploy `krb-controller` and `krb-webhook` |
| 37 | + |
| 38 | +```bash |
| 39 | + |
| 40 | +kubectl apply -f https://raw.githubusercontent.com/ketches/kube-recycle-bin/master/manifests/deploy.yaml |
| 41 | +``` |
| 42 | + |
| 43 | +## Install CLI |
| 44 | + |
| 45 | +Multiple installation methods are available: |
| 46 | + |
| 47 | +1. Install using `go install` command: |
| 48 | + |
| 49 | +```bash |
| 50 | +go install github.com/ketches/kube-recycle-bin/cmd/krb-cli@latest |
| 51 | +``` |
| 52 | + |
| 53 | +2. Use the script (suitable for Linux and MacOS): |
| 54 | + |
| 55 | +```bash |
| 56 | +curl -sSL https://github.com/ketches/kube-recycle-bin/raw/master/install_cli.sh | sh |
| 57 | +``` |
| 58 | + |
| 59 | +3. Download the corresponding binary file for your operating system from the [Release](https://github.com/ketches/kube-recycle-bin/releases) page, extract it, and move `krb-cli` to a directory in your `$PATH`. |
| 60 | + |
| 61 | +4. Install from source code: |
| 62 | + |
| 63 | +```bash |
| 64 | +git clone https://github.com/ketches/kube-recycle-bin.git |
| 65 | +cd kube-recycle-bin |
| 66 | +make install |
| 67 | +``` |
| 68 | + |
| 69 | +## Guide |
| 70 | + |
| 71 | +Note: The prerequisite is that `krb-controller` and `krb-webhook` have been successfully deployed. |
| 72 | + |
| 73 | +场景:自动回收 `dev`, `prod` 命名空间下删除的 `Deployment`, `StatefulSet` 和 `Service` 资源。 |
| 74 | +Scenario: Automatically recycle deleted `Deployment`, `StatefulSet`, and `Service` resources in the `dev` and `prod` namespaces. |
| 75 | + |
| 76 | +1. Create a recycling policy |
| 77 | + |
| 78 | +```bash |
| 79 | +krb-cli recycle deployments statefulsets services -n dev,prod |
| 80 | +``` |
| 81 | + |
| 82 | +1. Restore the recycled resource |
| 83 | + |
| 84 | +```bash |
| 85 | +# First, create test resources |
| 86 | +kubectl create deployment krb-test-nginx-deploy --image=nginx --replicas=0 -n dev |
| 87 | +kubectl expose deployment krb-test-nginx-deploy --name krb-test-nginx-svc --port=80 --target-port=80 -n dev |
| 88 | + |
| 89 | +# Delete test resources |
| 90 | +kubectl delete deploy krb-test-nginx-deploy -n dev |
| 91 | +kubectl delete svc krb-test-nginx-svc -n dev |
| 92 | + |
| 93 | +# Check the recycle bin. Execute the following command to get the deleted resources, indicating that the recycling policy has taken effect. |
| 94 | +krb-cli get ri |
| 95 | + |
| 96 | +# Restore the resource using the resource name obtained from the above command |
| 97 | +krb-cli restore krb-test-nginx-deploy-skk5c89b krb-test-nginx-svc-txv4vj6v |
| 98 | + |
| 99 | +# Check the restored resources |
| 100 | +kubectl get deploy krb-test-nginx-deploy -n dev |
| 101 | +kubectl get svc krb-test-nginx-svc -n dev |
| 102 | +``` |
0 commit comments