DevOps Classroom notes 17/Oct/2026

Hostpath Volume:

  • A hostpth volume in k8s mount as filesystem or directory.
  • it will create same node where my pod will deploy.

Host Volume

apiVersion: apps/v1
kind: Deployment
metadata:
  name: recreate-deployment-app
  labels:
    app: apache2
spec:
  replicas: 2
  selector:
    matchLabels:
      app: apache2
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: apache2
    spec:
      initContainers:
        - name: download-template
          image: alpine:latest
          command:
            - sh
            - -c
            - |
              set -e
              apk add --no-cache wget unzip
              wget -O /tmp/template.zip \
                https://templatemo.com/tm-zip-files-2020/templatemo_589_lugx_gaming.zip
              mkdir -p /tmp/template
              unzip -o /tmp/template.zip -d /tmp/template
              cp -a /tmp/template/templatemo_589_lugx_gaming/. \
                /usr/local/apache2/htdocs/
          volumeMounts:
            - name: apache2-html
              mountPath: /usr/local/apache2/htdocs/

      containers:
        - name: apache2
          image: httpd:2
          ports:
            - containerPort: 80
          volumeMounts:
            - name: apache2-html
              mountPath: /usr/local/apache2/htdocs/

      volumes:
        - name: apache2-html
          hostPath:
            path: /var/data/apache2-htdocs
            type: DirectoryOrCreate

sample test

Configmap & Secrets mount volume:

  1. create configMap
apiVersion: v1
Kind: ConfigMap
metadata:
  name: app-config
data:
  location: hyd
  app.properties: |
    mode=production
    log_level=info

Add configmap volume mount pah

apiVersion: apps/v1
kind: Deployment
metadata:
  name: recreate-deployment-app
  labels:
    app: apache2
spec:
  replicas: 2
  selector:
    matchLabels:
      app: apache2
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: apache2
    spec:
      initContainers:
        - name: download-template
          image: alpine:latest
          command:
            - sh
            - -c
            - |
              set -e
              apk add --no-cache wget unzip
              wget -O /tmp/template.zip \
                https://templatemo.com/tm-zip-files-2020/templatemo_589_lugx_gaming.zip
              mkdir -p /tmp/template
              unzip -o /tmp/template.zip -d /tmp/template
              cp -a /tmp/template/templatemo_589_lugx_gaming/. \
                /usr/local/apache2/htdocs/
          volumeMounts:
            - name: apache2-html
              mountPath: /usr/local/apache2/htdocs/
      containers:
        - name: apache2
          image: httpd:2
          ports:
            - containerPort: 80
          volumeMounts:
            - name: apache2-html
              mountPath: /usr/local/apache2/htdocs/
            - name: app-config-data
              mountPath: /etc/app
      volumes:
        - name: apache2-html
          hostPath:
            path: /var/data/apache2-htdocs
            type: DirectoryOrCreate
        - name: app-config-data
          configMap:
            name: app-config
  1. secret mount
kubectl create secret generic rds-secret \
  --from-literal=DB_HOST=://amazonaws.com \
  --from-literal=DB_PORT=5432 \
  --from-literal=DB_USER=dbadmin \
  --from-literal=DB_PASSWORD=yoursecurepassword

Generate yaml by using --dry-run=client -o yaml

To create secret by yaml

apiVersion: v1
data:
  DB_HOST: Oi8vYW1hem9uYXdzLmNvbQ==
  DB_PASSWORD: eW91cnNlY3VyZXBhc3N3b3Jk
  DB_PORT: NTQzMg==
  DB_USER: ZGJhZG1pbg==
kind: Secret
metadata:
  name: rds-secret
apiVersion: apps/v1
kind: Deployment
metadata:
  name: recreate-deployment-app
  labels:
    app: apache2
spec:
  replicas: 2
  selector:
    matchLabels:
      app: apache2
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: apache2
    spec:
      initContainers:
        - name: download-template
          image: alpine:latest
          command:
            - sh
            - -c
            - |
              set -e
              apk add --no-cache wget unzip
              wget -O /tmp/template.zip \
                https://templatemo.com/tm-zip-files-2020/templatemo_589_lugx_gaming.zip
              mkdir -p /tmp/template
              unzip -o /tmp/template.zip -d /tmp/template
              cp -a /tmp/template/templatemo_589_lugx_gaming/. \
                /usr/local/apache2/htdocs/
          volumeMounts:
            - name: apache2-html
              mountPath: /usr/local/apache2/htdocs/
      containers:
        - name: apache2
          image: httpd:2
          ports:
            - containerPort: 80
          volumeMounts:
            - name: apache2-html
              mountPath: /usr/local/apache2/htdocs/
            - name: app-config-data
              mountPath: /etc/app
            - name: rds-secret-pg
              mountPath: /etc/secrets
              readOnly: true
      volumes:
        - name: apache2-html
          hostPath:
            path: /var/data/apache2-htdocs
            type: DirectoryOrCreate
        - name: app-config-data
          configMap:
            name: app-config
        - name: rds-secret-pg
          secret:
            secretName: rds-secret

Presistance volume:

Offical docs

  • k8s pv will suppourt multiple back storages

PV in K8s

Offical docs for PV API referance

accessModes in volume:

RWO ==> ReadWirteOnce (Read-wirte by a single node) – local storage RWM ==> ReadWirteMany (Read-wirte by many nodes) – NFS ROM ==> ReadOnlyMany (Read-only by many nodes) RWOP ==> ReadWirteOncePod (single pod having read-wirte once)

Actions required

sudo mkdir -p /mnt/data
sudo chmod 777 /mnt/data

Create PV, PVC and deployment

apiVersion: v1
kind: PersistentVolume
metadata:
  name: sample-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  volumeMode: Filesystem
  local:
    path: /mnt/data
    fsType: ext4
  nodeAffinity:
    required:
      nodeSelectorTerms:
        - matchExpressions:
            - key: kubernetes.io/hostname
              operator: In
              values:
                - ip-172-31-39-231

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: sample-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: local-storage
  volumeName: sample-pv

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: recreate-deployment-app
  labels:
    app: apache2
spec:
  replicas: 2
  selector:
    matchLabels:
      app: apache2
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: apache2
    spec:
        
      initContainers:
        - name: download-template
          image: alpine:latest
          command:
            - sh
            - -c
            - |
              set -e

              apk add --no-cache wget unzip

              wget -O /tmp/template.zip \
                https://templatemo.com/tm-zip-files-2020/templatemo_589_lugx_gaming.zip

              mkdir -p /tmp/template

              unzip -o /tmp/template.zip -d /tmp/template

              cp -a /tmp/template/templatemo_589_lugx_gaming/. \
                /usr/local/apache2/htdocs/
          volumeMounts:
            - name: apache2-html
              mountPath: /usr/local/apache2/htdocs/

      containers:
        - name: apache2
          image: httpd:2
          ports:
            - containerPort: 80
          volumeMounts:
            - name: apache2-html
              mountPath: /usr/local/apache2/htdocs/

      volumes:
        - name: apache2-html
          persistentVolumeClaim:
            claimName: sample-pvc

Task:

  1. create a pv name qt-pv
  • capacity: 2GI
  • VolumeType:local
  • Hostpath: /qt/data
  • Nodeaffinity: spcific node
  1. create pvc

    1. request: 1Gi
    2. access: rwo
    3. bind pv volumeName: sample-pv
  2. attach this volume to deployment:

    1. nginx
    2. replic2
    3. /usr/share/nginx/html -volume mount

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Animated Social Media Icons by Acurax Responsive Web Designing Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube