Saturday, October 9, 2021

Running StorageClass Resource on Kubernetes

Introduction

We will learn creating dynamic volume using StorageClass on Minikube today. 

StorageClass

We will create StorageClass, PersistentVolumeClaim, Pod and Service using yaml:

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: slow
provisioner: k8s.io/minikube-hostpath
parameters:
  type: pd-ssd
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: ashok-pv-claim
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: slow
  resources:
    requests:
      storage: 10Gi
---
apiVersion: v1
kind: Pod
metadata:
  name: ashok-pv-pod
  labels:
    app: hello  
spec:
  volumes:
    - name: ashok-pv-storage
      persistentVolumeClaim:
        claimName: ashok-pv-claim
  containers:
    - name: ashok-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: ashok-pv-storage
---
apiVersion: v1
kind: Service
metadata:
  name: helloweb-svc
  labels:
    app: hello
spec:
  type: LoadBalancer
  ports:
  - port: 8080
    targetPort: 80
  selector:
    app: hello

We need to create an  index.html file for our nignx pod and then use this file in our PersistentVolumeClaim for storage in our Pod.

You can login to Pod using:

$ kubectl exec -it ashok-pv-pod -- /bin/bash
root@ashok-pv-pod:/#
Create index.html file at /usr/share/nginx/html/index.html :

$ echo 'Hello from Kubernetes storage using storage class' > /usr/share/nginx/html/index.html
Create a tunnel to access service in browser:
$ minikube tunnel
🏃  Starting tunnel for service helloweb-svc.
Open the browser  http://localhost:8080/ :



Happy Coding !!


No comments:

Post a Comment