Search code examples
kubernetesvirtualbox

VirtualBox inside kubernetes container


Headless VirtualBox successfully runs inside Docker container

docker run --device=/dev/vboxdrv:/dev/vboxdrv my-vb

I need to run this image on Kubernetes and I get:

VBoxHeadless: Error -1909 in suplibOsInit!
VBoxHeadless: Kernel driver not accessible

Kubernetes object:

metadata:
  name: vbox
  labels:
    app: vbox
spec:
  selector:
    matchLabels:
      app: vbox
  template:
    metadata:
      labels:
        app: vbox
    spec:
      securityContext:
        runAsUser: 0
      containers:
      - name: vbox-vm
        image: my-vb
        imagePullPolicy: 'Always'
        ports:
        - containerPort: 6666

        volumeMounts:
        - mountPath: /root/img.vdi
          name: img-vdi

        - mountPath: /dev/vboxdrv
          name: vboxdrv

      volumes:
      - name: img-vdi
        hostPath:
          path: /root/img.vdi
          type: File

      - name: vboxdrv
        hostPath:
          path: /dev/vboxdrv
          type: CharDevice

This image runs in Docker so must be the problem in Kubernetes configuration.


Solution

  • Slight modification is required in the configuration for this work:

    metadata:
      name: vbox
      labels:
        app: vbox
    spec:
      selector:
        matchLabels:
          app: vbox
      template:
        metadata:
          labels:
            app: vbox
        spec:
          securityContext:
            runAsUser: 0
          containers:
          - name: vbox-vm
            image: my-vb
            imagePullPolicy: 'Always'
            securityContext:  # << added
              privileged: true
    
            ports:
            - containerPort: 6666
    
            volumeMounts:
            - mountPath: /root/img.vdi
              name: img-vdi
    
            - mountPath: /dev/vboxdrv
              name: vboxdrv
    
          volumes:
          - name: img-vdi
            hostPath:
              path: /root/img.vdi
              type: File
    
          - name: vboxdrv
            hostPath:
              path: /dev/vboxdrv
              type: CharDevice
    

    To be able to run privileged containers you'll need to have:

    • kube-apiserver running with --allow-privileged
    • kubelet (all hosts that might have this container) running with --allow-privileged=true

    See more at https://kubernetes.io/docs/concepts/workloads/pods/pod/#privileged-mode-for-pod-containers

    Once it works do it properly via PodSecurityPolicy