How to Deploy an EmptyDir to a Kubernetes Cluster

Kubernetes EmptyDir is a local directory that shares its storage volume with one or more containers for the goal of data sharing. Although it can be mounted on a hostPath volume or an Inline Volume, it cannot be used as a Persistent Volume Claim (PVC).

Our previous post covered EmptyDir and when it is a good idea to use them.

This post will focus on creating a deployment and attaching an EmptyDir volume to containers inside a pod.

The below example will create a deployment as usual. In the bottom part of the deployment, we mount a volume called data to the container. The volume type is emptyDir.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: web-app
  template:
    metadata:
      labels:
        app: web-app
    spec:
      containers:
        - name: web-app
          image: nginx
          volumeMounts:
            - name: data
              mountPath: /data
      volumes:
        - name: data
          emptyDir: {}    
      restartPolicy: Always 

To create the deployment, I am using the following command.

kubectl apply -f deployment.yaml

Posted

in

by