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
25%

Attaching a Kubernetes Volume to a Workload

In addition to the ConfigMap, we also need to attach the Volume to the PostgreSQL Pod so that it can store its data persistently.

To deploy databases, we usually don't use Deployments but another resource called a StatefulSet. However, since we are progressing step by step, we will first create a Deployment for PostgreSQL to understand how to consume a Volume. Later, we will learn more about StatefulSets.

This is the YAML that attaches the Volume:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  namespace: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:18
          ports:
            - containerPort: 5432
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              subPath: postgres
              name: postgredb
      volumes

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.