About Me

My photo
I am MCSE in Data Management and Analytics with specialization in MS SQL Server and MCP in Azure. I have over 13+ years of experience in IT industry with expertise in data management, Azure Cloud, Data-Canter Migration, Infrastructure Architecture planning and Virtualization and automation. Contact me if you are looking for any sort of guidance in getting your Infrastructure provisioning automated through Terraform. I sometime write for a place to store my own experiences for future search and read by own blog but can hopefully help others along the way. Thanks.

Ephemeral storage & Persistent storage in AKS difference and how to expand a PVC from 1 GB to 60 GB

Ephemeral storage 

--------------------------------------

In a Kubernetes cluster, storage can be categorized into two types: ephemeral storage and persistent storage. Ephemeral storage is temporary storage that is tied to the lifecycle of a pod, while persistent storage retains data even when the pod is deleted or restarted.

Ephemeral storage is local to the node where the pod is running. This means that the capacity and availability of ephemeral storage depend on the node's resources. The most common use of ephemeral storage is as a scratch space for temporary files or as a cache. Examples of ephemeral storage include emptyDirs and hostPaths.

EmptyDirs are Kubernetes volumes that are created and destroyed with a pod's lifecycle. When a pod is deleted, its emptyDir is deleted along with it. HostPaths are volumes that are mounted from the host's file system. They are useful for testing, but they have several limitations, such as reduced portability and security risks.

Ephemeral storage is useful for applications that don't require data persistence or for data that can be easily regenerated or recreated. Ephemeral storage is also useful for stateless applications, such as web servers or microservices.

Example  of ephemeral storage 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webserver-deployment
  namespace: rak
spec:
  replicas: 1
  selector:
    matchLabels:
      app: webserver
  template:
    metadata:
      labels:
        app: webserver
    spec:
      containers:
      - name: webserver
        image: nginx:latest
        ports:
        - containerPort: 80
        volumeMounts:
        - name: html
          mountPath: /usr/share/nginx/html
      volumes:
      - name: html
        emptyDir: {}

---

apiVersion: v1
kind: Service
metadata:
  name: webserver-service
  namespace: rak
spec:
  selector:
    app: webserver
  ports:
  - name: http
    port: 80
    targetPort: 80
  type: LoadBalancer

---

Persistent storage 

-----------------------------------

Persistent storage, on the other hand, retains data even when the pod is deleted or restarted. Persistent storage can be provisioned independently of the nodes and can be scaled up or down as needed. Persistent storage is commonly used for storing application data, databases, and logs.

In AKS, persistent storage can be provided using Azure Disks, Azure Files, or other cloud-based storage solutions. Azure Disks are durable and performant block storage solutions that are optimized for running databases and other I/O intensive workloads. Azure Files are fully managed file shares that can be accessed from anywhere using the SMB protocol.

Persistent storage can be provisioned using PersistentVolumeClaims (PVCs), which are used to request storage from a storage provider. The PVC is a request for a certain amount of storage with a specific storage class. The storage provider provisions the storage, and the PVC is bound to a PersistentVolume (PV). The PV is a representation of a physical volume, such as a disk or a file share, that can be mounted to a pod.

The storage class is used to specify the type of storage that is requested. In AKS, there are several storage classes available, such as standard, premium, and ultra disks. The storage class determines the performance and cost characteristics of the storage.

Persistent storage is useful for applications that require data persistence, such as databases or file shares. Persistent storage is also useful for stateful applications, such as stateful sets or replicated databases.

The choice between ephemeral and persistent storage depends on the application's requirements for data retention, availability, and scalability. Ephemeral storage is useful for applications that don't require data persistence or for data that can be easily regenerated or recreated. Persistent storage is useful for applications that require data persistence and for stateful applications.


In conclusion, ephemeral storage and persistent storage are two types of storage in a Kubernetes cluster. Ephemeral storage is temporary and local to a pod, while persistent storage is durable and independent of a pod's lifecycle. The choice between ephemeral and persistent storage depends on the application's requirements for data retention, availability, and scalability. AKS provides several storage solutions, such as Azure Disks and Azure Files, that can be used to provide persistent storage for Kubernetes workloads.

Example of Persistent Storage


apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: html-pvc
  namespace: rak
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: webserver-deployment-persist
  namespace: rak
spec:
  replicas: 1
  selector:
    matchLabels:
      app: webserver
  template:
    metadata:
      labels:
        app: webserver
    spec:
      containers:
      - name: webserver
        image: nginx:latest
        ports:
        - containerPort: 80
        volumeMounts:
        - name: html
          mountPath: /usr/share/nginx/html
      volumes:
      - name: html
        persistentVolumeClaim:
          claimName: html-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: webserver-service--persist
  namespace: rak
spec:
  selector:
    app: webserver
  ports:
  - name: http
    port: 80
    targetPort: 80
  type: LoadBalancer

A PV will get created


A PVC will get created



under the default storage class 




1 GB Disk will get created and will appear under Management resource group of AKS.

How to expand the Persistent volume claim for a pod in a deployment

suppose you have a PVC, whose size is 1 GB

C:\Users\kusha>kubectl get pvc -n rak-persist
NAME       STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
html-pvc   Bound    pvc-889c4819-de22-4d03-b2bf-d4147323de8d   1Gi        RWO            default        53m

PVC is in bound state and it is claimed by PV -->  pvc-889c4819-de22-4d03-b2bf-d4147323de8d

C:\Users\kusha>kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                                           STORAGECLASS   REASON   AGE
pvc-7c018ca6-0075-4b86-bda6-4bc7c50a0225   60Gi       RWO            Delete           Bound    mongodb/mongodb-storage-mongodb-statefulset-0   managed-csi             4d12h
pvc-889c4819-de22-4d03-b2bf-d4147323de8d   1Gi        RWO            Delete           Bound    rak-persist/html-pvc                            default                 55m

Check PVC is used by which POD, in order to validate this you will execute the command 

kubectl describe pvc -n rak-persist

check the value of a column used By 
Used By:       webserver-deployment-persist-86b4975964-cf2z5

now check pod is managed by either deployment/statefulsets

kubectl describe pod webserver-deployment-persist-86b4975964-cf2z5 -n rak-persist
check controlled by column

Controlled By:  ReplicaSet/webserver-deployment-persist-86b4975964

How to check Replicaset is controlled by which deployment 


C:\Users\kusha>kubectl describe replicasets webserver-deployment-persist-86b4975964 -n rak-persist
  Controlled By:  Deployment/webserver-deployment-persist

Now we got deployment who is consuming this PVC

Now Scale down deployment to 0

Scaling down a deployment to 0 means that you want to completely stop the deployment and have no instances running. This can be achieved by adjusting the number of replicas of the deployment to 0.
 The method to scale down a deployment to 0 may differ based on the deployment tool or platform that you are using, but here are some general steps that you can follow:
 Use the appropriate command or interface to access the deployment that you want to scale down.
 Locate the configuration settings for the deployment's replicas.
 Change the number of replicas to 0. This can be done by setting the replicas field to 0 in the deployment configuration file or by using a command such as 
    kubectl scale deployment [deployment-name] --replicas=0 for Kubernetes.
 Save the changes to the configuration file or execute the command to update the deployment's settings.
Wait for the deployment to scale down to 0 instances. 
This may take a few minutes depending on the deployment tool and the size of the deployment.
Once the deployment has been scaled down to 0 instances, the deployment will be completely stopped and no resources will be consumed. 
Keep in mind that if you want to restart the deployment later, you will need to adjust the number of replicas back to a positive value.

Now you have to scale down the deployment 

kubectl scale deployment webserver-deployment-persist --replicas=0

C:\Users\kusha>kubectl scale deployment webserver-deployment-persist --replicas=0 -n rak-persist

deployment.apps/webserver-deployment-persist scaled

C:\Users\kusha>kubectl get pods -n rak-persist
No resources found in rak-persist namespace.

Now expand the PVC

kubectl edit pvc html-pvc   -n rak-persist

expand the storage at 
   
   spec.resources.requests.storage = 60Gi from 1 Gi

and save the file


if you will edit the pvc again, you will get a message like:-

status:
  accessModes:
  - ReadWriteOnce
  capacity:
    storage: 1Gi
  conditions:
  - lastProbeTime: null
    lastTransitionTime: "2023-04-10T03:36:40Z"
    message: Waiting for user to (re-)start a pod to finish file system resize of
      volume on node.
    status: "True"
    type: FileSystemResizePending
  phase: Bound

hence scale up the deployment, so the pod will get created.. 
scale up the deployment

C:\Users\kusha>kubectl scale deployment webserver-deployment-persist --replicas=1 -n rak-persist
deployment.apps/webserver-deployment-persist scaled

C:\Users\kusha>kubectl get pods -n rak-persist
NAME                                            READY   STATUS              RESTARTS   AGE
webserver-deployment-persist-86b4975964-spznk   0/1     ContainerCreating   0          7s


C:\Users\kusha>kubectl get pods -n rak-persist
NAME                                            READY   STATUS    RESTARTS   AGE
webserver-deployment-persist-86b4975964-spznk   1/1     Running   0          49s

Now finally check your PVC expanded or not


C:\Users\kusha>kubectl get pvc -n rak-persist
NAME       STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
html-pvc   Bound    pvc-889c4819-de22-4d03-b2bf-d4147323de8d   60Gi       RWO            default        88m






1 comment:

Noelletessaa said...

Wow, this is really informative!

If you're serious about investing in cryptocurrencies, you need a trustworthy exchange like Opris to make sure your transactions are safe and secure. As a leading provider of cryptocurrency exchange software , Opris offers top-of-the-line security measures, user-friendly interfaces, and a wide range of trading options to meet the needs of all types of investors.

paxful clone script
binance clone script
coinbase clone script
kucoinnclone script
wazirx clone script
kraken clone script
huobi clone script