<p>I have generated logs for my pods using <code>kubectl logs 'pod name</code>. But I want to persist these logs in a volume (some kind of persistent storage), because container logs will get wiped out if the pods go down. Is there a way to do this? Do I have to write some sort of a script?
I have read many answers but I still do  not understand how to go about it, any help is appreciated. Thanks!</p>

Saranya Gupta
<p>I know this is an old question, but I've just had the same problem and I've spent some time to figure out the solution, so I'd like to share a more detailed solution.</p>
<p>Like Aayush Mall said, you'll need the <a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/" rel="noreferrer">PersistentVolume</a> and <a href="https://kubernetes.io/docs/concepts/storage/volumes/#persistentvolumeclaim" rel="noreferrer">PersistentVolumeClaim</a> objects to create the volume and then link it to the pod (preferably via a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/" rel="noreferrer">Deployment</a> object).</p>
<p>Basically, The PersistentVolume would define how and where the volume would be stored in the host and the PersistentVolumeClaim would define the constraints to bind the volume to some container.</p>
<p>From the <a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/" rel="noreferrer">docs</a>:</p>
<blockquote>
<p>A PersistentVolume (PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes. It is a resource in the cluster just like a node is a cluster resource. PVs are volume plugins like Volumes, but have a lifecycle independent of any individual Pod that uses the PV. This API object captures the details of the implementation of the storage, be that NFS, iSCSI, or a cloud-provider-specific storage system.</p>
<p>A PersistentVolumeClaim (PVC) is a request for storage by a user. It is similar to a Pod. Pods consume node resources and PVCs consume PV resources. Pods can request specific levels of resources (CPU and Memory). Claims can request specific size and access modes (e.g., they can be mounted ReadWriteOnce, ReadOnlyMany or ReadWriteMany, see AccessModes).</p>
</blockquote>
<p>So, let's say your pods are running in two nodes: <code>mynode-1</code> and <code>mynode-2</code>.</p>
<p>Your <code>PersistentVolume</code> spec will look like this.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: myapp-log-pv
spec:
  capacity:
    storage: 10Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  local:
    path: /var/log/myapp
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - mynode-1
          - mynode-2
</code></pre>
<p>Your <code>PersistentVolumeClaim</code> like this.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: myapp-log-pvc
spec:
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  storageClassName: local-storage
  resources:
    requests:
      storage: 2Gi
  volumeName: myapp-log
</code></pre>
<p>And then, you just have to tell the deployment object how to mount the volume inside the container. So, your <code>Deployment</code> spec will look like this.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deploy
spec:
  selector:
    matchLabels:
      app: myapp
  replicas: 1
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: myrepo/myapp:latest
        volumeMounts:
          - name: log
            mountPath: /var/log
      volumes:
      - name: log
        persistentVolumeClaim:
          claimName: myapp-log-pvc
      
</code></pre>
<p>And that's it. When your deployment starts, it'll create the pod with the container, mount a volume named <code>log</code> for the path <code>/var/log</code> (inside the container) and bound this volume to some PV matching the requirements of the PVC named <code>myapp-log-pvc</code>. As we've created the <code>myapp-log-pv</code> with the same <code>volumeMode</code>, <code>accessModes</code> and <code>storageClassName</code> fields and with more storage capacity then the required by <code>myapp-log-pvc</code>, they will be bound. So, your app logs will be stored in the path <code>/var/log/myapp</code> (field <code>spec.local.path</code> in the <code>myapp-log-pv</code> spec) inside the node running the pod.</p>
<p>I hope it help :)</p>
<p>Also, I'm kinda new in the kubernetes world, so please let me know if you notice I misunderstood something or if there is a better way to do this.</p>
