I want to share files between two containers in Kubernetes. Therefore I created a SharedVolumeClaim, which I plan to mount in both containers. But for a start, I tried to mount it in one container only. Whenever I deploy this, two Pods get created.
$ kubectl get pods | grep frontend
dev-frontend-84ca5d6dd6-8n8lf 0/1 ContainerCreating 0 18h
dev-frontend-b4959fb97-f9mgw 1/1 Running 0 18h
The first container is stuck in creating because it can not access the SharedVolume (Volume is already attached by pod fresh-namespace/dev-frontend-b4959fb97-f9mgw
).
But when I remove the code that mounts the volume into the container and deploy again, only one container is created. This also happens if I start with a completely new namespace.
$ kubectl get pods | grep frontend
dev-frontend-587bc7f359-7ozsx 1/1 Running 0 5m
Why does the mount spawn a second pod as it should only be one?
Here are the relevant parts of the deployment files:
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: shared-file-pvc
labels:
app: frontend
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 100Mi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: dev-frontend
labels:
app: frontend
environment: dev
spec:
replicas: 1
revisionHistoryLimit: 0
selector:
matchLabels:
app: frontend
environment: dev
template:
metadata:
labels:
app: frontend
environment: dev
spec:
volumes:
- name: shared-files
persistentVolumeClaim:
claimName: shared-file-pvc
containers:
- name: frontend
image: some/registry/frontend:version
imagePullPolicy: Always
ports:
- containerPort: 8080
resources:
requests:
memory: 100Mi
cpu: 250m
limits:
memory: 300Mi
cpu: 750m
volumeMounts:
- name: shared-files <!-- works if I remove -->
mountPath: /data/shared-files <!-- this two lines -->
---
Can anybody help me, what I am missing here?
Thanks in advance!