Docker container does/doesnt work inside kubernetes

6/20/2019

I am a bit confused here. It does work as normal docker container but when it goes inside a pod it doesnt. So here is how i do it.

Dockerfile in my local to create the image and publish to docker registry

FROM alpine:3.7
COPY . /var/www/html
CMD tail -f /dev/null

Now if i just pull the image(after deleting the local) and run as a container. It works and i can see my files inside /var/www/html.

Now i want to use that inside my kubernetes cluster.

Def : Minikube --vm-driver=none

I am running kube inside minikube with driver none option. So for single node cluster.

EDIT

I can see my data inside /var/www/html if i remove volume mounts and claim from deployment file.

Deployment file

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    io.kompose.service: app
  name: app
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: app
    spec:
      securityContext:
        runAsUser: 1000
        runAsGroup: 1000
      containers:
      - image: kingshukdeb/mycode
        name: pd-mycode
        resources: {}
        volumeMounts:
        - mountPath: /var/www/html
          name: claim-app-storage
      restartPolicy: Always
      volumes:
      - name: claim-app-storage
        persistentVolumeClaim:
          claimName: claim-app-nginx
status: {}

PVC file

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  creationTimestamp: null
  labels:
    io.kompose.service: app-nginx1
  name: claim-app-nginx
spec:
  storageClassName: testmanual
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi
status: {}

PV file

apiVersion: v1
kind: PersistentVolume
metadata:
  name: app-nginx1
  labels:
    type: local
spec:
  storageClassName: testmanual
  capacity:
    storage: 100Mi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/data/volumes/app"

Now when i run these files it creates the pod, pv, pvc and pvc is bound to pv. But if i go inside my container i dont see my files. hostpath is /data/volumes/app . Any ideas will be appreciated.

-- Kingshuk Deb
docker
kubernetes
kubernetes-pod

1 Answer

7/4/2019

When PVC is bound to a pod, volume is mounted in location described in pod/deployment yaml file. In you case: mountPath: /var/www/html. That's why files "baked into" container image are not accessible (simple explanation why here)

You can confirm this by exec to the container by running kubectl exec YOUR_POD -i -t -- /bin/sh, and running mount | grep "/var/www/html".

Solution

You may solve this in many ways. It's best practice to keep your static data separate (i.e. in PV), and keep the container image as small and fast as possible.

If you transfer files you want to mount in PV to your hosts path /data/volumes/app they will be accessible in your pod, then you can create new image omitting the COPY operation. This way even if pod crashes changes to files made by your app will be saved.

If PV will be claimed by more than one pod, you need to change accessModes as described here:

The access modes are:

  • ReadWriteOnce – the volume can be mounted as read-write by a single node
  • ReadOnlyMany – the volume can be mounted read-only by many nodes
  • ReadWriteMany – the volume can be mounted as read-write by many nodes

In-depth explanation of Volumes in Kubernetes docs: https://kubernetes.io/docs/concepts/storage/persistent-volumes/

-- MWZ
Source: StackOverflow