skip to Main Content

In docker world, we can use pure docker image and use --volumes-from to connect it with container, how this works in the kubernetes

Docker image could be html files like

FROM scratch
COPY html /www

How can I mount it to the nginx pod?

BTW: Surely I can turn docker pure data image to use busybox as base image, then copy the data out using initContainers, which make the image 1M bigger, here try to see whether it is possible in k8s world

2

Answers


  1. You would use the volumes and volumeMounts.

    UPD:

    html file to mount:

    $ cat index.html
      <h1>HELLO</h1>
    

    Create configMap with the content of the file:

    $ kubectl create configmap nginx-index-html-configmap --from-file=index.html
      configmap/nginx-index-html-configmap created
    

    nginx pod file:

    $ cat nginx-with-config.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        volumeMounts:
          - mountPath: /usr/share/nginx/html/index.html
            name: nginx-config
            subPath: index.html
      volumes:
        - name: nginx-config
          configMap:
            name: nginx-index-html-configmap
    

    Creating the pod:

    $ kubectl create -f nginx-with-config.yaml
      pod/nginx created
    

    Checking nginx serves the file:

    $ kubectl exec -it nginx -- curl 127.0.0.1
      <h1>HELLO</h1>
    

    UPD2:

    You can have everything in one big happy file, no need to prep anything in advance:

    $ cat nginx-with-config.yaml
    
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: nginx-index-html-configmap-2
    data:
      index.html: |
        <h1>HELLO 2!</h1>
    
    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx2
    spec:
      containers:
      - name: nginx
        image: nginx
        volumeMounts:
          - mountPath: /usr/share/nginx/html/index.html
            name: nginx-config
            subPath: index.html
      volumes:
        - name: nginx-config
          configMap:
            name: nginx-index-html-configmap-2
    
    $ kubectl apply -f nginx-with-config.yaml
      configmap/nginx-index-html-configmap-2 created
      pod/nginx2 created
    
    $ kubectl exec -it nginx2 -- curl 127.0.0.1
      <h1>HELLO 2!</h1>
    
    Login or Signup to reply.
  2. Unlike Docker’s named volumes, volume mounts in Kubernetes never copy anything into the volume. You occasionally see tricks with Docker Compose setups where a Docker named volume is mounted over two containers, with the expectation that static files will be copied into the volume from one of them to be served by the other; this just doesn’t work in Kubernetes unless you copy the files yourself.

    For the setup you show, you have a collection of files you want to serve, and you want to have the standard nginx image serve them. Instead of trying to copy files between images, you can have a unified image that starts FROM nginx and contains your files:

    FROM nginx
    COPY html /usr/share/nginx/html
    # Base image provides a suitable default CMD and other setup
    

    You don’t need any sort of volume to run this. Just specify it as the image: in your Deployment spec, and all of the files to be served are already compiled into the image.

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