Skip to content

Instantly share code, notes, and snippets.

@resouer
Last active March 13, 2023 08:40
Show Gist options
  • Save resouer/378bcdaef1d9601ed6aa to your computer and use it in GitHub Desktop.
Save resouer/378bcdaef1d9601ed6aa to your computer and use it in GitHub Desktop.
How to implement volumes-from in Kubernetes Pod?
---
apiVersion: v1
kind: Pod
metadata:
name: server
spec:
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
@resouer
Copy link
Author

resouer commented Oct 14, 2015

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:
  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: {}

@scrisenbery
Copy link

Will this work in a case where the command run in "peer" on start requires the mounted file to be present?

@scrisenbery
Copy link

Also, what would happen if Kubernetes put two of the same pod on one host?

@resouer
Copy link
Author

resouer commented Nov 12, 2015

@scrisenbery The containers starts in order, but you still need to expect cp finish quickly.
Container dependency is not promised in k8s' current design, but you can test if health check probe can make this better?

I just use hostDir for example, you can use emptyDIr, then there will be no messed problem.

@kjvalencik
Copy link

I use a couple of simple start scripts to atomically copy the data and wait until it is ready.

# Copy the data in atomically
cp -r /src/data /dest/folder/temp && mv /dest/folder/temp /dest/folder/final
# Keep the side car alive so that it doesn't get restarted. This could also be implemented
# with `pause`.
# Note: This won't be necessary when per-pod restart policies are implemented
while true; do sleep 10; done

# Wait for the data to be available
until [ -d /dest/folder/final ]; do sleep 1; done;
./start.sh

@akamanocha
Copy link

What will happen if I want to use rolling update?

Is rolling update of one container inside a pod possible? Without touching second container?

@resouer
Copy link
Author

resouer commented Apr 9, 2016

@akamanocha It works well in your case. Just make sure that Tomcat can automatically load your wars, otherwise you need to restart it manually.

@derekmahar
Copy link

Keep the side car alive so that it doesn't get restarted. This could also be implemented
with pause.
Note: This won't be necessary when per-pod restart policies are implemented
while true; do sleep 10; done

@kjvalencik What do you mean by "pause"? Java Web Application with Tomcat and Sidercar Container keeps its sidecar container alive by invoking tail -f /dev/null.

@resouer
Copy link
Author

resouer commented Nov 3, 2016

Guys, this can be solved more elegant by using initContainer, see my update in the original answer: http://stackoverflow.com/questions/30538210/how-to-mimic-volumes-from-in-kubernetes/33118902#33118902

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment