Setting up a persitent volume with Kubernetes and Docker Destop for Windows

5/20/2020

I am trying to setup a persistent volume for K8s that is running in Docker Desktop for Windows. The end goal being I want to run Jenkins and not lose any work if docker/K8s spins down.

I have tried a couple of things but I'm either misunderstanding the ability to do this or I am setting something up wrong. Currently I have the environment setup like so:

I have setup a volume in docker for Jenkins. All I did was create the volume, not sure if I need more configuration here.

docker volume inspect jenkins-pv
[
    {
        "CreatedAt": "2020-05-20T16:02:42Z",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/jenkins-pv/_data",
        "Name": "jenkins-pv",
        "Options": {},
        "Scope": "local"
    }
]

I have also created a persistent volume in K8s pointing to the mount point in the Docker volume and deployed it.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: jenkins-pv-volume
  labels:
    type: hostPath
spec:
  storageClassName: manual
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  hostPath:
    path: "/var/lib/docker/volumes/jenkins-pv/_data"

I have also created a pv claim and deployed that.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: jenkins-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

Lastly I have created a deployment for Jenkins. I have confirmed it works and I am able to access the app.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins-app
  template:
    metadata:
      labels:
        app: jenkins-app
    spec:
      containers:
      - name: jenkins-pod
        image: jenkins/jenkins:2.237-alpine
        ports:
        - containerPort: 50000
        - containerPort: 8080
        volumeMounts:
        - name: jenkins-pv-volume
          mountPath: /var/lib/docker/volumes/jenkins-pv/_data
      volumes:
      - name: jenkins-pv-volume
        persistentVolumeClaim:
          claimName: jenkins-pv-claim

However, the data does not persist quitting Docker and I have to reconfigure Jenkins every time I start. Did I miss something or how/what I am trying to do not possible? Is there a better or easier way to do this?

Thanks!

-- Nick Orlowski
docker
jenkins
kubernetes

1 Answer

5/20/2020

I figured out my issue, it was two fold.

  1. I was trying to save data from the wrong location within the pod that was running Jenkins.
  2. I was never writing the data back to docker shared folder.

To get this working I created a shared folder in Docker (C:\DockerShare). Then I updated the host path in my Persistent Volume. The format is /host_mnt/path_to_docker_shared_folder_location Since I used C:\DockerShare my path is: /host_mnt/c/DockerShare

apiVersion: v1
kind: PersistentVolume
metadata:
  name: jenkins
  labels:
    type: hostPath
spec:
  storageClassName: manual
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /host_mnt/c/DockerShare/jenkins

I also had to update the Jenkins deployment because I was not actually saving any of the config. I should have been saving data from /var/jenkins_home.

Deployment looks like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins-app
  template:
    metadata:
      labels:
        app: jenkins-app
    spec:
      containers:
      - name: jenkins-pod
        image: jenkins/jenkins:2.237-alpine
        ports:
        - containerPort: 50000
        - containerPort: 8080
        volumeMounts:
        - name: jenkins
          mountPath: /var/jenkins_home
      volumes:
      - name: jenkins
        persistentVolumeClaim:
          claimName: jenkins

Anyways its working now and I hope this helps someone else when it comes to setting up a PV.

-- Nick Orlowski
Source: StackOverflow