apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2 # 告知 Deployment 运行 2 个与该模板匹配的 Pod
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
通过 YAML 文件创建一个 Deployment:
kubectl apply -f deployment.yaml
检查确认你的 Pods 上存在 ownerReferences
字段:
kubectl get pods -l app=nginx --output=yaml
输出中包含 ownerReferences
字段,类似这样:
apiVersion: v1
...
ownerReferences:
- apiVersion: apps/v1
blockOwnerDeletion: true
controller: true
kind: ReplicaSet
name: nginx-deployment-6595874d85
uid: 3251b01a-d0c2-43b3-9c59-74c7cfd9c429
resourceVersion: "25810839"
uid: 1bc75e45-70bc-4c7e-ab33-deeed1e34641
...
默认情况下,Kubernetes 使用后台级联删除 以删除依赖某对象的其他对象。取决于你的集群所运行的 Kubernetes 版本, 你可以使用 kubectl
或者 Kubernetes API 来切换到前台级联删除。 要获知版本信息,请输入 kubectl version
.
你可以使用 kubectl
或者 Kubernetes API 来基于前台级联删除来删除对象。
运行下面的命令:
kubectl delete deployment nginx-deployment --cascade=foreground
kubectl proxy --port=8080
使用 curl
来触发删除操作:
curl -X DELETE localhost:8080/apis/apps/v1/namespaces/default/deployments/nginx-deployment \
-d '{"kind":"DeleteOptions","apiVersion":"v1","propagationPolicy":"Foreground"}' \
-H "Content-Type: application/json"
输出中包含 foregroundDeletion
finalizer, 类似这样:
"kind": "Deployment",
"apiVersion": "apps/v1",
"metadata": {
"name": "nginx-deployment",
"namespace": "default",
"uid": "e1091db1-104d-4ae4-8923-e9e8f464d6d0",
"resourceVersion": "25811881",
"generation": 3,
"creationTimestamp": "2023-06-26T02:56:11Z",
"deletionTimestamp": "2023-06-26T03:02:58Z",
"deletionGracePeriodSeconds": 0,
"annotations": {
"deployment.kubernetes.io/revision": "1",
"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"apps/v1\",\"kind\":\"Deployment\",\"metadata\":{\"annotations\":{},\"name\":\"nginx-deployment\",\"namespace\":\"default\"},\"spec\":{\"replicas\":2,\"selector\":{\"matchLabels\":{\"app\":\"nginx\"}},\"template\":{\"metadata\":{\"labels\":{\"app\":\"nginx\"}},\"spec\":{\"containers\":[{\"image\":\"nginx:1.14.2\",\"name\":\"nginx\",\"ports\":[{\"containerPort\":80}]}]}}}}\n"
},
"finalizers": [
"foregroundDeletion"
]
......
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2 # 告知 Deployment 运行 2 个与该模板匹配的 Pod
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
通过 YAML 文件创建一个 Deployment:
kubectl apply -f deployment.yaml
你可以使用 kubectl
或者 Kubernetes API 来执行后台级联删除方式的对象删除操作。
Kubernetes 默认采用后台级联删除方式,如果你在运行下面的命令时不指定 --cascade
标志或者 propagationPolicy
参数时,用这种方式来删除对象。
运行下面的命令:
kubectl delete deployment nginx-deployment --cascade=background
启动一个本地代理会话:
kubectl proxy --port=8080
使用 curl
来触发删除操作:
curl -X DELETE localhost:8080/apis/apps/v1/namespaces/default/deployments/nginx-deployment \
-d '{"kind":"DeleteOptions","apiVersion":"v1","propagationPolicy":"Background"}' \
-H "Content-Type: application/json"
输出结果
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {},
"status": "Success",
"details": {
"name": "nginx-deployment",
"group": "apps",
"kind": "deployments",
"uid": "6175b70e-5593-484d-a04d-9aa17db47fa7"
}
默认情况下,当你告诉 Kubernetes 删除某个对象时, 控制器 也会删除依赖该对象 的其他对象。 取决于你的集群所运行的 Kubernetes 版本,你也可以使用 kubectl
或者 Kubernetes API 来让 Kubernetes 孤立 这些依赖对象。 要获知版本信息,请输入 kubectl version
.
使用 kubectl
运行下面的命令:
kubectl delete deployment nginx-deployment --cascade=orphan
使用 Kubernetes API
启动一个本地代理会话:
kubectl proxy --port=8080
使用 curl
来触发删除操作:
curl -X DELETE localhost:8080/apis/apps/v1/namespaces/default/deployments/nginx-deployment \
-d '{"kind":"DeleteOptions","apiVersion":"v1","propagationPolicy":"Orphan"}' \
-H "Content-Type: application/json"
输出中在 finalizers
字段中包含 orphan
,如下所示:
"kind": "Deployment",
"apiVersion": "apps/v1",
"namespace": "default",
"uid": "6f577034-42a0-479d-be21-78018c466f1f",
"creationTimestamp": "2021-07-09T16:46:37Z",
"deletionTimestamp": "2021-07-09T16:47:08Z",
"deletionGracePeriodSeconds": 0,
"finalizers": [
"orphan"
],
...
你可以检查 Deployment 所管理的 Pods 仍然处于运行状态:
kubectl get pods -l app=nginx