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
CodePudding user response:
Its because of the mountPath:
The root directive indicates the actual path on your hard drive where this virtual host's assets (HTML, images, CSS, and so on) are located, ie /usr/share/nginx/html. The index setting tells Nginx what file or files to serve when it's asked to display a directory
When you curl to default port 80, curl serve you back content of the html directory. .
CodePudding user response:
Just an elaboration of @P.... answer .
- Following is creating a common storage space "tagged" with name "web-content"
volumes:
- name: web-content
emptyDir: {}
- web-content is mount in location
/tmp/web-content/on init container namedinit-conwhich is writingcheck this outinindex.html
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/
- same
web-contentvolume which has theindex.htmlis being mounted as directory/usr/share/nginx/htmlso theindex.htmllocation will be seen as/usr/share/nginx/html/index.html. (default landing page for nginx ) for containernginxhence it showscheck this outwhen you curl to it.
containers:
- image: nginx:1.17.3-alpine
name: nginx
volumeMounts:
- name: web-content
mountPath: /usr/share/nginx/html
