Question

How to use Digital Ocean Volumes as persistent storage for the pod to share the storage across multiple pods?

Posted January 31, 2020 340 views
DigitalOcean Managed Kubernetes

I want to use Digital Ocean Volume to store the data which is shared across the pods on a same cluster. Multiple pods will be reading and writing from the same volume at a same time.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hello,

When you need to write and access persistent data in a Kubernetes cluster, you can create and access DigitalOcean Block Storage Volumes by creating a PersistentVolumeClaim (PVC) as part of your deployment. I would recommend following the steps from the official DigitalOcean documentation on how to add Block Storage Volumes to a Kubernetes Cluster:

https://www.digitalocean.com/docs/kubernetes/how-to/add-volumes/

Hope that this helps!
Regards,
Bobby

  • 1. Following is the docker image that I am using

    FROM ubuntu:18.04
    RUN adduser --system --quiet --shell=/bin/bash --home=/opt/bob --gecos 'bob' --group bob
    RUN apt-get update && apt-get upgrade -y
    RUN mkdir /var/lib/bob
    RUN chown bob:bob /opt/bob -R && chown bob:bob -R /var/lib/bob
    USER bob
    CMD ["touch", "/var/lib/bob/Demofile.txt"]
    

    2. Created a DigitalOcean Block Storage

    3. Spinup Kubernetes cluster with following deployment

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: cubuntu
    spec:
      selector:
        matchLabels:
          app: cubuntu
      template:
        metadata:
          labels:
            app: cubuntu
        spec:
          containers:
          - name: cubuntu
            image: registry.gitlab.com/username/cubuntu:latest
            securityContext:
              privileged: true
            volumeMounts:
            - mountPath: "/var/lib/bob"
              name: do-block-storage
          volumes:
            - name: do-block-storage
              persistentVolumeClaim:
                claimName: bob-pv-claim
    
    ---
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: bob-pv-volume
    spec:
      storageClassName: do-block-storage
      capacity:
        storage: 1Gi
      accessModes:
        - ReadWriteOnce
      hostPath:
        path: "/var/bob-pv-volume"
    
    
    ---
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: bob-pv-claim
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 1Gi
      storageClassName: do-block-storage
    

    4. In the logs, got the following

    touch: cannot touch '/var/lib/bob/Demofile.txt': Permission denied
    
    • Hi @parthivgls,

      I would suggest trying to specify the fsGroup under your securityContext as well.

      First, find out the group ID of your bob user, and then specify the fsGroup:

      securityContext:
        fsGroup: 2000
      

      That way the mount would be mounted as your’s group and your user should have the options to write to the mount.

      Let me know how it goes!
      Regards,
      Bobby

Submit an Answer