Monday 18 April 2022

CKA Kubernetes ( K8S ) Storage

 The preparation for the CKA (Certified Kubernetes Administrator) requires lots of practice and practice. Fortunately, we have lots of online playgrounds to keep practicing, there are lots of free courseware available and lots of paid as well are available. In addition to that, we get two attempts to clear the exam



Docker Storage Driver

• When we run the docker container, the docker creates another layer called as 'Container Layer' as a writable layer to store content like logs, temp file created by app or to modify the existing file. The files in the container layer will be lost when the container stops
• Commands
○ docker volume create data_volume
§ this will create a directory under /var/lib/docker/data_volume
○ docker run -v data_volume:/var/lib/mysql mysql
§ Here it mounts the volume to the 
○ docker run -v data_volume2:/var/lib/docker/data_volume2
§ It creates a folder /var/lib/docker/data_volume2
○ docker run -v /data/mysql:/var/lib/mysql mysql
§ Local folder is mounted
○ docker run --mount type=bind,source=/data/mysql,target=/var/lib/mysql mysql
• Docker uses storage driver for
○ Creating the writable layered and maintaining the files in it and terminating when the container stops etc
○ There are many drivers like AUFS, ZFS, BTRFS etc
○ Docker itself will choose the best driver based on the native operating system. But we can override it

Docker Volume Driver

• Default driver is 'Local' which will use the host OS filesystem
• There are many other drivers like
○ AzureFileStorage
○ DigitalOcean
○ gce-docker
○ convoy etc

Container Interfaces

• Container Runtime Interfaces
○ Used to abstract the runtime containers like docker, rkt, cri-o etc
○ If any new runtime container support is introduced they simply have to follow the CRI docs and can implement without touching the k8s code
• Container Network Interfaces
○ Used to abstract the networking implementation used to support communication between nodes, pods etc
○ Some examples are flannel, weaveworks, cilium
• Container Storage Interfaces
○ Used to abstract the underlying storage used by using drivers like portworx, Amazon EBS, Dell EMC, Gluster FS
○ CSI is not K8S standard, it is universal standard. So if any storage vendor has the contract for CSI then it can be plugged

Volumes and Mounts

• When we create a POD, under the spec we can define the list of volumes under spec and the mounts under the containers
• There are multiple volume providers like the 'hostPath' which creates a volume in current running node. Apart from that there are many providers for it

PersistentVolume

• It allows administrator to define a different set of storage options using the persistent volume and the POD can use one of them. This gives the advantage that now each pod definition dont have to maintain all the storage configuration within itself

apiVersion: v1
kind: PersistentVolume
metadata:
  name:  pv-log
spec:
  capacity:
    storage: 100Mi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: /pv/log

PersistentVolumeClaim

• PVC is another k8s object created by the user with definition like requires storage size, mode etc
• Once the PVC is created the by the user, the kubernetes binds the PVC with PV
• PVC and PV are 1-1 means like only one claim can be made to a PV. Even if there are free space in PV, it cannot accommodate additional PVC 

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: claim-log-1
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 50Mi
  volumeName: pv-log

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: webapp
  name: webapp
spec:
  containers:
  - image: kodekloud/event-simulator
    name: pod
    resources: {}
    volumeMounts:
    - mountPath: /log
      name: log-pvc
  volumes:
  - name: log-volume
    hostPath:
     path: /var/log/webapp
  - name: log-pvc
    persistentVolumeClaim:
      claimName: claim-log-1
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
# Storage Class
• Creating a PV and creating a storage type like aws, gce are called as static provisioning

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer

apiVersion: v1
kind: PersistentVolume
metadata:
  name:  local-pv
spec:
  capacity:
    storage: 500Mi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /opt/vol1
  storageClassName: local-storage


apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: local-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 500Mi
  storageClassName: local-storage

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  containers:
  - image: nginx:alpine
    name: nginx
    resources: {}
    volumeMounts:
      - mountPath: "/var/www/html"
        name: volume-html
  volumes:
    - name: volume-html
      persistentVolumeClaim:
        claimName: local-pvc
  dnsPolicy: ClusterFirst
  restartPolicy: Always

No comments:

Post a Comment