How to extract volumeClaimTemplates to a separate PersistentVolumeClaim yaml file?

可紊 提交于 2020-06-29 04:19:10

问题


Let's say I have a StatefulSet definition

apiVersion: v1
kind: StatefulSet
metadata:
  name: web
spec:
  ...
  volumeClaimTemplates:
  — metadata:
      name: www
    spec:
      resources:
        requests:
          storage: 1Gi

This will create me a PersistentVolumeClaim (PVC) with a PersistentVolume (PV) of 1 GiB for each pod.

How can I write something like this

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: www
spec:
  ...
  resources:
    requests:
      storage: 1Gi
...

and connect it with the StatefulSet in a way that it still creates a PVC and PV for each pod?


回答1:


I am guessing that in your question you are using statfulset example from this website so I will follow its naming convention.

The solution I am about to present you was tested by myself and it seems to work.

In k8s api reference you can find the folllowing definition:

volumeClaimTemplates is a list of claims that pods are allowed to reference. The StatefulSet controller is responsible for mapping network identities to claims in a way that maintains the identity of a pod. Every claim in this list must have at least one matching (by name) volumeMount in one container in the template. A claim in this list takes precedence over any volumes in the template, with the same name.

So this means that as long as you have volumeclaim with specific name, staefulset will use it without creating a new one. This means that you can create some pv/pvc manually and statefulset will use them.

All you need to do is to correctly name your pvcs. How is this name supposed to look like? Here is the first part:

volumeClaimTemplates:
  - metadata:
      name: www        <-here is the first part

and the second part is a pod name.

(Have a look at this Stack question on can-i-rely-on-volumeclaimtemplates-naming-convention.)

These two parts combined together create a name of pvc (separated with dash) e.g.

www-web-0        <- this is how you are supposed to name one of your pvcs
│   └ second part (pod name)
└ first part

If you already have (automatically provisioned) PVCs, use

kubectl get pvc <pvcname> -oyaml > pvcname.yaml
kubectl get pv  <pvname>  -oyaml > pvname.yaml

to save its specification to disk. Then you can run:

kubectl apply -f pvcname.yaml
kubectl apply -f pvname.yaml

to apply pvc/pv configuration. Remember that some yaml files may require slight modifications before running kubectl apply.



来源:https://stackoverflow.com/questions/62238476/how-to-extract-volumeclaimtemplates-to-a-separate-persistentvolumeclaim-yaml-fil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!