在本文中,我们将讨论PV和PVC一直Terminating的状态。
何时会Terminting?
在以下情况下,资源将处于Terminating状态。
- 在删除Bounded 状态的PVC之前,删除了对应的PV,PV在删除后是Terminting状态。
- 删除PVC时,仍有引用了PVC的pod在运行(pods attached to the pvc),删除后PVC是Terminting状态。
Kubernetes为了防止数据丢失,会保护当前正在使用的PVC和绑定到PVC的PV不会被从系统中删除。这种保护功能是通过Finalizers完成的。Finalizer 是带有命名空间的键,告诉 Kubernetes 等到特定的条件被满足后, 再完全删除被标记为删除的资源。 Finalizer 提醒控制器清理被删除的对象拥有的资源。
每次创建 PVC 或 PV 时,都会将 Finalizer 附加到我们的资源。 - Finalizer [kubernetes.io/pv-protection] -> 创建 PV 时
- Finalizer [kubernetes.io/pvc-protection] -> 创建 PVC 时
Terminating State 终止状态
Terminating State意味着 PVC 和/或 PV 的删除将被推迟,直到它们不再被 pod 使用或绑定到 PVC。
验证
我们将创建一个 PVC 和一个简单的应用程序,以便我们可以验证 Finalizers 并模拟 Terminating state以及如何修复它。
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: example-claim
spec:storageClassName: longhornaccessModes:- ReadWriteOnceresources:requests:storage: 100Mi
apiVersion: apps/v1
kind: Deployment
metadata:name: example-deploy#namespace: examplelabels:app: example-appannotations:
spec:selector:matchLabels:app: example-appreplicas: 2strategy:type: RollingUpdaterollingUpdate:maxSurge: 1maxUnavailable: 0template:metadata:labels:app: example-appspec:containers:- name: example-appimage: nginximagePullPolicy: Alwaysports:- containerPort: 5000volumes:- name: datapersistentVolumeClaim:claimName: example-claim
验证一下一切是否已启动并正在运行:
kubectl get pvc --field-selector metadata.name=example-claim
kubectl get pv | grep -w "default/example-claim"
# claims associated with pod
kubectl get pods -o custom-columns=POD:.metadata.name,PVC:.spec.volumes[*].persistentVolumeClaim.claimName
PV PVC pod 的关系如下图:
在我们模拟Terminating state之前,让我们验证 PVC 和 PV 是否具有我们之前提到的注释 Finalizer。
#Looking for finalizer pv-protection
kubectl get pv pvc-b05c6e74-89b4-4669-8e00-5036f109a487 -o json | jq .metadata.finalizers
#Looking for finalizer pv-protectionkubectl get pvc example-claim -o json | jq .metadata.finalizers
让我们模拟Terminating state:
正如我们之前提到的,如果在 PV 绑定到 PVC ,直接删除PV 将会使PV处于Terminating state。
kubectl delete pv pvc-b05c6e74-89b4-4669-8e00-5036f109a487
同时删除 PVC:
kubectl delete pvc example-claim
因为仍然有使用 PVC 的pod,所以 PVC 也不会被删除,而是一直处于Terminating state。
删除pod:
kubectl delete pod example-deploy-75bcc94c6d-fftq2
or
kubectl delete deployment example-deploy
注意事项:
- When you delete a statefulset that have a retention policy whenDeleted:delete it will also remove the pods and the PVC.
- If you persistant volume have a retain policy Delete, when deleting the pvc the bound pv and underlying storage volume will be also deleted.
- If your PV retain policy is not Delete, you’ll have to remove the PV and storage manually.
不删除依赖项直接删除PV/PVC
如果删除终止 pv 的Finalizers,pvc 将失去 Bound,保持在 Lost 状态,并且可能会丢失数据。
kubectl patch pvc pvc_name -p '{"metadata":{"finalizers":null}}'
or
kubectl patch pv pv_name -p '{"metadata":{"finalizers":null}}'