<p>Using <code>kubeadm</code> to create a cluster, I have a master and work node. </p>

<p>Now I want to share a <code>persistentVolume</code> in the work node, which will be bound with <code>Postgres</code> pod.</p>

<p>Expecting the code will create <code>persistentVolume</code> in the path <code>/postgres</code> of work node, but it seems the <code>hostPath</code> will not work in a cluster, how should I assign this property to the specific node?</p>

<pre><code>kind: PersistentVolume
apiVersion: v1
metadata:
  name: pv-postgres
  labels:
    type: local
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/postgres"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pvc-postgres
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  selector:
    matchLabels:
      app: postgres
  replicas: 1
  strategy: {}
  template:
    metadata:
      labels:
        app: postgres
    spec:
      dnsPolicy: ClusterFirstWithHostNet
      hostNetwork: true
      volumes:
      - name: vol-postgres
        persistentVolumeClaim:
          claimName: pvc-postgres
      containers:
      - name: postgres
        image: postgres:12
        imagePullPolicy: Always
        env:
        - name: DB_USER
          value: postgres
        - name: DB_PASS
          value: postgres
        - name: DB_NAME
          value: postgres
        ports:
        - name: postgres
          containerPort: 5432
        volumeMounts:
        - mountPath: "/postgres"
          name: vol-postgres
        livenessProbe:
          exec:
            command:
            - pg_isready
            - -h
            - localhost
            - -U
            - postgres
          initialDelaySeconds: 30
          timeoutSeconds: 5
        readinessProbe:
          exec:
            command:
            - pg_isready
            - -h
            - localhost
            - -U
            - postgres
          initialDelaySeconds: 5
          timeoutSeconds: 1
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
spec:
  ports:
  - name: postgres
    port: 5432
    targetPort: postgres
  selector:
    app: postgres
</code></pre>

<p>As per <a href="https://kubernetes.io/docs/concepts/storage/volumes/#hostpath" rel="noreferrer">docs</a>.</p>

<blockquote>
  <p>A hostPath volume mounts a file or directory from the host node’s filesystem into your Pod. This is not something that most Pods will need, but it offers a powerful escape hatch for some applications.</p>
</blockquote>

<p>In short, <code>hostPath</code> type refers to node (machine or VM) resource, where you will schedule pod. It mean that you already need to have this folder on this node. 
To assign resources to specify node you have to use <a href="https://kubernetes.io/docs/concepts/configuration/assign-pod-node/" rel="noreferrer">nodeSelector</a> in your <code>Deployment</code>, <code>PV</code>.</p>

<p>Depends on the scenario, using <code>hostPath</code> is not the best idea, however I will provide below example YAMLs which might show you concept. Based on your YAMLs but with <code>nginx image</code>.</p>

<pre><code>kind: PersistentVolume
apiVersion: v1
metadata:
  name: pv-postgres
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/tmp/postgres" ## this folder need exist on your node. Keep in minds also who have permissions to folder. Used tmp as it have 3x rwx
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - ubuntu18-kubeadm-worker1    

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pvc-postgres
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  selector:
    matchLabels:
      app: postgres
  replicas: 1
  strategy: {}
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - image: nginx
        name: nginx      
        volumeMounts:
        - mountPath: /home    ## path to folder inside container
          name: vol-postgres
      affinity:               ## specified affinity to schedule all pods on this specific node with name ubuntu18-kubeadm-worker1
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/hostname
                operator: In
                values:
                - ubuntu18-kubeadm-worker1  
      dnsPolicy: ClusterFirstWithHostNet
      hostNetwork: true
      volumes:
      - name: vol-postgres
        persistentVolumeClaim:
          claimName: pvc-postgres

persistentvolume/pv-postgres created
persistentvolumeclaim/pvc-postgres created
deployment.apps/postgres created
</code></pre>

<p>Unfortunately PV is bounded to PVC in 1:1 relationship, so for each time, you would need to create PV and PVC. </p>

<p>However if you are using <code>hostPath</code> it's enough to specify <code>nodeAffinity</code>, <code>volumeMounts</code> and <code>volumes</code> in <code>Deployment</code> YAML without <code>PV</code> and <code>PVC</code>.</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  selector:
    matchLabels:
      app: postgres
  replicas: 1
  strategy: {}
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - image: nginx:latest
        name: nginx      
        volumeMounts:
        - mountPath: /home    
          name: vol-postgres
      affinity:               
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/hostname
                operator: In
                values:
                - ubuntu18-kubeadm-worker1  
      dnsPolicy: ClusterFirstWithHostNet
      hostNetwork: true
      volumes:
      - name: vol-postgres
        hostPath:
          path: /tmp/postgres

deployment.apps/postgres created

user@ubuntu18-kubeadm-master:~$ kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
postgres-77bc9c4566-jgxqq   1/1     Running   0          9s
user@ubuntu18-kubeadm-master:~$ kk exec -ti postgres-77bc9c4566-jgxqq /bin/bash
root@ubuntu18-kubeadm-worker1:/# cd home
root@ubuntu18-kubeadm-worker1:/home# ls
test.txt  txt.txt
</code></pre>
