本文分析的是kubernetes 刪除資源對象的策略
具體源碼在$GOPATH/src/k8s.io/kubernetes/vendor/k8s.io/apimachinery/pkg/apis/meta/v1/types.go文件中
策略
DeleteOptions 結構體屬性如下
type DeletionPropagation string
const (
// Orphans the dependents.
DeletePropagationOrphan DeletionPropagation = "Orphan"
// Deletes the object from the key-value store, the garbage collector will
// delete the dependents in the background.
DeletePropagationBackground DeletionPropagation = "Background"
// The object exists in the key-value store until the garbage collector
// deletes all the dependents whose ownerReference.blockOwnerDeletion=true
// from the key-value store. API sever will put the "foregroundDeletion"
// finalizer on the object, and sets its deletionTimestamp. This policy is
// cascading, i.e., the dependents will be deleted with Foreground.
DeletePropagationForeground DeletionPropagation = "Foreground"
)
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// DeleteOptions may be provided when deleting an API object.
type DeleteOptions struct {
TypeMeta `json:",inline"`
// The duration in seconds before the object should be deleted. Value must be non-negative integer.
// The value zero indicates delete immediately. If this value is nil, the default grace period for the
// specified type will be used.
// Defaults to a per object value if not specified. zero means delete immediately.
// +optional
GracePeriodSeconds *int64 `json:"gracePeriodSeconds,omitempty" protobuf:"varint,1,opt,name=gracePeriodSeconds"`
// Must be fulfilled before a deletion is carried out. If not possible, a 409 Conflict status will be
// returned.
// +optional
Preconditions *Preconditions `json:"preconditions,omitempty" protobuf:"bytes,2,opt,name=preconditions"`
// Deprecated: please use the PropagationPolicy, this field will be deprecated in 1.7.
// Should the dependent objects be orphaned. If true/false, the "orphan"
// finalizer will be added to/removed from the object's finalizers list.
// Either this field or PropagationPolicy may be set, but not both.
// +optional
OrphanDependents *bool `json:"orphanDependents,omitempty" protobuf:"varint,3,opt,name=orphanDependents"`
// Whether and how garbage collection will be performed.
// Either this field or OrphanDependents may be set, but not both.
// The default policy is decided by the existing finalizer set in the
// metadata.finalizers and the resource-specific default policy.
// Acceptable values are: 'Orphan' - orphan the dependents; 'Background' -
// allow the garbage collector to delete the dependents in the background;
// 'Foreground' - a cascading policy that deletes all dependents in the
// foreground.
// +optional
PropagationPolicy *DeletionPropagation `json:"propagationPolicy,omitempty" protobuf:"varint,4,opt,name=propagationPolicy"`
}
從以上源碼內容可以看出,有三種刪除策略
Foreground:刪除控制器之前,所管理的資源對象必須先刪除
Background:刪除控制器後,所管理的資源對象由GC刪除
Orphan:只刪除控制器對象,不刪除控制器所管理的資源對象
接下來驗證這三種策略,這個需要kube-apiserver開啓8080端口,直接通過kubectl proxy 8080
啓動,採用的是curl工具
1.Foreground policy
創建 deployment
$kubectl run my-nginx --image=nginx -n qinzhao
deployment.apps/my-nginx created
驗證
curl -XDELETE -H "Content-Type: application/json" localhost:8080/apis/extensions/v1beta1/namespaces/qinzhao/deployments/my-nginx -d '{"kind":"DeleteOptions","apiVersion":"v1","propagationPolicy":"Foreground"}'
{
"kind": "Deployment",
"apiVersion": "extensions/v1beta1",
"metadata": {
"name": "my-nginx",
"namespace": "qinzhao",
"selfLink": "/apis/extensions/v1beta1/namespaces/qinzhao/deployments/my-nginx",
"uid": "5c3e7e1a-188a-11e9-b0dd-5254eec04736",
"resourceVersion": "15787798",
"generation": 2,
"creationTimestamp": "2019-01-15T05:56:54Z",
"deletionTimestamp": "2019-01-15T06:32:14Z",
"deletionGracePeriodSeconds": 0,
"labels": {
"run": "my-nginx"
},
"annotations": {
"deployment.kubernetes.io/revision": "1"
},
"finalizers": [
"foregroundDeletion"
]
},
"spec": {
"replicas": 1,
"selector": {
"matchLabels": {
"run": "my-nginx"
}
},
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"run": "my-nginx"
}
},
"spec": {
"containers": [
{
"name": "my-nginx",
"image": "nginx",
"resources": {
},
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File",
"imagePullPolicy": "Always"
}
],
"restartPolicy": "Always",
"terminationGracePeriodSeconds": 30,
"dnsPolicy": "ClusterFirst",
"securityContext": {
},
"schedulerName": "default-scheduler"
}
},
"strategy": {
"type": "RollingUpdate",
"rollingUpdate": {
"maxUnavailable": "25%",
"maxSurge": "25%"
}
},
"revisionHistoryLimit": 2,
"progressDeadlineSeconds": 600
},
"status": {
"observedGeneration": 1,
"replicas": 1,
"updatedReplicas": 1,
"readyReplicas": 1,
"availableReplicas": 1,
"conditions": [
{
"type": "Available",
"status": "True",
"lastUpdateTime": "2019-01-15T05:57:02Z",
"lastTransitionTime": "2019-01-15T05:57:02Z",
"reason": "MinimumReplicasAvailable",
"message": "Deployment has minimum availability."
},
{
"type": "Progressing",
"status": "True",
"lastUpdateTime": "2019-01-15T05:57:02Z",
"lastTransitionTime": "2019-01-15T05:56:54Z",
"reason": "NewReplicaSetAvailable",
"message": "ReplicaSet \"my-nginx-8656859f54\" has successfully progressed."
}
]
}
}
可以看到返回有deletionTimestamp,finalizers這兩個屬性
"deletionTimestamp": "2019-01-15T06:32:14Z",
"finalizers": [
"foregroundDeletion"
]
這個就是Foreground policy刪除策略
2.Background Policy
創建 deployment
$kubectl run my-nginx --image=nginx -n qinzhao
deployment.apps/my-nginx created
驗證
# curl -X DELETE localhost:8080/apis/extensions/v1beta1/namespaces/qinzhao/deployments/my-nginx -d '{"kind":"DeleteOptions","apiVersion":"v1","propagationPolicy":"Background"}' -H "Content-Type: application/json"
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {
},
"status": "Success",
"details": {
"name": "my-nginx",
"group": "extensions",
"kind": "deployments",
"uid": "f5a51714-188f-11e9-b0dd-5254eec04736"
}
}
3.Orphan Policy策略
創建deployment
# kubectl run my-nginx --image=nginx -n qinzhao
deployment.apps/my-nginx created
# kubectl get pod -n qinzhao
NAME READY STATUS RESTARTS AGE
demo02-599f688bf8-bw9sj 1/1 Running 0 5h
my-nginx-8656859f54-j4x2l 1/1 Running 0 9s
驗證
# curl -X DELETE localhost:8080/apis/extensions/v1beta1/namespaces/qinzhao/deployments/my-nginx -d '{"kind":"DeleteOptions","apiVersion":"v1","propagationPolicy":"Orphan"}' -H "Content-Type: application/json"
{
"kind": "Deployment",
"apiVersion": "extensions/v1beta1",
"metadata": {
"name": "my-nginx",
"namespace": "qinzhao",
"selfLink": "/apis/extensions/v1beta1/namespaces/qinzhao/deployments/my-nginx",
"uid": "a901a487-1890-11e9-b0dd-5254eec04736",
"resourceVersion": "15789226",
"generation": 2,
"creationTimestamp": "2019-01-15T06:42:00Z",
"deletionTimestamp": "2019-01-15T06:43:11Z",
"deletionGracePeriodSeconds": 0,
"labels": {
"run": "my-nginx"
},
"annotations": {
"deployment.kubernetes.io/revision": "1"
},
"finalizers": [
"orphan"
]
},
"spec": {
"replicas": 1,
"selector": {
"matchLabels": {
"run": "my-nginx"
}
},
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"run": "my-nginx"
}
},
"spec": {
"containers": [
{
"name": "my-nginx",
"image": "nginx",
"resources": {
},
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File",
"imagePullPolicy": "Always"
}
],
"restartPolicy": "Always",
"terminationGracePeriodSeconds": 30,
"dnsPolicy": "ClusterFirst",
"securityContext": {
},
"schedulerName": "default-scheduler"
}
},
"strategy": {
"type": "RollingUpdate",
"rollingUpdate": {
"maxUnavailable": "25%",
"maxSurge": "25%"
}
},
"revisionHistoryLimit": 2,
"progressDeadlineSeconds": 600
},
"status": {
"observedGeneration": 1,
"replicas": 1,
"updatedReplicas": 1,
"readyReplicas": 1,
"availableReplicas": 1,
"conditions": [
{
"type": "Available",
"status": "True",
"lastUpdateTime": "2019-01-15T06:42:09Z",
"lastTransitionTime": "2019-01-15T06:42:09Z",
"reason": "MinimumReplicasAvailable",
"message": "Deployment has minimum availability."
},
{
"type": "Progressing",
"status": "True",
"lastUpdateTime": "2019-01-15T06:42:09Z",
"lastTransitionTime": "2019-01-15T06:42:00Z",
"reason": "NewReplicaSetAvailable",
"message": "ReplicaSet \"my-nginx-8656859f54\" has successfully progressed."
}
]
}
}
相關屬性
"deletionTimestamp": "2019-01-15T06:43:11Z",
"finalizers": [
"orphan"
]
查看
查看rs
# kubectl get rs my-nginx-8656859f54 -nqinzhao -o json | jq ".items[0].metadata.ownerReferences"
null