Kubernetes Storage
Persistent storage is an important requirement for many applications running on Kubernetes. In this section, we will explain how to manage persistent storage for your Kubernetes applications using persistent volumes and persistent volume claims.
- Define a persistent volume: A persistent volume is a piece of storage in the cluster that can be dynamically provisioned or manually allocated by a cluster administrator. You can define a persistent volume using a YAML configuration file. For example:
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: /data/my-pv
In this example, we define a persistent volume with 1 gigabyte of storage, a single access mode (ReadWriteOnce
), and a hostPath
that refers to a directory on the host machine where the persistent volume will be stored.
- Define a persistent volume claim: A persistent volume claim is a request for a specific amount of storage that is bound to a persistent volume. You can define a persistent volume claim using a YAML configuration file. For example:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
In this example, we define a persistent volume claim for 1 gigabyte of storage with a single access mode (ReadWriteOnce
).
- Mount the persistent volume in your application: You can mount the persistent volume in your application by adding a volume and volume mount to the pod specification. For example:
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
containers:
- name: my-app
image: my-image:latest
volumeMounts:
- name: my-data
mountPath: /data
volumes:
- name: my-data
persistentVolumeClaim:
claimName: my-pvc
In this example, we mount the persistent volume in the /data
directory of the container using a volume mount. We also define a volume with the name my-data
that references the persistent volume claim my-pvc
.
- Create the persistent volume and persistent volume claim: You can create the persistent volume and persistent volume claim by applying the YAML configuration files using the
kubectl apply
command. For example:
kubectl apply -f persistent-volume.yaml
kubectl apply -f persistent-volume-claim.yaml
- Use the persistent storage in your application: Your application can now use the persistent storage mounted at
/data
to store and retrieve data. For example, in a Python Flask application, you can use theflask_sqlalchemy
library to store data in a SQLite database on the persistent volume. Here is an example of how to configure the database URL to use a file on the persistent volume:
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join('/data', 'my-db.sqlite')
db = SQLAlchemy(app)
# Define your models here
In this example, we configure the SQLite database URL to use a file at /data/my-db.sqlite
, which is on the persistent volume.
With these steps, you can manage persistent storage for your Kubernetes applications using persistent volumes and persistent volume claims. This provides a reliable and scalable way to store and retrieve data for your applications.
Leave a Comment