Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Kubernetes - Storage: Volume - emptyDir

Back


emptyDir


Declarative Manifest


Classic use case: work with init container

apiVersion: v1
kind: Pod
metadata:
  name: demo-init-db
spec:
  volumes:
    - name: initdb
      emptyDir: {}
    - name: db-data
      emptyDir: {}
  # init container
  initContainers:
    - name: installer
      image: installer_image
      volumeMounts:
        - name: initdb
          mountPath: /initdb.d
  # container
  containers:
    - name: mongo
      image: mongo
      volumeMounts:
        - name: initdb
          mountPath: /docker-entrypoint-initdb.d/ # use entry point for init script
          readOnly: true
        - name: db-data
          mountPath: /data/db

Lab: emptyDir - share file

# demo-emptydir-sharefile.yaml
apiVersion: v1
kind: Pod
metadata:
  name: demo-emptydir-sharefile
spec:
  volumes:
    - name: shared-data
      emptyDir: {}
  # no restart
  restartPolicy: Never

  containers:
    - name: file-writer
      image: busybox
      volumeMounts:
        - name: shared-data
          mountPath: /app-data
      command:
        [
          "/bin/sh",
          "-c",
          "echo '<h1>Hello from the data writer container!</h1>' > /app-data/index.html; sleep 10",
        ]

    - name: nginx
      image: nginx:alpine
      volumeMounts:
        - name: shared-data
          mountPath: /usr/share/nginx/html
      ports:
        - containerPort: 80
kubectl apply -f demo-emptydir-sharefile.yaml
# pod/demo-emptydir-sharefile created

kubectl get pod
# NAME                      READY   STATUS     RESTARTS      AGE
# demo-emptydir-sharefile   1/2     NotReady   0             23s

kubectl port-forward demo-emptydir-sharefile 8080:80
# Forwarding from 127.0.0.1:8080 -> 80
# Forwarding from [::1]:8080 -> 80

curl localhost:8080
# <h1>Hello from the data writer container!</h1>