I am fairly new to Kubernetes and need some help.
I am using StatefulSet and 3 replicas. There are 2 worker nodes.
I am looking to provision separate hostPath for each replica and not a hardcoded hostPath. Also hostPath is 'local' on the worker node.
For example -
volumeMounts:
- mountPath: /usr/local/store
name: store
volumes:
- name: store
hostPath:
path: /space/myapp/$(POD_NAME)
Here POD_NAME is app-0, app-1, app-2 (3 replicas).
It is fine for our need to have /space/myapp/app-0, /space/myapp/app-1, /space/myapp/app-2 created on multiple worker nodes.
I did some reading and could not come across any obvious solution.
A solution is not to use replicas and create 3 individual PODs with their own hardcoded hostPath. But that is not desirable.
Could you please guide, what in Kubernetes can help me achieve this? Grateful for any help or direction.
The thing you want is a StatefulSet
, which brings with it PVC templates. You are definitely in a worse position by requiring the hostPath
, but either the formal CSI or the previous FlexVolume support can help with that problem.
Am not discussing whether you need to use Deployments vs. Stateful Sets with PVC templates as that is out of the scope of the question - though it may help to research that out first.
One possible solution would be to manage the replicas yourself rather than using the Kubernetes replicas feature (as "everything" isn't being replicated). Having tagged the question "kubernetes-helm" am assuming you can use helm templates.
What you want can be achieved using the following:
values.yaml
file.Indicative code:
chartname:
pods:
- name: pod1
path: volumeHostpathForPod1
- name: pod2
path: volumeHostpathForPod2
...etc
chartname/templates/pod.yaml
(or equivalent file)Indicative Code :
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: "statefulset"
...(other stateful set properties)
spec: #stateful set specs
template:
spec: #pod(s) specs
containers:
{{- range .Values.pods }}
- ... (common pod properties)
name: "pod{{ .name }}"
volumeMounts:
- mountPath: /usr/local/store
name: "storeForPod{{ .name }}"
{{- end }}
volumes:
{{- range .Values.pods }}
- name: "storeForPod{{ .name }}"
hostPath:
path: "/space/myapp/{{ .path }}"
{{- end }}
helm install <deployment-name> ./chartname -f values.yaml <--dry-run> <--debug>