Feedback

Chat Icon

Cloud-Native Microservices With Kubernetes - 2nd Edition

A Comprehensive Guide to Building, Scaling, Deploying, Observing, and Managing Highly-Available Microservices in Kubernetes

Deploying Stateful Microservices: Persisting Data in Kubernetes
27%

Testing Data Persistence

Our Flask application saves its data in the PostgreSQL containerized database. The latter uses an external Volume to persist its data. The Volume is attached to the PostgreSQL Pod using a PersistentVolumeClaim (PVC) and is protected against data loss when the PVC (and the Pod) are deleted, since the StorageClass used has a reclaimPolicy set to Retain.

This is what we expect from our setup:

  • We can delete the application Deployment and recreate it without losing any data.
  • We can delete the database Deployment and recreate it without losing any data.

Let's test these scenarios one by one.

# 1. Delete the application Deployment
kubectl delete deployment stateful-flask -n stateful-flask
# Recreate the application Deployment
kubectl apply -f kubernetes/deployment.yaml
# Make a GET request to verify that the data is still there
sleep 10 && curl "http://stateful-flask.

Cloud-Native Microservices With Kubernetes - 2nd Edition

A Comprehensive Guide to Building, Scaling, Deploying, Observing, and Managing Highly-Available Microservices in Kubernetes

Enroll now to unlock all content and receive all future updates for free.