Understanding Persistent Volumes and Liveness Probes in Kubernetes
In Kubernetes, persistent volumes (PVs) and liveness probes are important components used to manage storage and application health, respectively. Here’s an overview of each:
Persistent Volumes (PVs)
Persistent Volumes provide a way to manage durable storage in Kubernetes. PVs are a storage resource in the cluster, independent of any individual pod. They abstract details of how storage is provided from how it is consumed. This abstraction allows developers to use storage without needing to know the specifics of the underlying storage infrastructure.
Key Concepts:
PersistentVolume (PV): A piece of storage in the cluster provisioned by an administrator or dynamically provisioned using Storage Classes.
PersistentVolumeClaim (PVC): A request for storage by a user. It specifies the size and access mode (e.g., ReadWriteOnce, ReadOnlyMany).
StorageClass: Defines a class of storage, allowing for dynamic provisioning of PVs.
Example:
- Define a PersistentVolume (PV):
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: /mnt/data
- Create a PersistentVolumeClaim (PVC):
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
- Use the PVC in a Pod:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: my-storage
volumes:
- name: my-storage
persistentVolumeClaim:
claimName: my-pvc
Liveness Probes
Liveness Probes are used to determine if a container is running. If a liveness probe fails, Kubernetes will kill the container and, depending on the restart policy, may restart it. This mechanism ensures that applications are running properly and can recover from certain types of failures.
Key Types of Probes:
HTTP Probes: Kubernetes performs an HTTP GET request against the container's IP and port.
TCP Socket Probes: Kubernetes attempts to open a TCP connection to the container's IP and port.
Exec Probes: Kubernetes executes a command inside the container.
Example:
- HTTP Liveness Probe:
apiVersion: v1
kind: Pod
metadata:
name: liveness-http
spec:
containers:
- name: my-container
image: my-image
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 3
periodSeconds: 3
- TCP Socket Liveness Probe:
apiVersion: v1
kind: Pod
metadata:
name: liveness-tcp
spec:
containers:
- name: my-container
image: my-image
livenessProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 3
periodSeconds: 3
- Exec Liveness Probe:
apiVersion: v1
kind: Pod
metadata:
name: liveness-exec
spec:
containers:
- name: my-container
image: my-image
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
By using persistent volumes and liveness probes, Kubernetes makes sure your applications have stable storage and stay healthy. It automatically restarts any containers that fail their liveness checks.
Subscribe to my newsletter
Read articles from Mohmmad Saif directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Mohmmad Saif
Mohmmad Saif
👋Hello I am Mohd Saif, passionate technology enthusiast currently pursuing a bachelor of Computer application degree. 🎓Education: I am currently pursuing a bachelor of Computer application degree with the focus on coding at Bareilly University my education journey has equipped me with strong foundation in Computer science and I am eager to apply my knowledge to real word challenges. 💡Passion for technology: I have always been deeply passionate about technology and I am particular drawn to devops with AWS. 🚀Skills: 🔹Linux 🔹Shell scripting 🔹Python 🔹Ansible 🔹Docker 🔹Kubernetes 🔹Jenkins CI/CD 🔹Maven 🔹Git and GitHub ✨Future goals: My goal is to facilitate seamless collaboration between development and operations teams, ensuring faster releases and high-quality software. I am a proactive learner, constantly exploring new DevOps trends and practices