Use data from local system in my kubernetes Pod

2/23/2018

I want to use data from the files present in my localsystem in my pod in k8s. How is PersistentLocalVolumes used in this and is it safe to use PersistentLocalVolumes as it is an alpha feature.

Thanks

-- aditya rawat
docker
kubernetes
minikube

2 Answers

2/23/2018

For cluster created with kubeadm.

/etc/systemd/system/kubelet.service.d/10-kubeadm.conf

Add a line for KUBE_FEATURE_GATES.

Environment="KUBE_FEATURE_GATES=--feature-gates PersistentLocalVolumes=true,VolumeScheduling=true,MountPropagation=true"

Add $KUBE_FEATURE_GATES to the ExecStart line.

ExecStart=/usr/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_SYSTEM_PODS_ARGS $KUBELET_NETWORK_ARGS $KUBELET_DNS_ARGS $KUBELET_AUTHZ_ARGS $KUBELET_CADVISOR_ARGS $KUBELET_CGROUP_ARGS $KUBELET_CERTIFICATE_ARGS $KUBELET_EXTRA_ARGS $KUBE_FEATURE_GATES

Manifest

$ cat local_pvc.yaml

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: example-local-pv
  annotations:
    "volume.alpha.kubernetes.io/node-affinity": '{
      "requiredDuringSchedulingIgnoredDuringExecution": {
        "nodeSelectorTerms": [
          { "matchExpressions": [
            { "key": "kubernetes.io/hostname",
              "operator": "In",
              "values": ["my-node"]  <--- change the node name to yours
            }
          ]}
         ]}
        }'
spec:
  capacity:
    storage: 5Gi  <----- change the size to your need
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  local:
    path: /mnt/disks/vol1  <----- change the path to yours

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: example-local-claim
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi  <----- change the size to your need
  storageClassName: local-storage

----
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer

Execution

  1. Restart the cluster.
  2. Create the pv & pvc (kubectl create -f local_pvc.yaml).
  3. Use the pvc in the pod.

References

-- mon
Source: StackOverflow

2/23/2018

You can use the hostPath Volume, which will allow you to mount a directory from the host filesystem onto the POD.

-- yamenk
Source: StackOverflow