Issues mounting hostPath in kubernetes

7/28/2017

Alright, I'm trying to set up a local version of a Jenkins container in kubernetes' minikube and I was using this guide as a model.

The problem that I'm having is that the hostPath does not seem to be mounting correctly.

When I try to mount the host path without using a "persistentVolumeClaim" my code looks like this:

deployment_jenkins.yaml

apiVersion: extensions/v1beta1

kind: Deployment
metadata:
  name: jenkins
  namespace: jenkins
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: master
    spec:
      containers:
      - name: master
        image: jenkins:2.7.2
        ports:
        - containerPort: 8080
        - containerPort: 50000
        readinessProbe:
          httpGet:
            path: /login
            port: 8080
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 2
          failureThreshold: 5
        env:
        - name: JENKINS_OPTS
          valueFrom:
            secretKeyRef:
              name: jenkins
              key: options
        volumeMounts:
        - mountPath: /var/jenkins_home
          name: jenkins-home
        resources:
          limits:
            cpu: 500m
            memory: 1500Mi
          requests:
            cpu: 500m
            memory: 1500Mi
      volumes:
      - name: jenkins-home
      hostPath:
        path: /data

and I get this error when I run kubectl apply-f deployment_jenkins.yaml:

my-mac kubernetes $ kubectl apply -f deployment_jenkins.yaml
error: error validating "deployment_jenkins.yaml": error validating data: 
found invalid field hostPath for v1.PodSpec; if you choose to ignore these 
errors, turn validation off with --validate=false

and then when I try to use claims, I get this:

persistent_volume_claim_jenkins.yaml

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: jenkins-home-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  volumeName: jenkins-home

persistent_volume_jenkins.yaml

kind: PersistentVolume
apiVersion: v1
metadata:
  name: jenkins-home
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 5Gi
  hostPath:
    path: /data

deployment_jenkins.yaml

apiVersion: extensions/v1beta1

kind: Deployment
metadata:
  name: jenkins
  namespace: jenkins
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: master
    spec:
      containers:
      - name: master
        image: jenkins:2.7.2
        ports:
        - containerPort: 8080
        - containerPort: 50000
        readinessProbe:
          httpGet:
            path: /login
            port: 8080
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 2
          failureThreshold: 5
        env:
        - name: JENKINS_OPTS
          valueFrom:
            secretKeyRef:
              name: jenkins
              key: options
        volumeMounts:
        - mountPath: /var/jenkins_home
          name: jenkins-home
        resources:
          limits:
            cpu: 500m
            memory: 1500Mi
      requests:
        cpu: 500m
        memory: 1500Mi
      volumes:
      - name: jenkins-home
        persistentVolumeClaim:
          claimName: jenkins-home-claim

And then I run the command:

my-mac kubernetes $ kubectl apply -f .
deployment "jenkins" configured
persistentvolumeclaim "jenkins-home-claim" configured
persistentvolume "jenkins-home" configured

And ultimately, my pod never spins up and I get these errors in the dashboard: enter image description here

I'm super confused and any help in figuring out what I'm doing wrong and how to get this to work would be greatly appreciated.

-- user1079703
kubernetes
minikube

1 Answer

7/28/2017

Some of your yaml is mis-indented leading to a different (broken) interpretation:

volumes: - name: jenkins-home hostPath: path: /data

This is an object with two fields, volumes and hostPath. What you want, instead, is:

volumes: - name: jenkins-home hostPath: path: /data

Also, I recommend always quoting (non-block) strings in your yaml to avoid surprises:

volumes: - name: 'jenkins-home' hostPath: path: '/data'

-- Jean-Paul Calderone
Source: StackOverflow