I'd like to set a Persistent Volume (pv and pvc), shared by pods in Kind cluster. But I need to keep the data persisted in my laptop (host server) as well, so the volume's path should be something in my laptop, which I can directly access it.
If I delete the kind cluster, the volume should be persisted, not be destroyed.
I hope to easily add or update in that volume or copy files out of it from my host laptop.
How can I let the pods know it in KIND cluster?
Paste my kind.yaml
for your refernce
$ kind
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
- role: worker
When you create your kind cluster you can specify host directories to be mounted on a virtual node. If you do that, then you can configure volumes with hostPath
storage, and they will refer to the mount paths on the node.
So you would create a kind config file:
apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
nodes:
- role: control-plane
extraMounts:
- hostPath: /home/bill/work/foo
containerPath: /foo
and then run
kind create cluster --config kind-config.yaml
to create the cluster.
In your Kubernetes YAML file, you need to mount that containerPath
as a "host path" on the node. A pod spec might contain in part:
volumes:
- name: foo
hostPath:
path: /foo # matches kind containerPath:
containers:
- name: foo
volumeMounts:
- name: foo
mountPath: /data # in the container filesystem
Note that this setup is extremely specific to kind. Host paths aren't reliable storage in general: you can't control which node a pod gets scheduled on, and both pods and nodes can get deleted in real-world clusters. In some hosted setups (AWS EKS, Google GKE) you may not be able to control the host content at all.
You might revisit your application design to minimize the need for "files" as first-class objects. Rather than "update the volume" consider deploying a new Docker image with updated content; rather than "copy files out" consider an HTTP service you can expose through an ingress controller.
I would like to add that to minimise the specific configuration to Kind
you should use pv / pvc
this way the configuration on a real cluster will only differ in the definition of pv.
So if you configure extraMounts on your Kind cluster:
apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
nodes:
- role: control-plane
extraMounts:
- hostPath: /home/bill/work/www
containerPath: /www
Then on that cluster create PV and PVC:
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-www
spec:
storageClassName: standard
accessModes:
- ReadWriteOnce
capacity:
storage: 2Gi
hostPath:
path: /www/
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-www
spec:
volumeName: pv-www
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
After that you can use it in deployment like this:
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
volumes:
- name: www
persistentVolumeClaim:
claimName: pvc-www
containers:
- name: nginx
image: nginx:1.14.2
volumeMounts:
- name: www
mountPath: /var/www
As a result your local /home/bill/work/www
will be mounted to /var/www
inside containers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With