K8s Volumes

Deployment file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:latest
          ports:
            - containerPort: 80
          volumeMounts:
            - name: nginx-html
              mountPath: /usr/share/nginx/html/index.html
              subPath: index.html

            - name: nginx-data
              mountPath: /var/www/data
      volumes:
        - name: nginx-html      
          configMap:
            name: nginx-html
      
        - name: nginx-data
          persistentVolumeClaim:
            claimName: nginx-pv-claim

ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-html
data:
  index.html: |
    <!DOCTYPE html>
    <html>
    <head>
      <title>My NGINX Page</title>
    </head>
    <body>
      <h1>Hello from NGINX!</h1>
      <p>Running in Kubernetes.</p>
    </body>
    </html>

Persistent Volume

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nginx-pv
spec:
  capacity:
    storage: 1Gi

  accessModes:
    - ReadWriteOnce

  persistentVolumeReclaimPolicy: Retain

  hostPath:
    path: /data/nginx-pv

  # This tells Kubernetes which node has the hostPath data
  nodeAffinity:
    required:
      nodeSelectorTerms:
        - matchExpressions:
            - key: kubernetes.io/hostname
              operator: In
              values:
                - NODE-NAME

Persistent Volume Claim

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nginx-pv-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi