apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: my-repset
spec:
replicas: 3
selector:
matchLabels:
pod-is-for: garbage-collection-example
template:
metadata:
labels:
pod-is-for: garbage-collection-example
spec:
containers:
- name: nginx
创建此ReplicaSet:
kubectl create -f https://k8s.io/examples/controllers/replicaset.yaml
查看:
kubectl get pods --output=yaml
结果如下:确认ownerReferences的值
apiVersion: v1
kind: Pod
metadata:
...
ownerReferences:
- apiVersion: apps/v1
controller: true
blockOwnerDeletion: true
kind: ReplicaSet
name: my-repset
uid: d9607e19-f88f-11e6-a518-42010a800195
在执行具体的操作请求时,通过设置删除请求中的"propagationPolicy"字段为 “Orphan”, “Foreground”, or “Background”中的一种,选择上述三种删除策略中的一种。
在kubernetes1.9以前,对大部分控制器的删除,默认策略是"Orphan"(注意本段中说的默认值指REST API的默认行为,不是kubectl命令),包含ReplicationController, ReplicaSet, StatefulSet, DaemonSet, and Deployment,也就是在删除根对象时不删除从对象。并且当apiVersion是extensions/v1beta1, apps/v1beta1, and apps/v1beta2时,除非特别指定,默认删除策略仍然是"Orphan"。在kubernetes1.9版本中,所有类型的对象,在app/v1版本的apiVersion中,默认从对象删除。
后台级联删除示例:
kubectl proxy --port=8080
curl -X DELETE localhost:8080/apis/apps/v1/namespaces/default/replicasets/my-repset -d ‘{"kind":"DeleteOptions","apiVersion":"v1","propagationPolicy":"Background"}‘ -H "Content-Type: application/json"
kubectl proxy --port=8080
curl -X DELETE localhost:8080/apis/apps/v1/namespaces/default/replicasets/my-repset -d ‘{"kind":"DeleteOptions","apiVersion":"v1","propagationPolicy":"Foreground"}‘ -H "Content-Type: application/json"
kubectl proxy --port=8080
curl -X DELETE localhost:8080/apis/apps/v1/namespaces/default/replicasets/my-repset -d ‘{"kind":"DeleteOptions","apiVersion":"v1","propagationPolicy":"Orphan"}‘ -H "Content-Type: application/json"
kubectl delete replicaset my-repset --cascade=false
# 当--cascade为true时,kubectl采用的是前台级联删除还是后台级联删除,文档中没有讲。
09-K8S Basic-kubernetes垃圾回收器 Garbage Collection(GC)
原文:https://www.cnblogs.com/dai-zhe/p/14881465.html