How to use block device attached to host, as block device within the pod

8/25/2019

Question: How can I use raw devices attached to the host within the pod as block device.

I tried using "hostPath" with type "BlockDevice"

volumes:
- my-data:
  hostPath:
    path: /dev/nvme1n2
    type: BlockDevice
containers:
.....
  volumeDevices:
  - name: my-data
    devicePath: /dev/sda

This configuration gives me the below error.

Invalid value: "my-data": can only use volume source type of PersistentVolumeClaim for block mode

Can I achieve this using PersistentVolume and PersistentVolumeClaim ? Can someone help me with an example config. Appreciate the help.

-- Colin
kubernetes

1 Answer

8/25/2019

Support for Block devices in K8s allows user and admins to use PVs & PVCs for raw block devices to be mounted in Pods. Excerpts below show a small use-case.

  • Create a PV which refers the Raw device on host say /dev/xvdf.
kind: PersistentVolume
apiVersion: v1
metadata:
  name: local-raw-pv
spec:
  volumeMode: Block
  capacity:
    storage: 100Gi
  local:
    path: /dev/xvdf
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  • Create a PVC claiming the block device for applications
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: block-pvc
spec:
  accessModes:
    - ReadWriteOnce
  volumeMode: Block
  resources:
    requests:
      storage: 10Gi
  • Create pod with the above claim which will mount the host device /dev/xvdf inside pod at path /dev/xvda
apiVersion: v1
kind: Pod
metadata:
  name: pod-with-block-volume
spec:
  containers:
    - name: some-container
      image: ubuntu
      command: ["/bin/sh", "-c"]
      args: [ "tail -f /dev/null" ]
      volumeDevices:
        - name: data
          devicePath: /dev/xvda
  volumes:
    - name: data
      persistentVolumeClaim:
        claimName: block-pvc
-- sanster_23
Source: StackOverflow