Logo

dev-resources.site

for different kinds of informations.

Kubernets Storage Demos

Published at
1/2/2025
Categories
kubernetes
pv
pvc
cka
Author
cheedge_lee
Categories
4 categories in total
kubernetes
open
pv
open
pvc
open
cka
open
Author
11 person written this
cheedge_lee
open
Kubernets Storage Demos

In my previous post, shows the basic concepts for Storage in kubernetes, here I want to use some simplified demos to show how these concepts exactly used in a cluster.

Here we use one Emphemral volume -- HostPath type, and a Persistent Volume -- local type as two examples:

HostPath Type

HostPath Type is an empheral volume, even if it will keep the stored file after Pod deleted.

Main Difference:

  • No PVC, directly bound with Pod
  • Manually create affinity for Pod
apiVersion: v1
kind: PersistentVolume
metadata:
  name: hostpath-pv
spec:
  capacity:
    storage: 100Mi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /mnt/data # if not exist will create

---
apiVersion: v1
kind: Pod
metadata:
  name: pod-using-hostpath
spec:
  affinity:
    nodeAffinity: # host path should have the nodeAffinity item
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
            - key: kubernetes.io/hostname
              operator: In
              values:
              - node01
  volumes:
    - name: hostpath-volume
      hostPath:
        path: /mnt/data
  containers:
    - name: app
      image: busybox
      command: ["sleep", "3600"]
      volumeMounts:
        - mountPath: /data
          name: hostpath-volume
Enter fullscreen mode Exit fullscreen mode

Local Type

local type is Persistent Volume type, therefore we need create a PVC, and also with a StorageClass.

Main Difference:

  • PVC
  • SC
  • PV need nodeAffinity
  • Pod will be assigned according to the node of PV automatically
apiVersion: v1
kind: PersistentVolume
metadata:
  name: local-pv
spec:
  capacity:
    storage: 100Mi
  accessModes:
    - ReadWriteOnce
  storageClassName: local-storage # just a template
  local:
    path: /mnt/data # this should exist
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - node01

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


---
apiVersion: v1
kind: Pod
metadata:
  name: pod-using-local
spec:
  volumes:
    - name: local-volume
      persistentVolumeClaim:
        claimName: local-pvc
  containers:
    - name: app
      image: busybox
      command: ["sleep", "3600"]
      volumeMounts:
        - mountPath: /data
          name: local-volume
Enter fullscreen mode Exit fullscreen mode

Reuse the PV

If we have delete the PV, but still want to continue using the content stored, we need to specify the claimRef field and make sure the persistentVolumeReclaimPolicy field is Retain (for local type the official documnet DON'T recommend set persistentVolumeReclaimPolicy: Delete.
for more details of reuse the PV, pls check the official documents: here.

cka Article's
30 articles in total
Favicon
Turning Markdown into Learning: publishing a challenge on labs.iximiuz.com
Favicon
CKA Quick Check Points -- RBAC
Favicon
CKA Quick Check Points -- Network
Favicon
CKA Quick Check Points -- Logs & Configs
Favicon
CKA Recap -- Deployment
Favicon
ClusterIP vs. NodePort
Favicon
CKA Recap -- Ingress & NetworkPolicy
Favicon
Kubernets Storage Demos
Favicon
3. CKA Storage
Favicon
Why must a Kubernetes cluster have an odd number of nodes
Favicon
Understanding Pod Topology Spread Constraints in Kubernetes
Favicon
Deployments and Replica Sets in Kubernetes
Favicon
40 days of Kubernetes: Docker Fundamentals
Favicon
Understanding Kubernetes Pods and YAML Fundamentals
Favicon
Creating a Kubernetes Cluster with Kubeadm and Containerd: A Comprehensive Step-by-Step Guide
Favicon
Mastering Multi-Stage Builds in Docker 🚀
Favicon
Mastering Docker Fundamentals: The First Step in Becoming a Certified Kubernetes Administrator
Favicon
How I Passed the Certified Kubernetes Administrator (CKA) Exam and How You Can Too
Favicon
AWS EKS Ingress - Canary
Favicon
Certified Kubernetes Administrator (CKA) Prep.
Favicon
Certified Kubernetes Administrator (CKA) - Several Successful Story
Favicon
YouTube 13k Subscriber crossed! Exciting Giveaway
Favicon
How to Pass the Certified Kubernetes Administrator Examination
Favicon
'Kubernetes Complete Course In 10 Hours' – your ultimate beginner's guide to Kubernetes FREE!
Favicon
Kubernetes CKA Exam Question Bank .. 17 Questions with Complete Lab .. In 3 hours .. FREE!
Favicon
ROAD TO CKA [Certified Kubernetes Administrator ]-Core Concept- Pod(Day-7)
Favicon
ROAD TO CKA [Certified Kubernetes Administrator ]-Kubernetes Architecture(Day-4)
Favicon
Installing Kubernetes-1.27.0 in Ubuntu 22.04
Favicon
ROAD TO CKA [Certified Kubernetes Administrator ]-What Kubernetes is & is not?(Day-3)
Favicon
ROAD TO CKA [Certified Kubernetes Administrator ]-Core Concept- Control Plane/Master Node (Day-5)

Featured ones: