website/content/zh-cn/docs/tasks/run-application/delete-stateful-set.md

165 lines
5.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: 删除 StatefulSet
content_type: task
weight: 60
---
<!--
reviewers:
- bprashanth
- erictune
- foxish
- janetkuo
- smarterclayton
title: Delete a StatefulSet
content_type: task
weight: 60
-->
<!-- overview -->
<!--
This task shows you how to delete a {{< glossary_tooltip term_id="StatefulSet" >}}.
-->
本任务展示如何删除 {{< glossary_tooltip text="StatefulSet" term_id="StatefulSet" >}}。
## {{% heading "prerequisites" %}}
<!--
- This task assumes you have an application running on your cluster represented by a StatefulSet.
-->
- 本任务假设在你的集群上已经运行了由 StatefulSet 创建的应用。
<!-- steps -->
<!--
## Deleting a StatefulSet
You can delete a StatefulSet in the same way you delete other resources in Kubernetes:
use the `kubectl delete` command, and specify the StatefulSet either by file or by name.
-->
## 删除 StatefulSet {#deleting-a-statefulset}
你可以像删除 Kubernetes 中的其他资源一样删除 StatefulSet
使用 `kubectl delete` 命令,并按文件或者名字指定 StatefulSet。
```shell
kubectl delete -f <file.yaml>
```
<!--
```shell
kubectl delete statefulsets <statefulset-name>
```
-->
```shell
kubectl delete statefulsets <statefulset 名称>
```
<!--
You may need to delete the associated headless service separately after the StatefulSet itself is deleted.
```shell
kubectl delete service <service-name>
```
-->
删除 StatefulSet 之后你可能需要单独删除关联的无头服务Headless Service
```shell
kubectl delete service <Service 名称>
```
<!--
When deleting a StatefulSet through `kubectl`, the StatefulSet scales down to 0.
All Pods that are part of this workload are also deleted. If you want to delete
only the StatefulSet and not the Pods, use `--cascade=orphan`. For example:
--->
当通过 `kubectl` 删除 StatefulSet 时StatefulSet 会被缩容为 0。
属于该 StatefulSet 的所有 Pod 也被删除。
如果你只想删除 StatefulSet 而不删除 Pod使用 `--cascade=orphan`
```shell
kubectl delete -f <file.yaml> --cascade=orphan
```
<!--
By passing `--cascade=orphan` to `kubectl delete`, the Pods managed by the StatefulSet
are left behind even after the StatefulSet object itself is deleted. If the pods have
a label `app.kubernetes.io/name=MyApp`, you can then delete them as follows:
--->
通过将 `--cascade=orphan` 传递给 `kubectl delete`,在删除 StatefulSet 对象之后,
StatefulSet 管理的 Pod 会被保留下来。如果 Pod 具有标签 `app.kubernetes.io/name=MyApp`
则可以按照如下方式删除它们:
```shell
kubectl delete pods -l app.kubernetes.io/name=MyApp
```
<!--
### Persistent Volumes
Deleting the Pods in a StatefulSet will not delete the associated volumes.
This is to ensure that you have the chance to copy data off the volume before
deleting it. Deleting the PVC after the pods have terminated might trigger
deletion of the backing Persistent Volumes depending on the storage class
and reclaim policy. You should never assume ability to access a volume
after claim deletion.
-->
### 持久卷 {#persistent-volumes}
删除 StatefulSet 管理的 Pod 并不会删除关联的卷。这是为了确保你有机会在删除卷之前从卷中复制数据。
在 Pod 已经终止后删除 PVC 可能会触发删除背后的 PV 持久卷,具体取决于存储类和回收策略。
永远不要假定在 PVC 删除后仍然能够访问卷。
{{< note >}}
<!--
Use caution when deleting a PVC, as it may lead to data loss.
-->
删除 PVC 时要谨慎,因为这可能会导致数据丢失。
{{< /note >}}
<!--
### Complete deletion of a StatefulSet
To delete everything in a StatefulSet, including the associated pods,
you can run a series of commands similar to the following:
-->
### 完全删除 StatefulSet {#complete-deletion-of-a-statefulset}
要删除 StatefulSet 中的所有内容,包括关联的 Pod
你可以运行如下所示的一系列命令:
```shell
grace=$(kubectl get pods <stateful-set-pod> --template '{{.spec.terminationGracePeriodSeconds}}')
kubectl delete statefulset -l app.kubernetes.io/name=MyApp
sleep $grace
kubectl delete pvc -l app.kubernetes.io/name=MyApp
```
<!--
In the example above, the Pods have the label `app.kubernetes.io/name=MyApp`;
substitute your own label as appropriate.
-->
在上面的例子中Pod 的标签为 `app.kubernetes.io/name=MyApp`;适当地替换你自己的标签。
<!--
### Force deletion of StatefulSet pods
If you find that some pods in your StatefulSet are stuck in the 'Terminating'
or 'Unknown' states for an extended period of time, you may need to manually
intervene to forcefully delete the pods from the apiserver.
This is a potentially dangerous task. Refer to
[Force Delete StatefulSet Pods](/docs/tasks/run-application/force-delete-stateful-set-pod/)
-->
### 强制删除 StatefulSet 的 Pod {#force-deletion-of-statefulset-pods}
如果你发现 StatefulSet 的某些 Pod 长时间处于 'Terminating' 或者 'Unknown' 状态,
则可能需要手动干预以强制从 API 服务器中删除这些 Pod。这是一项有点危险的任务。
详细信息请阅读[强制删除 StatefulSet 的 Pod](/zh-cn/docs/tasks/run-application/force-delete-stateful-set-pod/)。
## {{% heading "whatsnext" %}}
<!--
Learn more about [force deleting StatefulSet Pods](/docs/tasks/run-application/force-delete-stateful-set-pod/).
-->
进一步了解[强制删除 StatefulSet 的 Pod](/zh-cn/docs/tasks/run-application/force-delete-stateful-set-pod/)。