Kubernetes

Volume

Volumes are one of the ways you can store data. Magnum allows Kubernetes to use Cinder Volumes as Kubernetes Volumes, so that you can store data persistently.

Create a StorageClass

  1. Create a default StorageClass. You only need to do this once per cluster. This defines which availability zone you want to create volumes in. Remember to replace <availability-zone> with the availability zone where you have volume quota. Create the following as storageclass.yaml.

    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
      name: default
      annotations:
        storageclass.kubernetes.io/is-default-class: "true"
    parameters:
      availability: <availability-zone>
    provisioner: cinder.csi.openstack.org
    
  2. Apply the yaml

    kubectl apply -f storageclass.yaml
    

Create a PersistentVolumeClaim

  1. Create a PersistentVolumeClaim. On Nectar Cloud, this creates a volume backed by Cinder. Create the following as myvol.yaml.

    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata:
      name: myvol
      namespace: default
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 3Gi
    
  2. Apply the yaml

    kubectl apply -f myvol.yaml
    
  3. You should be able to see the PersistentVolume created. (You might need to wait a few minutes for it to be ready)

    kubectl get pvc
    kubectl get pv
    

Create a Pod

  1. Create a nginx Pod that uses the PVC as a persistent volume. Create the following as nginxcinder.yaml.

    apiVersion: v1
    kind: Pod
    metadata:
      name: nginxcinder
      labels:
        app: nginxcinder
    spec:
      containers:
        - name: nginxcinder
          image: nginx
          volumeMounts:
            - name: html-volume
              mountPath: "/usr/share/nginx/html"
      volumes:
        - name: html-volume
          persistentVolumeClaim:
            claimName: myvol
    
  2. Apply the yaml

    kubectl apply -f nginxcinder.yaml
    

Writing index page for nginx

In the previous example, we have mounted a blank new volume to our pod.

Now, we want to write data to volume for nginx to service.

  1. Open up a shell to the pod

    kubectl exec -it pod/nginxcinder -- /bin/bash
    
  2. Use the following command to create the default index page.

    root@nginxcinder:/# echo "THIS IS MY PAGE" > /usr/share/nginx/html/index.html
    
  3. Exit the pod cli

    root@nginxcinder:/# exit
    

Redirect loadbalancer

Direct your loadbalancer to your new pod. We can do this easily by updating the label.

  1. Edit nginxservice.yaml and make the following changes

    apiVersion: v1
    kind: Service
    metadata:
      name: nginxservice
    spec:
      ports:
      - port: 80
        targetPort: 80
        protocol: TCP
      selector:
        #run: webserver
        app: nginxcinder
      type: LoadBalancer
    
  2. Run kubectl apply -f nginxservice.yaml

  3. Refresh the URL pointing to your loadbalancer. You should now see your custom page

Checking for persistency

  1. If you delete the pod and recreate it, the same content should still be available. This shows that the volume is not tied to the lifetime of the pod.

    kubectl delete pod nginxcinder
    kubectl apply -f nginxcinder.yaml
    

Clean up

  1. Delete the pod and PersistentVolume when you are done

    kubectl delete pod nginxcinder
    kubectl delete pvc myvol
    

More information

For more information, refer to:

Tutorial Complete!

You have completed this tutorial. Continue with the other tutorials in the Series or return to the tutorials home page.

Continue Series Return Home