Solution

2 minutes read
  1. The specification to define the PersistentVolumeClaim named redis:
apiVersion: v1
kind: PersistentVolumeClaim
metadata: 
  name: redis
spec: 
  accessModes:
    - ReadWriteOnce
  resources:
    requests: 
      storage: 1Gi

The redis Deployment is modified as follows:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: redis
  name: redis
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
        - image: redis:7.0.8-alpine3.17
          name: redis
          volumeMounts:
          - name: data
            mountPath: /data
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: redis
  1. The specification to define the PersistentVolumeClaim named db:
apiVersion: v1
kind: PersistentVolumeClaim
metadata: 
  name: db
spec: 
  accessModes:
    - ReadWriteOnce
  resources:
    requests: 
      storage: 1Gi

The db Deployment is modified as follows:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: db
  name: db
spec:
  replicas: 1
  selector:
    matchLabels:
      app: db
  template:
    metadata:
      labels:
        app: db
    spec:
      containers:
        - image: postgres:15.1-alpine3.17
          name: postgres
          env:
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: db
                  key: password
          volumeMounts:
          - name: data
            mountPath: /var/lib/postgresql/data
          ports:
            - containerPort: 5432
              name: postgres
      volumes:
      - name: data
        persistentVolumeClaim: 
          claimName: db
  1. Deploy the application using the following command from the manifests directory:
kubectl apply -f .

Using the IP address of one of the cluster nodes, you can access the vote and result interfaces via ports 31000 and 31001 respectively.

  1. You can list the PersistentVolumeClaim resources and observe that a PersistentVolume has been created for each of the 2 PVCs.

List of PersistentVolumeClaims:

$ kubectl get pvc
NAME                          STATUS   VOLUME                                     CAPACITY  ACCESS MODES   STORAGECLASS   AGE
persistentvolumeclaim/redis   Bound    pvc-789e3c5c-4402-4b96-b09d-ee441e8ade1d   1Gi       RWO            local-path     39s
persistentvolumeclaim/db      Bound    pvc-75b9a32c-eab5-4452-a9b8-12d41dd74e7a   1Gi       RWO            local-path     39s

List of PersistentVolumes:

$ kubectl get pv
NAME                                                        CAPACITY  ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM           STORAGECLASS   REASON   AGE
persistentvolume/pvc-789e3c5c-4402-4b96-b09d-ee441e8ade1d   1Gi       RWO            Delete           Bound    default/redis   local-path              32s
persistentvolume/pvc-75b9a32c-eab5-4452-a9b8-12d41dd74e7a   1Gi       RWO            Delete           Bound    default/db      local-path              32s
  1. Delete the application using the following command from the manifests directory:
kubectl delete -f .