Guest mode. You can read posts without signing up. Comments, ratings, job contacts and your own posts open after you sign up and accept the rules. What is Kommit →

Редакция КоммитРедакция портала
commit e223b29 · 15 days ago

Kubernetes, part 4: volumes and storage, StatefulSet, Helm 4, and PostgreSQL via the CloudNativePG operator

🌐 Machine-translated from Russian. Show original

In this part:

How Kubernetes works with disks#

ObjectWhat is this
StorageClassthe "type" of storage and who creates it: local disk, NFS, Longhorn, Ceph, cloud disk,
PersistentVolumeClaim (PVC)application request: "needs 10 GB, ReadWriteOnce"
PersistentVolume (PV)a specific volume created for a

The application requests PVC, not a specific drive — so the same manifest works both in k3s and in the cloud.

Access modeMeans
ReadWriteOnce (RWO)volume connects to a single node — the database, most applications
ReadWriteMany (RWX)simultaneously from multiple nodes — shared files; NFS, CephFS, or Longhorn with RWX

local-path in k3s#

By default, k3s has StorageClass local-path: volume is the directory in /var/lib/rancher/k3s/storage on the node where pod is running.

kubectl get storageclass
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: data
  namespace: demo
spec:
  accessModes: [ReadWriteOnce]
  storageClassName: local-path
  resources:
    requests:
      storage: 5Gi

Connection in Deployment:

      containers:
        - name: app
          volumeMounts:
            - name: data
              mountPath: /data
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: data

⚠️ local-path binds data to a single node: if a node dies, data is inaccessible until it is raised. For one server, this is normal (backup the directory). For a cluster of multiple nodes, replicable storage is required — Longhorn (part 5), Ceph (Rook), or external NFS/storage with a CSI driver.

Deployment or StatefulSet#

DeploymentStatefulSet
Pod namesrandom (app-7d9f...)constant (db-0, db-1)
Volumescommon PVC or without volumestheir PVC for each pod (volumeClaimTemplates)
Order of launchinganysequentially
What is the purpose ofweb, APIs, stateless workersdatabases, queues, clusters with node roles,

Writing a StatefulSet for a database manually is only worth learning about. For production, operators are hired — programs that know how to deploy, backup, and switch a specific DBMS.

Helm 4: Kubernetes Batch Manager#

Helm stands for "apt for Kubernetes": the chart contains manifest templates, and you only change the values (values). The current major version is Helm 4 (as of November 2026 — 4.3.1).

curl -fsSL https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-4 | bash
helm version

It can also be packaged: on Windows — winget install Helm.Helm; variants for other systems — on helm.sh/docs/intro/install.

Main commands using the same podinfo as an example:

helm repo add podinfo https://stefanprodan.github.io/podinfo
helm repo update
helm show values podinfo/podinfo > podinfo-values.yaml     # все настройки чарта
helm install web podinfo/podinfo -n web --create-namespace --set replicaCount=2
helm list -A
helm upgrade web podinfo/podinfo -n web -f podinfo-values.yaml
helm history web -n web
helm rollback web 1 -n web
helm uninstall web -n web

Many projects distribute charts through OCI registries: helm install имя oci://реестр/путь/чарт --version X — this is how the cert manager is placed in part 5.

💡 Before installing someone else's chart, see which images it pulls and who supports them. Since 2025, for example, Bitnami has noticeably reduced the free image catalog, and many charts linked to them had to be moved to other sources. Store values (values.yaml) in Git and always specify the chart --version .

Your own chart — when it's time#

For now, the application has only one environment — the usual manifests from part 3 are enough. When test and prod with different domains and number of replicas appear, either their own chart (helm create myapp will create a stub) or Kustomize (built into kubectl: kubectl apply -k) with a common database and "overlays" for each environment.

PostgreSQL in Kubernetes: CloudNativePG#

CloudNativePG is an open PostgreSQL statement for Kubernetes: it creates a cluster with replicas, failovers, and backs up to S3-compatible storage.

helm repo add cnpg https://cloudnative-pg.github.io/charts
helm upgrade --install cnpg cnpg/cloudnative-pg -n cnpg-system --create-namespace
kubectl -n cnpg-system get pods

Cluster of a single instance for the test (in production — instances: 3 on different nodes):

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: appdb
  namespace: demo
spec:
  instances: 1
  storage:
    size: 10Gi
    storageClass: local-path
  bootstrap:
    initdb:
      database: app
      owner: app
kubectl apply -f appdb.yaml
kubectl -n demo get cluster,pods
kubectl -n demo get secret appdb-app -o jsonpath='{.data.uri}' | base64 -d; echo

The operator will create the user themselves, the secret appdb-app with a connection string, and services: appdb-rw (write, current primary instance) and appdb-ro (read from lines). Connect the application to appdb-rw and retrieve the password from the secret via secretKeyRef.

Compatible versions of PostgreSQL, backup configuration, and recovery can be found in the CloudNativePG documentation. General principles — backup with WAL archive, recovery test, replication monitoring — are the same as in the "PostgreSQL for Administrator" series.

⚠️ If the company has one database and no experience with Kubernetes, PostgreSQL on a separate VM next to the cluster is a normal and often more reliable solution. The operator is justified when there are many databases or a unified way to issue them to commands is needed.

Backup of single k3s#

On one server, the entire cluster state is in /var/lib/rancher/k3s/server (SQLite database, token, certificates), and local-path data is in /var/lib/rancher/k3s/storage. Back up both directories, for example restic according to the "Small Company Infrastructure" scheme, and databases with their own resources. Manifests and values are already in Git.

Next — a real cluster: Kubernetes, part 5: a cluster of several nodes on k3s with etcd, Longhorn, Ingress, and HTTPS via cert-manager.

Comments 00 участников · 0 гостей🔒 Rate and discuss after signing up

Comments · 0

No comments yet.Sign in to comment, rate and see all comments.