This deployment creates 1 pod that has init container in it. The container mounts volume into tmp/web-content and writes 1 single line ‘check this out’ to index.html
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-init-container
namespace: mars
spec:
replicas: 1
selector:
matchLabels:
id: test-init-container
template:
metadata:
labels:
id: test-init-container
spec:
volumes:
- name: web-content
emptyDir: {}
initContainers:
- name: init-con
image: busybox:1.31.0
command: ['sh', '-c' ,"echo 'check this out' > /tmp/web-content/index.html"]
volumeMounts:
- name: web-content
mountPath: /tmp/web-content/
containers:
- image: nginx:1.17.3-alpine
name: nginx
volumeMounts:
- name: web-content
mountPath: /usr/share/nginx/html
ports:
- containerPort: 80
I fire up temporary pod to check if i can see this line ‘check this out’ using curl.
k run tmp --restart=Never --rm -i --image=nginx:alpine -- curl 10.0.0.67
It does show the line. However, how does curl know which directory to go in?
I dont specify it should go to /tmp/web-content explicitly
2
Answers
Its because of the
mountPath
:When you curl to default port 80, curl serve you back content of the html directory.
.
Just an elaboration of @P…. answer .
/tmp/web-content/
on init container namedinit-con
which is writingcheck this out
inindex.html
web-content
volume which has theindex.html
is being mounted as directory/usr/share/nginx/html
so theindex.html
location will be seen as/usr/share/nginx/html/index.html
. (default landing page for nginx ) for containernginx
hence it showscheck this out
when you curl to it.