How to attach a new hard disk to Minikube in Virtualbox?

Viewed 687

I'm trying to attach a hard disk to Minikube. I can create a new hard drive by going to settings > storage and adding a new hard disk from there but I don't know how to attach it to Minikube. Anyone know how? any help will be appreciated!

1 Answers

Giving additional storage for your minikube instance can be done in multiple ways within Virtualbox environment.

Some of them are:


Focusing on:

Create an additional Virtualbox disk and mount it to minikube instance

Assuming that:

  • You have your minikube instance up and running with Virtualbox as a driver
  • You attached your newly created disk to your minikube instance (VMDK for example)

Steps:

  • Check if minikube instance is aware of new disk
  • Prepare your new disk to be mounted
  • Mount your disk
  • Use your newly created disk with example pods

Check if minikube instance is aware of new disk

You will need to connect to your minikube instance either with:

  • run in powershell/cmd/terminal following command:
    • $ minikube ssh
  • graphically with Virtualbox

You will need to identify the disk you've mounted earlier. You can do it with below command:

  • $ fdisk -l

Output of this command should look like that:

Disk /dev/sdb: 16 GiB, 17179869184 bytes, 33554432 sectors
Disk model: VBOX HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xb925daf0

Prepare your new disk to be mounted

You will need to create a partition table for this disk and format it.

  • $ fdisk /dev/sdb
  • n,p,1,enter,enter - create a one partition for whole disk

You can use below guide for more reference:

You will also need to format this disk:

  • mkfs.ext4 /dev/sdb1 # please notice the number

Mount your disk

You can mount this disk with following commands:

  • mkdir /data/disk - create a folder to mount your disk into
  • mount /dev/sdb1 /data/disk - mount your disk to specified location (not working after reboot)

Disclaimer!

Creating an entry in /etc/fstab to have this disk mounted between shutdowns/reboots will not work as /etc/fstab will be overridden with each $ minikube start

To overcome /etc/fstab being overridden you will need to:

  • create a file in: /var/lib/boot2docker/
  • name of: bootlocal.sh
  • with content: mount /dev/sdb1 /data/disk

This steps will create a script that will be executed on each start of your minikube instance mounting your disk to a /data/disk.

Reference to startup commands in minikube:


Use your newly created disk with example pods

By default minikube instance support hostPath:

You can create a PV and PVC with below example:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0001
spec:
  storageClassName: standard
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 1Gi
  hostPath:
    path: /data/disk/pv0001
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv0001-claim
spec:
  storageClassName: standard
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  volumeName: pv0001

You can mount previously created PVC to a pod:

apiVersion: v1
kind: Pod
metadata:
  name: ubuntu
spec:
  volumes:
    - name: storage-name
      persistentVolumeClaim:
        claimName: pv0001-claim
  containers:
    - name: ubuntu
      image: ubuntu
      command: 
      - sleep
      - "infinity"
      volumeMounts:
        - mountPath: "/storage"
          name: storage-name

I put a file in /data/disk/pv0001 to check if it's working:

$ kubectl exec -it ubuntu -- cat /storage/hello.txt
Hello there!
Related