Kubernetes unknown field "volumes"

12/21/2017

I am trying to deploy a simple nginx in kubernetes using hostvolumes. I use the next yaml:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: webserver
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: webserver
    spec:
      containers:
      - name: webserver
        image: nginx:alpine
        ports:
        - containerPort: 80
        volumeMounts:
        - name: hostvol
          mountPath: /usr/share/nginx/html
    volumes:
    - name: hostvol
      hostPath:
        path: /home/docker/vol

When I deploy it kubectl create -f webserver.yaml, it throws the next error:

error: error validating "webserver.yaml": error validating data: ValidationError(Deployment.spec.template): unknown field "volumes" in io.k8s.api.core.v1.PodTemplateSpec; if you choose to ignore these errors, turn validation off with --validate=false
-- Asier Gomez
kubernetes

1 Answer

12/21/2017

I believe you have the wrong indentation. The volumes key should be at the same level as containers.

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: webserver
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: webserver
    spec:
      containers:
      - name: webserver
        image: nginx:alpine
        ports:
        - containerPort: 80
        volumeMounts:
        - name: hostvol
          mountPath: /usr/share/nginx/html
      volumes:
      - name: hostvol
        hostPath:
          path: /home/docker/vol

Look at this wordpress example from the documentation to see how it's done.

-- Jose Armesto
Source: StackOverflow