Kubernetes-part-10

Volumes
volumes are used to provide storage that pods and containers can access. Unlike regular container storage, which is ephemeral (lost when the container stops), volumes allow data to persist or be shared across containers in a pod. Volumes ensure that data survives container restarts and can be used to share data between containers within the same pod. Any no. of volumes can be attached to pod.
Types of volumes in Kubernetes
emptyDir
An
emptyDirvolume is created when a pod is assigned to a node and exists as long as the pod is running. Once the pod is deleted, the data in theemptyDiris also removed.Use case: Temporary storage for data that needs to be shared between containers in the same pod.
Example:
apiVersion: v1 kind: Pod metadata: name: emptydir-pod spec: containers: - name: busybox image: busybox command: ['sh', '-c', 'echo "Writing data to /data/emptydir-volume..."; echo "Hello from Kubesimplify" > /data/emptydir-volume/hello.txt; sleep 3600'] volumeMounts: - name: temp-storage mountPath: /data/emptydir-volume volumes: - name: temp-storage emptyDir: {}The YAML configuration defines a Kubernetes pod named
emptydir-podthat uses anemptyDirvolume for temporary storage. A container running thebusyboximage executes a command to write"Hello from Kubesimplify"to a file namedhello.txtat/data/emptydir-volume. This volume is created when the pod starts and exists only as long as the pod is running; all data is deleted when the pod is terminated, making it ideal for temporary storage needs.apiVersion: v1 kind: Pod metadata: name: emptydir-pod spec: containers: - name: busybox image: busybox command: ['sh', '-c', 'echo "Writing data to /data/emptydir-volume..."; sleep 3600'] volumeMounts: - name: temp-storage mountPath: /data/emptydir-volume volumes: - name: temp-storage emptyDir: medium: Memory sizeLimit: 512MiThis pod runs a
busyboxcontainer that writes data to a temporary, memory-based volume mounted at/data/emptydir-volumeinside the container. The volume is limited to 512 MiB and will be deleted once the pod stops.
hostPath
A HostPath volume mounts a file or directory from the node’s filesystem (the host) into the pod, allowing the container to access or write data directly on the host.
Use case: Accessing specific files or logs on the host node.
Example:
apiVersion: v1 kind: Pod metadata: name: hostpath-pod spec: containers: - name: busybox image: busybox command: ['sh', '-c', 'echo "Writing data to /data/hostpath-volume..."; echo "Hello from Kubesimplify" > /data/hostpath-volume/hello.txt; sleep 3600'] volumeMounts: - name: host-storage mountPath: /data/hostpath-volume volumes: - name: host-storage hostPath: path: /tmp/hostpath type: DirectoryOrCreateThe HostPath Volume mounts the directory
/tmp/hostpathfrom the node's filesystem into the container at/data/hostpath-volume. The pod writes the text "Hello from Kubesimplify" to a file namedhello.txtinside the container at/data/hostpath-volume/hello.txt. Since this is a HostPath volume, the file is actually created in/tmp/hostpath/hello.txton the host node. Additionally, the data stored in the HostPath volume will persist even if the pod is deleted, as it is stored directly on the host node’s filesystem.
Persistent Volume (PV)
It is a piece of storage in a Kubernetes cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes.
PVs are cluster resources that exist independently of any individual pod and are designed for long-term data storage.
They can be backed by various types of storage systems, such as NFS, iSCSI, cloud storage (like AWS EBS, Google Persistent Disk), or local storage.
PVs have a lifecycle that is separate from the pods that use them, allowing data to persist even if pods are deleted or moved.
Persistent Volume Claim (PVC):
A Persistent Volume Claim (PVC) is a request for storage by a user or a pod.
It specifies the desired storage size and access modes (e.g., ReadWriteOnce, ReadOnlyMany) and can also include specific storage class requirements.
When a PVC is created, Kubernetes looks for a matching PV that satisfies the request. If a suitable PV is found, it is bound to the PVC, allowing pods to use that storage.
If no matching PV is available, Kubernetes may dynamically provision a new PV if a Storage Class is specified in the PVC.



