HiveBrain v1.2.0
Get Started
← Back to all entries
snippetkubernetesMinor

How to create named volumes in Kubernetes?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
volumescreatekubernetesnamedhow

Problem

Several named volumes have been created in docker-compose. How to create these in Kubernetes?

How to transform the following named volume example defined in docker-compose to kubernetes format

version: "3"
services:
  x:
    image: z
    volumes:
      - a:/path/inside/container
  y:
    image: c
    volumes:
      - a:/path/inside/container
volumes:
  a:

Solution

k8s have documentation page about volumes and their types https://kubernetes.io/docs/concepts/storage/volumes/

So in short you need to define spec.volumes and spec.containers[].volumeMounts like:

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: gcr.io/google_containers/test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /cache
      name: cache-volume
  volumes:
  - name: cache-volume
    emptyDir: {}

Code Snippets

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: gcr.io/google_containers/test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /cache
      name: cache-volume
  volumes:
  - name: cache-volume
    emptyDir: {}

Context

StackExchange DevOps Q#1812, answer score: 5

Revisions (0)

No revisions yet.