2017-02-22 03:12:47 +00:00
|
|
|
---
|
|
|
|
title: Garbage Collection
|
2017-03-16 20:58:07 +00:00
|
|
|
redirect_from:
|
|
|
|
- "/docs/concepts/abstractions/controllers/garbage-collection/"
|
|
|
|
- "/docs/concepts/abstractions/controllers/garbage-collection.html"
|
2017-04-25 03:35:01 +00:00
|
|
|
- "/docs/user-guide/garbage-collection/"
|
|
|
|
- "/docs/user-guide/garbage-collection.html"
|
|
|
|
|
2017-02-22 03:12:47 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
{% capture overview %}
|
|
|
|
|
|
|
|
The role of the Kubernetes garbage collector is to delete certain objects
|
|
|
|
that once had an owner, but no longer have an owner.
|
|
|
|
|
|
|
|
**Note**: Garbage collection is a beta feature and is enabled by default in
|
|
|
|
Kubernetes version 1.4 and later.
|
|
|
|
|
|
|
|
{% endcapture %}
|
|
|
|
|
|
|
|
|
|
|
|
{% capture body %}
|
|
|
|
|
|
|
|
## Owners and dependents
|
|
|
|
|
|
|
|
Some Kubernetes objects are owners of other objects. For example, a ReplicaSet
|
|
|
|
is the owner of a set of Pods. The owned objects are called *dependents* of the
|
|
|
|
owner object. Every dependent object has a `metadata.ownerReferences` field that
|
|
|
|
points to the owning object.
|
|
|
|
|
|
|
|
Sometimes, Kubernetes sets the value of `ownerReference` automatically. For
|
|
|
|
example, when you create a ReplicaSet, Kubernetes automatically sets the
|
2017-03-17 00:37:26 +00:00
|
|
|
`ownerReference` field of each Pod in the ReplicaSet. In 1.6, Kubernetes
|
2017-03-18 00:42:04 +00:00
|
|
|
automatically sets the value of `ownerReference` for objects created or adopted
|
|
|
|
by ReplicationController, ReplicaSet, StatefulSet, DaemonSet, and Deployment.
|
2017-03-17 00:37:26 +00:00
|
|
|
|
|
|
|
You can also specify relationships between owners and dependents by manually
|
|
|
|
setting the `ownerReference` field.
|
2017-02-22 03:12:47 +00:00
|
|
|
|
|
|
|
Here's a configuration file for a ReplicaSet that has three Pods:
|
|
|
|
|
2017-03-16 20:58:07 +00:00
|
|
|
{% include code.html language="yaml" file="my-repset.yaml" ghlink="/docs/concepts/workloads/controllers/my-repset.yaml" %}
|
2017-02-22 03:12:47 +00:00
|
|
|
|
|
|
|
If you create the ReplicaSet and then view the Pod metadata, you can see
|
|
|
|
OwnerReferences field:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
kubectl create -f http://k8s.io/docs/concepts/abstractions/controllers/my-repset.yaml
|
|
|
|
kubectl get pods --output=yaml
|
|
|
|
```
|
|
|
|
|
|
|
|
The output shows that the Pod owner is a ReplicaSet named my-repset:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
apiVersion: v1
|
|
|
|
kind: Pod
|
|
|
|
metadata:
|
|
|
|
...
|
|
|
|
ownerReferences:
|
|
|
|
- apiVersion: extensions/v1beta1
|
|
|
|
controller: true
|
2017-03-17 00:37:26 +00:00
|
|
|
blockOwnerDeletion: true
|
2017-02-22 03:12:47 +00:00
|
|
|
kind: ReplicaSet
|
|
|
|
name: my-repset
|
|
|
|
uid: d9607e19-f88f-11e6-a518-42010a800195
|
|
|
|
...
|
|
|
|
```
|
|
|
|
|
2017-03-22 20:28:37 +00:00
|
|
|
## Controlling how the garbage collector deletes dependents
|
|
|
|
|
|
|
|
When you delete an object, you can specify whether the object's dependents are
|
|
|
|
also deleted automatically. Deleting dependents automatically is called *cascading
|
|
|
|
deletion*. There are two modes of *cascading deletion*: *background* and *foreground*.
|
|
|
|
|
|
|
|
If you delete an object without deleting its dependents
|
|
|
|
automatically, the dependents are said to be *orphaned*.
|
|
|
|
|
|
|
|
### Background cascading deletion
|
|
|
|
|
|
|
|
In *background cascading deletion*, Kubernetes deletes the owner object
|
|
|
|
immediately and the garbage collector then deletes the dependents in
|
|
|
|
the background.
|
|
|
|
|
|
|
|
### Foreground cascading deletion
|
|
|
|
|
|
|
|
In *foreground cascading deletion*, the root object first
|
|
|
|
enters a "deletion in progress" state. In the "deletion in progress" state,
|
|
|
|
the following things are true:
|
|
|
|
|
|
|
|
* The object is still visible via the REST API
|
|
|
|
* The object's `deletionTimestamp` is set
|
|
|
|
* The object's `metadata.finalizers` contains the value "foregroundDeletion".
|
|
|
|
|
2017-03-27 01:25:03 +00:00
|
|
|
Once the "deletion in progress" state is set, the garbage
|
2017-03-22 20:28:37 +00:00
|
|
|
collector deletes the object's dependents. Once the garbage collector has deleted all
|
2017-03-27 01:25:03 +00:00
|
|
|
"blocking" dependents (objects with `ownerReference.blockOwnerDeletion=true`), it delete
|
2017-03-22 20:28:37 +00:00
|
|
|
the owner object.
|
2017-03-17 00:37:26 +00:00
|
|
|
|
2017-03-18 00:42:04 +00:00
|
|
|
Note that in the "foregroundDeletion", only dependents with
|
2017-03-22 20:28:37 +00:00
|
|
|
`ownerReference.blockOwnerDeletion` block the deletion of the owner object.
|
2017-03-27 01:25:03 +00:00
|
|
|
Kubernetes version 1.7 will add an admission controller that controls user access to set
|
2017-03-22 20:28:37 +00:00
|
|
|
`blockOwnerDeletion` to true based on delete permissions on the owner object, so that
|
|
|
|
unauthorized dependents cannot delay deletion of an owner object.
|
|
|
|
|
|
|
|
If an object's `ownerReferences` field is set by a controller (such as Deployment or ReplicaSet),
|
|
|
|
blockOwnerDeletion is set automatically and you do not need to manually modify this field.
|
|
|
|
|
|
|
|
### Setting the cascading deletion policy
|
2017-03-18 00:42:04 +00:00
|
|
|
|
2017-03-22 20:28:37 +00:00
|
|
|
To control the cascading deletion policy, set the `deleteOptions.propagationPolicy`
|
|
|
|
field on your owner object. Possible values include "Orphan",
|
|
|
|
"Foregound", or "Background".
|
2017-03-17 00:37:26 +00:00
|
|
|
|
2017-03-18 00:42:04 +00:00
|
|
|
The default garbage collection policy for many controller resources is `orphan`,
|
|
|
|
including ReplicationController, ReplicaSet, StatefulSet, DaemonSet, and
|
|
|
|
Deployment. So unless you specify otherwise, dependent objects are orphaned.
|
2017-02-22 03:12:47 +00:00
|
|
|
|
2017-03-17 00:37:26 +00:00
|
|
|
Here's an example that deletes dependents in background:
|
2017-02-22 03:12:47 +00:00
|
|
|
|
2017-03-17 00:37:26 +00:00
|
|
|
```shell
|
|
|
|
kubectl proxy --port=8080
|
|
|
|
curl -X DELETE localhost:8080/apis/extensions/v1beta1/namespaces/default/replicasets/my-repset \
|
|
|
|
-d '{"kind":"DeleteOptions","apiVersion":"v1","propagationPolicy":"Background"}' \
|
|
|
|
-H "Content-Type: application/json"
|
|
|
|
```
|
2017-02-22 03:12:47 +00:00
|
|
|
|
2017-03-17 00:37:26 +00:00
|
|
|
Here's an example that deletes dependents in foreground:
|
2017-02-22 03:12:47 +00:00
|
|
|
|
2017-03-17 00:37:26 +00:00
|
|
|
```shell
|
|
|
|
kubectl proxy --port=8080
|
|
|
|
curl -X DELETE localhost:8080/apis/extensions/v1beta1/namespaces/default/replicasets/my-repset \
|
|
|
|
-d '{"kind":"DeleteOptions","apiVersion":"v1","propagationPolicy":"Foreground"}' \
|
|
|
|
-H "Content-Type: application/json"
|
|
|
|
```
|
2017-02-22 03:12:47 +00:00
|
|
|
|
2017-03-17 00:37:26 +00:00
|
|
|
Here's an example that orphans dependents:
|
2017-02-22 03:12:47 +00:00
|
|
|
|
|
|
|
```shell
|
|
|
|
kubectl proxy --port=8080
|
2017-03-17 00:37:26 +00:00
|
|
|
curl -X DELETE localhost:8080/apis/extensions/v1beta1/namespaces/default/replicasets/my-repset \
|
|
|
|
-d '{"kind":"DeleteOptions","apiVersion":"v1","propagationPolicy":"Orphan"}' \
|
|
|
|
-H "Content-Type: application/json"
|
2017-02-22 03:12:47 +00:00
|
|
|
```
|
|
|
|
|
2017-03-22 20:28:37 +00:00
|
|
|
kubectl also supports cascading deletion.
|
2017-03-17 00:37:26 +00:00
|
|
|
To delete dependents automatically using kubectl, set `--cascade` to true. To
|
|
|
|
orphan dependents, set `--cascade` to false. The default value for `--cascade`
|
|
|
|
is true.
|
2017-02-22 03:12:47 +00:00
|
|
|
|
|
|
|
Here's an example that orphans the dependents of a ReplicaSet:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
kubectl delete replicaset my-repset --cascade=false
|
|
|
|
```
|
|
|
|
|
2017-03-17 00:37:26 +00:00
|
|
|
## Known issues
|
|
|
|
* In 1.6, garbage collection does not support non-core resources, e.g.,
|
|
|
|
resources added via ThirdPartyResource or via aggregated API servers. It will
|
|
|
|
support non-core resources in the future. When it does, garbage collector will
|
|
|
|
delete objects with ownerRefereneces referring to non-existent object of a
|
|
|
|
valid non-core resource.
|
2017-02-22 03:12:47 +00:00
|
|
|
|
2017-03-17 00:37:26 +00:00
|
|
|
[Other known issues](https://github.com/kubernetes/kubernetes/issues/26120)
|
2017-02-22 03:12:47 +00:00
|
|
|
|
|
|
|
{% endcapture %}
|
|
|
|
|
|
|
|
|
|
|
|
{% capture whatsnext %}
|
|
|
|
|
2017-03-17 00:37:26 +00:00
|
|
|
[Design Doc 1](https://github.com/kubernetes/community/blob/master/contributors/design-proposals/garbage-collection.md)
|
2017-02-22 03:12:47 +00:00
|
|
|
|
2017-03-17 00:37:26 +00:00
|
|
|
[Design Doc 2](https://github.com/kubernetes/community/blob/master/contributors/design-proposals/synchronous-garbage-collection.md)
|
2017-02-22 03:12:47 +00:00
|
|
|
|
|
|
|
{% endcapture %}
|
|
|
|
|
|
|
|
|
|
|
|
{% include templates/concept.md %}
|