skip to Main Content

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


  1. 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.
    .

    Login or Signup to reply.
  2. 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 named init-con which is writing check this out in index.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-content volume which has the index.html is being mounted as directory /usr/share/nginx/html so the index.html location will be seen as /usr/share/nginx/html/index.html . (default landing page for nginx ) for container nginx hence it shows check this out when you curl to it.
          containers:
          - image: nginx:1.17.3-alpine
            name: nginx
            volumeMounts:
            - name: web-content
              mountPath: /usr/share/nginx/html
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search