Persistent Volumes in Kubernetes
This is part four of a series of lab tasks from KodeKloud for Kubernetes. Master blog listing all parts can be seen here. Make sure to follow setup instructions for minikube on your PC.
The Nautilus DevOps team is working on a Kubernetes template to deploy a web application on the cluster. There are some requirements to create/use persistent volumes to store the application code, and the template needs to be designed accordingly. Please find more details below:
- Create a
PersistentVolume
named aspv-xfusion
. Configure thespec
as storage class should bemanual
, set capacity to3Gi
, set access mode toReadWriteOnce
, volume type should behostPath
and set path to/mnt/security
(this directory is already created, you might not be able to access it directly, so you need not to worry about it).
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-xfusion
spec:
capacity:
storage: 3Gi
accessModes:
- ReadWriteOnce
storageClassName: manual
hostPath:
path: /mnt/security
- Create a
PersistentVolumeClaim
named aspvc-xfusion
. Configure thespec
as storage class should bemanual
, request1Gi
of the storage, set access mode toReadWriteOnce
.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-xfusion
spec:
accessModes:
- ReadWriteOnce
storageClassName: manual
resources:
requests:
storage: 1Gi
- Create a
pod
named aspod-xfusion
, mount the persistent volume you created with claim namepvc-xfusion
at document root of the web server, the container within the pod should be named ascontainer-xfusion
using imagenginx
withlatest
tag only (remember to mention the tag i.enginx:latest
).
apiVersion: v1
kind: Pod
metadata:
name: pod-xfusion
labels:
app: pv-nginx
spec:
volumes:
- name: storage-xfusion
persistentVolumeClaim: # Mount persistent volume
claimName: pvc-xfusion # Claim storage
containers:
- name: container-xfusion
image: nginx:latest
ports:
- containerPort: 80
volumeMounts:
- name: storage-xfusion
mountPath: /usr/share/nginx/html
- Create a node port type service named
web-xfusion
using node port30008
to expose the web server running within the pod.
apiVersion: v1
kind: Service
metadata:
name: web-xfusion
spec:
type: NodePort
selector:
app: pv-nginx
ports:
- port: 80
targetPort: 80
nodePort: 30008 # Expose the web server
Finally Cleanup
$ kubectl delete svc web-xfusion
service "web-xfusion" deleted
$ kubectl delete pod pod-xfusion
pod "pod-xfusion" deleted
$ kubectl delete pvc pvc-xfusion
persistentvolumeclaim "pvc-xfusion" deleted