X
11May

Volume management in Kubernetes

Since pods created in k8s are ephemeral, we are able to get the data as long as pods are alive, but if pods are terminated data stored in it completely lost and it cannot be get back, for that we need to mount a volume to write the data into that of a pod. To do that we have different ways. Let’s look about those things.

Hostpath:  with this type we can configure a directory on the host itself (node) and specify a path /data.  Once the volume is created, we mount the volume to a directory inside container to access it. Even when the pod deleted, the data processed will be still on the host and it can be get back once the pod is up. Sample YAML for hostpath volumes is as follows;

pods/storage/redis.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: redis
spec:
  containers:
  - name: redis
    image: redis
    volumeMounts:
    - name: redis-storage
      mountPath: /data/redis
  volumes:
  - name: redis-storage
    emptyDir: {}

this type is fine for a single node, but it is not good for multi node clusters. This is because the pods will use /data directory in all the nodes and expects all of them to be the same and have the same data. Since they are on different nodes, they are not same, unless you configured external replicated cluster storage solution. Kubernetes supports several types of storage solutions such as NFS, FLOCKER, EBS, Azure Disk, etc. for example to configure AZURE DISK as the storage option for the volume we will do as follows

On Azure VM, create a Pod using the volume spec based on azure.

In the pod, you need to provide the following information:

  • diskName: (required) the name of the VHD blob object OR the name of an Azure managed data disk if Kind is Managed.
  • diskURI: (required) the URI of the vhd blob object OR the resourceID of an Azure managed data disk if Kind is Managed.
  • kind: (optional) kind of disk. Must be one of Shared (multiple disks per storage account), Dedicated (single blob disk per storage account), or Managed (Azure managed data disk). Default is Shared.
  • cachingMode: (optional) disk caching mode. Must be one of None, ReadOnly, or ReadWrite. Default is None.
  • fsType: (optional) the filesystem type to mount. Default is ext4.
  • readOnly: (optional) whether the filesystem is used as readOnly. Default is false.
#azure.yml
apiVersion: v1
kind: Pod
metadata:
 name: azure
spec:
 containers:
  - image: kubernetes/pause
    name: azure
    volumeMounts:
      - name: azure
        mountPath: /mnt/azure
 volumes:
      - name: azure
        azureDisk:
          diskName: test.vhd
          diskURI: https://someaccount.blob.microsoft.net/vhds/test.vhd
kubectl create -f azure.yaml

persistent volumes:

in the above type we configured the volumes within the pod definition files, when you have a large environment, where lot pods are running inside it, every time u deploy a pod you have to configure volume for each pod. Instead of that we will configure a large  pool of volume centrally, then the pods will use small pieces out of that pool, that is where persistent volumes can help us, A persistent volume (pv) is a cluster wide    pool of storage volumes configured by an administrator to be used by pods on the cluster, and pods can use the storage using volume claims (pvc). We can persistent volumes as below, here I’m not gonna talk about hostpath with pv and pvc because that is not recommended for production oriented. Here I’m using azure disk storage class for dynamic provisioning,

1.create a storage class that is persistent volume

apiVersion: storage.k8s.io/v1
kind: StorageClass 
metadata:
  name: slow
provisioner: kubernetes.io/azure-disk
parameters:
  skuName: Standard_LRS
  location: eastus
  storageAccount: azure_storage_account_name

2.create PVC for that storage class

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-azuredisk-pv1
spec:
  storageClassName: slow
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 500Mi

3.mount the claim to pod

apiVersion: v1 
kind: Pod 
metadata:  
 name: webserver-pd 
spec:  
 containers:  
 - image: httpd    
   name: webserver    
   volumeMounts:    
   - mountPath: /data      
     name: dynamic-volume  
 volumes:  
 - name: dynamic-volume    
   persistentVolumeClaim:      
   claimName: pvc-azuredisk-pv1

apply the yamls to configure and claim the volumes

Related

Quick Understanding on Stateful Sets in Kubernetes

Stateful sets are similar to deployments, they can scale up and scale down, they can perform rolling...

Read More >

How to do Installation of K8S using kubeadm?

Till now I have given blogs on k8s objects, services, namespaces, ingress etc. but where to execute ...

Read More >

HOW TO DO BACKUP AND RESTORE ENTIRE K8S CLUSTER?

If you have deployed different applications on k8s cluster using various objects like deployments, p...

Read More >

Taints and Tolerations

Taints and tolerations are used to restrict the pods to schedule them onto respective nodes. There i...

Read More >

NODE AFFINITY

The k8s node affinity feature is to ensure pods are hosted on a particular node. As mentioned in pre...

Read More >

What is ingress and what are the components of it?

Ingress is a resource, which exposes the http and https from external sources to the services within...

Read More >

Kubernetes RBAC (Role Based Access Control)

The Kubernetes RBAC (role bases access control) system helps us in defining set of rules in controll...

Read More >

Namespaces in Kubernetes

A default namespace is created automatically when the cluster is being setup. To isolate or prevent ...

Read More >

Kubernetes basic Objects

In this article, we will learn about the basic objects of Kubernetes in detail with their respective...

Read More >

Difference Between K8S vs Azure Kubernetes Service (AKS)

Kubernetes aka K8s is container orchestration tool, which helps us to control our containers in the ...

Read More >

Share

Comments

xZAjySiCQXenvcI
5/30/2020 1:25:26 AM | Reply
WzNClFKbyfhoJYL
5/13/2020 2:25:42 PM | Reply

Post a Comment

  • Recent
  • Popular
  • Tag
Monthly Archive
Subscribe
Name

Contact Us
  • *
  • *