<p>I'm trying to tweak my Kubernetes-based app to make it more development-friendly. I'm using <code>kustomize</code> to add some extra containers into my <code>app-deployment</code>. It looks like this:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-deployment
spec:
  template:
    spec:
      volumes:
        - name: php-src
          emptyDir: {}
        - name: nginx-src
          emptyDir: {}
        - name: webpack-src
          emptyDir: {}
      containers:
        - name: lsyncd
          image: axiom/rsync-server
          ports:
            - name: sshd
              containerPort: 22
            - name: rsyncd
              containerPort: 873
          env:
            - name: USERNAME
              value: user
            - name: PASSWORD
              value: pass
          volumeMounts:
            - name: php-src
              mountPath: /data/php
            - name: nginx-src
              mountPath: /data/nginx
            - name: webpack-src
              mountPath: /data/webpack
        - name: nginx
          volumeMounts:
            - name: nginx-src
              mountPath: /app
        - name: php
          volumeMounts:
            - name: php-src
              mountPath: /app
        - name: webpack
          image: dreg.mpen.ca/kymark/webpack
          imagePullPolicy: Never  # https://stackoverflow.com/a/54043058/65387
          ports:
            - name: http
              containerPort: 8082
          livenessProbe:
            httpGet:
              port: http
              path: /assets/manifest.json
            initialDelaySeconds: 20
            periodSeconds: 60
          command: ['node','--max_old_space_size=20000','node_modules/.bin/webpack-dev-server']
          volumeMounts:
            - name: webpack-src
              mountPath: /app
</code></pre>
<p>Basically, I've added a new rsync server with 3 volumes corresponding with the 3 different containers that I want to upload code to. This part is working great; I can rsync my code into the containers and it shows up instantly.</p>
<p>However, the nginx, PHP, and webpack images already have a bit of code in that <code>/app</code> directory (like <code>node_modules</code> and <code>vendor</code>). When I mount over the top of it, it disappears (which makes sense, since I'm mounting an <code>emptyDir</code>). But what I would like to do instead is use an overlay mount so that any code I upload will be 'overlayed' on top of what's already there.</p>
<p>Can I do that? How?</p>

mpen
<p>No, <strong>Kubernetes</strong> mounts, unlike <strong>Docker</strong> mounts do not allow mounting one directory over the other. Potential <code>VOLUME</code> lines from the <code>Dockerfile</code> do not matter.</p>
<p>A common solution for your use-case is, to use an <strong>init container</strong> to set up the <a href="https://kubernetes.io/docs/concepts/storage/volumes/#emptydir" rel="nofollow noreferrer">emptyDir</a>, before the <strong>main container</strong> is started. So basically you specify the <code>emptyDir</code> as a volume mount in both the <strong>init container</strong> and the <strong>main container</strong> and have the <strong>init container</strong> copy the files into the <code>emptyDir</code>.</p>
<p>Additionally, there are two projects that offer different solutions to what you are trying to achieve:</p>
<ul>
<li><a href="https://github.com/ksync/ksync" rel="nofollow noreferrer">https://github.com/ksync/ksync</a></li>
<li><a href="https://github.com/telepresenceio/telepresence" rel="nofollow noreferrer">https://github.com/telepresenceio/telepresence</a></li>
</ul>
<p>Using one of those you would not need the custom <code>rsync</code> and <strong>init container</strong> setup in your overlay.</p>
