You encountered an interesting problem regarding StatefulSet and PVC removal. There is a discussion whether PVCs created by the StatefulSet should be deleted when deleting the corresponding StatefulSet. We recently received information that the feature to autodelete the PVCs created by StatefulSet will probably be available in the 1.23 release. According to the feature documentation, this will allow us to specifiy if the VolumeClaimTemplate PVCs will be deleted after deleting their StatefulSet.
I suspect that with this feature it'll be easy to delete your StatefulSet along with PVC and PV.
For now, you can consider moving the file with the PV to another directory and manage it separately.
However, I will propose another solution which is kind of a workaround but you may be interested.
Basically, we can use the -o
flag with the kustomize build
command. This creates one file per resource, which gives us more control over resources creation.
I will give you an example to illustrate how this can work.
Suppose I have a similar environment to you:
$ tree
.
├── base
│ ├── kustomization.yaml
│ └── statefulset.yaml
└── overlays
└── dev
├── kustomization.yaml
└── pv.yaml
$ cat overlays/dev/kustomization.yaml
bases:
- ../../base
resources:
- pv.yaml
Now let's create a directory where our manifest files generated by kustomize
will be stored:
$ mkdir generated_manifests
Then we can check if the command kustomize build overlays/dev -o generated_manifests
works as expected. First we'll apply the generated manifests (it'll create the web
StatefulSet and pv0003
PersistentVolume):
$ kustomize build overlays/dev -o generated_manifests && kubectl apply -Rf generated_manifests/
statefulset.apps/web created
persistentvolume/pv0003 created
As you can see, the appropriate manifest files have been created in the generated_manifests
directory:
$ ls generated_manifests/
apps_v1_statefulset_web.yaml v1_persistentvolume_pv0003.yaml
Finally, we can try to delete only the web
StatefulSet:
$ kustomize build overlays/dev -o generated_manifests && kubectl delete -f generated_manifests/apps_v1_statefulset_web.yaml
statefulset.apps "web" deleted
I would also like to mention that kustomize
has a feature like "ignore pv.yaml" but it will also be used when creating resources, not just when removing. This is known as a delete patch and a good example can be found here.