<p>I am working on Kubernetes pods and I have some pods running on an application. Now, I want to copy some files inside my pods but the situation here is my files present in my s3 bucket. So, I want to know an automated way that can directly copy my s3 files into my pod's folder directory. I don't know how to do this.</p>
<p>If anyone knows it then please reply.</p>
<p>Thanks</p>

Piyush Mittal
<p>There are multiple ways to achieve this, few of them are:</p>
<ol>
<li><strong>Using Kubectl</strong> (very basic not automated method):  first need to download s3 files on local and copy them into application pods can use kubectl command line to perform copy operation from host fs to pod fs.</li>
</ol>
<pre class="lang-bash prettyprint-override"><code>kubectl cp &lt;local-file&gt; &lt;pod-name&gt;:&lt;file-path&gt;
</code></pre>
<ol start="2">
<li><strong>Using Init-Container</strong> (Automated): Write a small script to download files from s3/cloud provider and place into shared volume between init-container and main container. In that way whenever your pod is spin up, the init-container will prepare volume before running actual container.</li>
</ol>
<p>Sample Pod yaml:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
 - name: myapp-container
    image: busybox:1.28
    command: ['sh', '-c', 'echo The app is running! &amp;&amp; sleep 3600']
    volumeMounts:
    - mountPath: /tmp
      name: data
  initContainers:
 - name: init-downloader
    image: busybox:1.28
    command: ['sh', '-c', &quot;aws s3 cp s3://xyz/ /tmp&quot;]
    volumeMounts:
    - mountPath: /tmp
      name: data
  volumes:
 - emptyDir: {}
    name: data
</code></pre>
<ol start="3">
<li><strong>Using CrobJob</strong> (Periodically Update): If the data needs to be updated in volume on periodic basis and not depends on pod restarts, then additional utility can be utilised to perform this action.</li>
</ol>
<ul>
<li>Either a script to download on local and perform kubectl copy operation on all pods</li>
<li>Use other opensource project to fullfill that, for example
<ul>
<li><a href="https://github.com/maorfr/skbn" rel="nofollow noreferrer">https://github.com/maorfr/skbn</a>, Skbn is a tool for copying files and directories between Kubernetes and cloud storage providers</li>
</ul>
</li>
</ul>
<p>Sample Job Yaml</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: batch/v1
kind: Job
metadata:
  labels:
    app: skbn
  name: skbn
spec:
  template:
    metadata:
      labels:
        app: skbn
    annotations:
      iam.amazonaws.com/role: skbn 
    spec:
      serviceAccountName: skbn
      containers:
      - name: skbn
        image: maorfr/skbn
        command: [&quot;skbn&quot;]
        args:
        - cp
        - --src
        - k8s://namespace/pod/container/path/to/copy/from
        - --dst
        - s3://bucket/path/to/copy/to
        env:
        - name: AWS_REGION
          value: us-east-1
</code></pre>
<p>There can be multiple ways to implement this requirement but mostly it will be using above mentioned methods only.</p>
<p>Thanks</p>
