punit gupta:
Hello
I have question about shared space between two containers within the same pod. Pls note i didn’t mount a volume on purpose.
Here i am trying to access /var from two cotainer within a POD.
I created below POD.
controlplane $ cat pod
apiVersion: v1
kind: Pod
metadata:
name: two-containers
spec:
restartPolicy: Never
containers:
- name: cont-1
image: busybox
command: ["/bin/sh"]
args: ["-c", “echo Hello from the cont-1 >> /var/log; sleep 10000”]
- name: cont-2
image: busybox
command: ["/bin/sh"]
args: ["-c", “sleep 10 ; cat /var/log ; echo printing from cont-2 what cont-1 wrote >> /var/log; sleep 1000”]
cont-1 was able to access /var/log from the container command when executing.
controlplane $ k exec two-containers cont-1 -ti -- cat /var/log
Defaulting container name to cont-1.
Use ‘kubectl describe pod/two-containers -n default’ to see all of the containers in this pod.
Hello from the cont-1
However cont-2 wasnt able to :
controlplane $ k logs two-containers cont-2
cat: can’t open ‘/var/log’: No such file or directory
However when i open the shell on cont-2, i can see it has access to /var/log
controlplane $ k exec two-containers cont-2 -ti -- cat /var/log
Defaulting container name to cont-1.
Use ‘kubectl describe pod/two-containers -n default’ to see all of the containers in this pod.
Hello from the cont-1
So my question is why cont-2 is not able to access /var/log of POD from container command, while we can see it has access when tried from shell.
AB:
@punit gupta I cannot see whether you mount the volume or not. did you mount it ?
AB:
yaml would be something like this
apiVersion: v1
kind: Pod
metadata:
name: two-containers
spec:
restartPolicy: Never
containers:
- name: cont-1
image: busybox
command: ["/bin/sh"]
args: ["-c", “echo Hello from the cont-1 >> /var/log; sleep 10000”]
volumeMounts:
- mountPath: /var/log
name: vol
- name: cont-2
image: busybox
command: ["/bin/sh"]
args: ["-c", “sleep 10 ; cat /var/log ; echo printing from cont-2 what cont-1 wrote >> /var/log; sleep 1000”]
volumeMounts:
- mountPath: /var/log
name: vol
volumes:
- name: vol
emptyDir: {}
Singam Nagasai Reddy:
this method is also called as sidecar right?
ns680:
Agree with AB, there needs to a common volume. I tried and it works for me, small change is that /var/log will be mounted as a directory, so you need to write to a file in it.
controlplane $ k exec two-containers cont-1 -- cat /var/log/file
Defaulting container name to cont-1.
Use 'kubectl describe pod/two-containers -n default' to see all of the containers in this pod.
Hello from the cont-1
printing from cont-2 what cont-1 wrote
Still curious whey we cannot exec to container “cont-2”, exec always defaults to “cont-1”
controlplane $ k exec two-containers cont-2 -- ps
Defaulting container name to cont-1.
Use 'kubectl describe pod/two-containers -n default' to see all of the containers in this pod.
PID USER TIME COMMAND
1 root 0:00 sleep 10000
53 root 0:00 ps
punit gupta:
Thanks guys for your responses, however, i explained in my question that i didnt mount volume on purpose. I know with volume mount it works.
My question is different.
cont-1 was able to write to /var/log without volume mount, why cont-2 was not able to ?
When connecting to cont-2 shell, we can access the /var/log, while cont-2 container command wasnt able to, why is it so ?
This is the mystery i am trying to understand.
ns680:
cont-2 will be able to write to /var/log but it will be different storage from cont-1. that is why you are seeing the error in “k logs” command on cont-2.
For some reason “k exec” is always run on cont-1, so you are actually connected to cont-1 not cont-2 in the shell. You can verify by running ps . I don’t why it is the case and curious to know the reason
punit gupta:
@ns680 in this case cont-2 shouldnt show this error :
However cont-2 wasnt able to :
controlplane $ k logs two-containers cont-2
cat: can’t open ‘/var/log’: No such file or directory
ns680:
the error is shown in “logs” not “exec” , logs command is working on cont-2 correctly
ns680:
if you run “ps” in exec on cont-2, you will see sleep 10000, not sleep 1000 which should be the case on cont-2 . There is also a message “Defaulting container name to cont-1.” , don’t know why it is happening.
punit gupta:
I got confused now, u said “exec” command works because it default to cont-1 [Good point and understood]. I still didnt understand why “log” says failure, if lets say “/var/log” are different storage areas.
AB:
Not sure. i have used different images for sidecar container and different log file. seems like its working. I have tried your your yaml file and c2 never run. check my yaml. In your case change c2 logfile to make sure same as c1 log file
ns680:
@punit gupta, please see your command “sleep 10 ; cat /var/log ; echo printing from cont-2 what cont-1 wrote >> /var/log; sleep 1000”]", when cat /var/log it run, there is nothing written in it , cont-2 cannot read storage of cont-1, hence the error . If you move cat /var/log after “echo” , on cont-2 the error should go away*.*
punit gupta:
@ns680 i think it makes sense now. The only open question is why “exec” command default to cont-1, when executed specifically for cont-2 ?
Thanks for your help.
ns680:
np, I am trying to look into why cont-1 is always used as default
ns680:
it works with -c option
controlplane $ kubectl exec two-containers -c cont-2 -- cat /var/log
printing from cont-2 what cont-1 wrote
punit gupta:
ok got it, thanks…
AB:
@ns680 how come cat worked where its a directory?
ns680:
@AB I used the orginal yaml from Punit in this case