[k8s] How to mount local directory (persistent volume) to Kubernetes pods of Docker Desktop for Mac?

  1. Check and add the directories you want to share into your Docker Desktop preferences.
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 1
minReadySeconds: 10
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:latest
...
volumeMounts:
- name: my-volume
mountPath: /data
volumes:
- name: my-volume
hostPath:
path: /tmp
type: DirectoryOrCreate

...
  1. Add the shared folder into Docker Desktop preferences.
#
# PersistentVolume
#
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-persistent-volume
labels:
type: local
spec:
storageClassName: hostpath
capacity:
storage: 256Mi
accessModes:
- ReadWriteMany
hostPath:
path: /tmp

persistentVolumeReclaimPolicy: Retain
---#
# PersistentVolumeClaim
#
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-persistent-volume-claim
spec:
storageClassName: hostpath
accessModes:
- ReadWriteMany
resources:
requests:
storage: 256Mi
apiVersion: v1
kind: StorageClass
apiVersion: storage.k8s.io/v1beta1
metadata:
name: my-storage-class
provisioner: docker.io/hostpath
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 1
minReadySeconds: 10
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:latest
...
volumeMounts:
- name: my-volume
mountPath: /data
volumes:
- name: my-volume
persistentVolumeClaim:
claimName: my-persistent-volume-claim

...

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Website Migration Strategies

7 ways to improve field service with IoT asset visibility

Complete Automation of Deploying and Managing Web Application on Kubernetes using Jenkins.

KitKat 101 : Consumer Expenditure Behavior Analysis

Why a focus on efficiency in software development can cause a lot of damage

Go Lang Tutorial: Conditions

Apache Kafka Starter with Sample Project

Power-of-2-Choices in Practice

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Julien Chen

Julien Chen

More from Medium

Xavki’s newsletter : 20211227 week

ELK Stack in Kubernetes Using Helm

Deploy Prometheus Monitoring Stack with Portainer — Part 2

Getting Started VMware Tanzu Community Edition (Part 1)