Skip to content

Commit

Permalink
Fix Deletion Policy Halt
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Raisul Islam Evan <[email protected]>
  • Loading branch information
Evanraisul committed Sep 25, 2024
1 parent da6b509 commit 38a6db2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 45 deletions.
5 changes: 1 addition & 4 deletions docs/guides/memcached/cli/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ spec:
cpu: 250m
memory: 64Mi
replicas: 1
deletionPolicy: Halt
deletionPolicy: WipeOut
version: 1.6.22
status:
observedGeneration: 1$7916315637361465932
Expand Down Expand Up @@ -115,7 +115,6 @@ Flag `--output=wide` is used to print additional information.
List command supports short names for each object types. You can use it like `kubectl get <short-name>`. Below are the short name for KubeDB objects:

- Memcached: `mc`
- DormantDatabase: `drmn`

You can print labels with objects. The following command will list all Memcached with their corresponding labels.

Expand Down Expand Up @@ -249,8 +248,6 @@ If Deployment exists for a Memcached server, following fields can't be modified
- spec.podTemplate.spec.nodeSelector
- spec.podTemplate.spec.env

For DormantDatabase, `spec.origin` can't be edited using `kubectl edit`

### How to Delete Objects

`kubectl delete` command will delete an object in `default` namespace by default unless namespace is provided. The following command will delete a Memcached `memcached-dev` in default namespace
Expand Down
18 changes: 7 additions & 11 deletions docs/guides/memcached/concepts/memcached.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ spec:
ports:
- name: http
port: 9200
deletionPolicy: Halt
deletionPolicy: Delete
```
### spec.replicas
Expand Down Expand Up @@ -241,26 +241,22 @@ See [here](https://github.com/kmodules/offshoot-api/blob/kubernetes-1.16.3/api/v
`deletionPolicy` gives flexibility whether to `nullify`(reject) the delete operation of `Memcached` crd or which resources KubeDB should keep or delete when you delete `Memcached` crd. KubeDB provides following four termination policies:

- DoNotTerminate
- Halt
- Delete (`Default`)
- WipeOut

When `deletionPolicy` is `DoNotTerminate`, KubeDB takes advantage of `ValidationWebhook` feature in Kubernetes 1.9.0 or later clusters to implement `DoNotTerminate` feature. If admission webhook is enabled, `DoNotTerminate` prevents users from deleting the database as long as the `spec.deletionPolicy` is set to `DoNotTerminate`.

Following table show what KubeDB does when you delete Memcached crd for different termination policies,

| Behavior | DoNotTerminate | Halt | Delete | WipeOut |
| ----------------------------------- | :------------: | :------: | :------: | :------: |
| 1. Block Delete operation | &#10003; | &#10007; | &#10007; | &#10007; |
| 2. Delete PetSet | &#10007; | &#10003; | &#10003; | &#10003; |
| 3. Delete Services | &#10007; | &#10003; | &#10003; | &#10003; |
| 4. Delete Secrets | &#10007; | &#10007; | &#10007; | &#10003; |
| Behavior | DoNotTerminate | Delete | WipeOut |
| ---------------------------| :------------: | :------: | :------: |
| 1. Block Delete operation | &#10003; | &#10007; | &#10007; |
| 2. Delete PetSet | &#10007; | &#10003; | &#10003; |
| 3. Delete Services | &#10007; | &#10003; | &#10003; |
| 4. Delete Secrets | &#10007; | &#10007; | &#10003; |

If you don't specify `spec.deletionPolicy` KubeDB uses `Delete` termination policy by default.

### spec.halted
Indicates that the database is halted and all offshoot Kubernetes resources except PVCs are deleted.

## spec.helathChecker
It defines the attributes for the health checker.
- spec.healthChecker.periodSeconds specifies how often to perform the health check.
Expand Down
67 changes: 37 additions & 30 deletions docs/guides/memcached/quickstart/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ status:
phase: Ready

```
## Connect with Memcached database

Now, you can connect to this database using `telnet`.
Here, we will connect to Memcached server from local-machine through port-forwarding.
Expand Down Expand Up @@ -365,63 +366,69 @@ END
quit
```

## DoNotTerminate Property
## Database DeletionPolicy

When `deletionPolicy` is `DoNotTerminate`, KubeDB takes advantage of `ValidationWebhook` feature in Kubernetes 1.9.0 or later clusters to implement `DoNotTerminate` feature. If admission webhook is enabled, It prevents users from deleting the database as long as the `spec.deletionPolicy` is set to `DoNotTerminate`. You can see this below:
This field is used to regulate the deletion process of the related resources when `Memcached` object is deleted. User can set the value of this field according to their needs. The available options and their use case scenario is described below:

**DoNotTerminate:**

When `deletionPolicy` is set to `DoNotTerminate`, KubeDB takes advantage of `ValidationWebhook` feature in Kubernetes 1.9.0 or later clusters to implement `DoNotTerminate` feature. If admission webhook is enabled, It prevents users from deleting the database as long as the `spec.deletionPolicy` is set to `DoNotTerminate`. You can see this below:

```bash
$ kubectl delete mc memcd-quickstart -n demo
Error from server (Forbidden): admission webhook "memcachedwebhook.validators.kubedb.com" denied the request: memcached demo/memcd-quickstart is can't terminated. To delete, change spec.deletionPolicy
```
Learn details of all `DeletionPolicy` [here](/docs/guides/memcached/concepts/memcached.md#specdeletionpolicy).
Now, run `kubectl edit mc memcd-quickstart -n demo` to set `spec.deletionPolicy` to `Halt`. Then you will be able to delete/halt the database.
Learn details of all `DeletionPolicy` [here](/docs/guides/memcached/concepts/memcached.md#specdeletionpolicy)
**Delete:**
## Halt Database
If you want to delete the existing database but want to keep `secrets` then you might want to set the `Memcached` object `deletionPolicy` to `Delete`. In this setting, `PetSet` and `Services` will be deleted.
When [DeletionPolicy](/docs/guides/memcached/concepts/memcached.md#specdeletionpolicy) is set to `Halt` and you delete the memcached object, the KubeDB operator will delete the PetSet and its pods but leaves the PVCs, secrets and database backup (snapshots) intact. Learn details of all `DeletionPolicy` [here](/docs/guides/memcached/concepts/memcached.md#specdeletionpolicy).
When the [DeletionPolicy](/docs/guides/mysql/concepts/database/index.md#specdeletionpolicy) is set to `Delete` and the MySQL object is deleted, the KubeDB operator will delete the PetSet and its pods but leaves the `secret` intact.
You can also keep the memcached object and halt the database to resume it again later. If you halt the database, the KubeDB operator will delete the petsets and services but will keep the memcached object, pvcs, secrets and backup (snapshots).
To halt the database, first you have to set the deletionPolicy to `Halt` in existing database. You can use the below command to set the deletionPolicy to `Halt`, if it is not already set.
Suppose, we have a database with `deletionPolicy` set to `Delete`. Now, are going to delete the database using the following command:
```bash
$ kubectl patch -n demo mc/memcd-quickstart -p '{"spec":{"deletionPolicy":"Halt"}}' --type="merge"
memcached.kubedb.com/memcd-quickstart patched
$ kubectl delete -n demo mc/memcd-quickstart
memcached.kubedb.com "memcd-quickstart" deleted
```
Then, you have to set the `spec.halted` as true to set the database in a `Halted` state. You can use the below command.
Now, run the following command to get all memcached resources in `demo` namespaces,
```bash
$ kubectl patch -n demo mc/memcd-quickstart -p '{"spec":{"halted":true}}' --type="merge"
memcached.kubedb.com/memcd-quickstart patched
$ kubectl get sts,svc,secret,pvc -n demo
NAME TYPE DATA AGE
auth-secret Opaque 1 3h
mc-configuration Opaque 1 3h
```
After that, kubedb will delete the petsets and services, and you can see the database Phase as `Halted`. You can see the bellow command.
From the above output, you can see that all memcached resources(`PetSet`, `Service` etc.) are deleted except `Secret`.
```bash
$ kubectl get memcached -n demo
NAME VERSION STATUS AGE
memcd-quickstart 1.6.22 Halted 91s
```
>If you don't set the `deletionPolicy` then the kubeDB set the DeletionPolicy to `Delete` by-default.

## Resume Halted Memcached
Now, to resume the database, i.e. to get the same database setup back again, you have to set the `spec.halted` as false. You can use the below command.
**WipeOut:**

```bash
$ kubectl patch -n demo mc/memcd-quickstart -p '{"spec":{"halted":false}}' --type="merge"
memcached.kubedb.com/memcd-quickstart patched
You can totally delete the `Memcached` database and relevant resources without any tracking by setting `deletionPolicy` to `WipeOut`. KubeDB operator will delete all relevant resources of this `Memcached` database (i.e, `PetSet`, `Secrets`, `Services`) when the `deletionPolicy` is set to `WipeOut`.

Suppose, we have a database with `deletionPolicy` set to `WipeOut`. Now, are going to delete the database using the following command:

```yaml
$ kubectl delete -n demo mc/memcd-quickstart
memcached.kubedb.com "memcd-quickstart" deleted
```

When the database is resumed successfully, you can see the database Status is set to `Ready`.
Now, run the following command to get all memcached resources in `demo` namespaces,

```bash
$ kc get memcached -n demo
NAME VERSION STATUS AGE
memcd-quickstart 1.6.22 Ready 30m
$ kubectl get sts,svc,secret -n demo
No resources found in demo namespace.
```

From the above output, you can see that all memcached resources are deleted. there is no option to recreate/reinitialize your database if `deletionPolicy` is set to `Delete`.

>Be careful when you set the `deletionPolicy` to `Delete`. Because there is no option to trace the database resources if once deleted the database.


## Cleaning up

To cleanup the Kubernetes resources created by this tutorial, run:
Expand Down

0 comments on commit 38a6db2

Please sign in to comment.