How to mimic '--volumes-from' in Kubernetes

后端 未结 5 1241
你的背包
你的背包 2020-12-02 12:32

I\'m looking for a pattern that allows to share volumes between two containers running on the same pod in Kubernetes.

My use case is: I have a Ruby on Rails applicat

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 12:52

    [update-2016-8] In latest Kubernetes release, you can use a very nice feature named init-container to replace the postStart part in my answer below, which will make sure the container order.

    apiVersion: v1
    kind: Pod
    metadata:
      name: javaweb-2
    spec:
      initContainers:
      - name: war
        image: resouer/sample:v2
        command: ["cp", "/sample.war", "/app"]
        volumeMounts:
        - mountPath: /app
          name: app-volume
      containers:
      - name: tomcat
        image: resouer/mytomcat:7.0
        command: ["sh","-c","/root/apache-tomcat-7.0.42-v2/bin/start.sh"]
        volumeMounts:
        - mountPath: /root/apache-tomcat-7.0.42-v2/webapps
          name: app-volume
        ports:
        - containerPort: 8080
          hostPort: 8001
      volumes:
      - name: app-volume
        emptyDir: {}
    

    NOTE: initContainer is still a beta feature so the work version of this yaml is actually like: http://kubernetes.io/docs/user-guide/production-pods/#handling-initialization, please notice the pod.beta.kubernetes.io/init-containers part.

    ---original answer begin---

    Actually, you can. You need to use container life cycle handler to control what files/dirs you want to share with other containers. Like:

    ---
    apiVersion: v1
    kind: Pod
    metadata:
        name: server
    spec:
        restartPolicy: OnFailure
        containers:
        - image: resouer/sample:v2
          name: war
          lifecycle:
            postStart:
              exec:
                command:
                  - "cp"
                  - "/sample.war"
                  - "/app"
          volumeMounts:
          - mountPath: /app
            name: hostv1 
        - name: peer
          image: busybox
          command: ["tail", "-f", "/dev/null"]
          volumeMounts:
          - name: hostv2
            mountPath: /app/sample.war
        volumes:
        - name: hostv1
          hostPath:
              path: /tmp
        - name: hostv2
          hostPath:
              path: /tmp/sample.war
    

    Please check my gist for more details:

    https://gist.github.com/resouer/378bcdaef1d9601ed6aa

    And of course you can use emptyDir. Thus, war container can share its /sample.war to peer container without mess peer's /app directory.

    If we can tolerate /app been overridden, it will be much simpler:

    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: javaweb-2
    spec:
      restartPolicy: OnFailure
      containers:
      - image: resouer/sample:v2
        name: war
        lifecycle:
          postStart:
            exec:
              command:
                - "cp"
                - "/sample.war"
                - "/app"
        volumeMounts:
        - mountPath: /app
          name: app-volume
      - image: resouer/mytomcat:7.0
        name: tomcat
        command: ["sh","-c","/root/apache-tomcat-7.0.42-v2/bin/start.sh"]
        volumeMounts:
        - mountPath: /root/apache-tomcat-7.0.42-v2/webapps
          name: app-volume
        ports:
        - containerPort: 8080
          hostPort: 8001 
      volumes:
      - name: app-volume
        emptyDir: {}
    

提交回复
热议问题