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
-
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 asstorageclass.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
-
Apply the yaml
kubectl apply -f storageclass.yaml
Create a PersistentVolumeClaim
-
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
-
Apply the yaml
kubectl apply -f myvol.yaml
-
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
-
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
-
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.
-
Open up a shell to the pod
kubectl exec -it pod/nginxcinder -- /bin/bash
-
Use the following command to create the default index page.
root@nginxcinder:/# echo "THIS IS MY PAGE" > /usr/share/nginx/html/index.html
-
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.
-
Edit
nginxservice.yaml
and make the following changesapiVersion: v1 kind: Service metadata: name: nginxservice spec: ports: - port: 80 targetPort: 80 protocol: TCP selector: #run: webserver app: nginxcinder type: LoadBalancer
-
Run
kubectl apply -f nginxservice.yaml
-
Refresh the URL pointing to your loadbalancer. You should now see your custom page
Checking for persistency
-
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
-
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