skip to Main Content

I have K8s cluster with one master and one worker Node.
Scenario – 1 I am deploying a simple sidecar container within same POD where I have define emptyDir as share volume and it is working fine. The manifest file

apiVersion: apps/v1
kind: Deployment
metadata:
   name: pod-with-sidecar
   namespace: test
spec:
   replicas: 1
   selector:
     matchLabels:
         app: debian-nginx
   template:
      metadata:
        labels:
           app: debian-nginx
      spec:
        containers:
        - name: main-debian
          image: debian
          command: ["/bin/sh"]
          args: ["-c", "while true; do date >> /var/log/index.html; sleep 2;done"]
          volumeMounts:
          - mountPath: /var/log
            name: shared-log
        - name: sidecar-nginx
          image: nginx
          volumeMounts:
          - mountPath: /usr/share/nginx/index.html
            name: shared-log
        volumes:
         - name: shared-log
           emptyDir: {}  

Here
In Container – 1 the file is created and the content is available in /car/log/index.html
Similarly in Container – 2 the file is created and the content is available in usr/share/nginx/html/index.html

Scenario – 2 I am deploying same simple sidecar container within same POD where I have define hosPath as share volume and it is not working. The manifest file

apiVersion: apps/v1
kind: Deployment
metadata:
    name: pod-with-sidecar
    namespace: test
spec:
   replicas: 1
   selector:
     matchLabels:
         app: debian
         app: nginx
   template:
     metadata:
       labels:
          app: debian
          app: nginx
     spec:
       containers:
       # Main application container
       - name: main-debian
         image: debian
         command: ["/bin/sh", "-c"]
         args: ["while true; do data >> /var/log/index.html; sleep 2;done"]
         volumeMounts:
         - mountPath: /var/log
           name: shared-vol
       # Sidecar container
       - name: sidecar-nginx
         image: nginx:1.7.9
         ports:
         - containerPort: 80
         volumeMounts:
         - mountPath: /usr/share/nginx/html # nginx-specific mount path
           name: shared-vol
       volumes:
       - name: shared-vol
         hostPath:
           path: /mydata
           type: DirectoryOrCreate

Here

In Container – 1 the file is created but there is no content available in /car/log/index.html

testuser@kmasterl:~$ kubectl exec -it pod-with-sidecar-85f88c9d5d-xjcgh -c main-debian -n test -- /bin/bash 
root@pod-with-sidecar-85f88c9d5d-xjcgh:/# ls -lrt var/log 
total 0 -rw-r--r-- 1 root root 0 Dec 14 21:28 index.html 
root@pod-with-sidecar-85f88c9d5d-xjcgh:/# cat /var/log/index.html 
root@pod-with-sidecar-85f88c9d5d-xjcgh:/#

Similarly in Container – 2 the file is created but there is no content available in usr/share/nginx/html/index.html

testuser@kmasterl:~$ kubectl exec -it pod-with-sidecar-85f88c9d5d-xjcgh -c sidecar-nginx -n test -- /bin/bash 
root@pod-with-sidecar-85f88c9d5d-xjcgh:/# ls -lrt /usr/share/nginx/html 
total 0 -rw-r--r-- 1 root root 0 Dec 14 21:28 index.html 
root@pod-with-sidecar-85f88c9d5d-xjcgh:/# cat usr/share/nginx/html/index.html  
root@pod-with-sidecar-85f88c9d5d-xjcgh:/#  

Could anyone suggest why it is not working when hostPath is defined as shared volume??

2

Answers


  1. If I understand what you’re trying to achieve correctly, I would use the emptyDir approach.

    That said, when you use hostPath the files or directories created on the underlying hosts are only writable by root. You either need to run your process as root in a privileged Container or modify the file permissions on the host to be able to write to a hostPath volume. For more details see: https://kubernetes.io/docs/concepts/storage/volumes/

    Login or Signup to reply.
  2. The manifest file of the Deployment is correct, there is only a typo in the command of the main container it should be do date and not do data

    args: ["while true; do date >> /var/log/index.html; sleep 2;done"]

    Fix the typo and it will work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search