0
0
Kubernetesdevops~7 mins

Database operators example in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Managing databases in Kubernetes can be complex because databases need special care for storage and backups. Database operators help automate these tasks so your database runs smoothly without manual work.
When you want to run a PostgreSQL database inside Kubernetes with automatic backups and scaling.
When you need to manage a MySQL database that recovers automatically after failures.
When you want to deploy a MongoDB cluster with easy updates and monitoring inside Kubernetes.
When you want to avoid manual setup of persistent storage and configuration for your database.
When you want your database to be managed like any other Kubernetes resource with simple commands.
Config File - postgres-operator.yaml
postgres-operator.yaml
apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
  name: my-postgres
  namespace: default
spec:
  instances:
    - name: instance1
      replicas: 1
  backups:
    pgbackrest:
      repos:
        - name: repo1
          volume:
            volumeClaimSpec:
              accessModes:
                - ReadWriteOnce
              resources:
                requests:
                  storage: 1Gi
  storage:
    volumes:
      - name: data
        volumeClaimSpec:
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 1Gi

This YAML file defines a PostgreSQL cluster managed by the Crunchy Data Postgres Operator.

apiVersion and kind specify the operator resource type.

metadata names the cluster.

spec.instances defines one instance with one replica.

spec.backups configures backup storage using pgBackRest.

spec.storage sets persistent storage for the database data.

Commands
This command creates the PostgreSQL cluster resource in Kubernetes using the operator. It tells Kubernetes to start managing the database with the settings in the YAML file.
Terminal
kubectl apply -f postgres-operator.yaml
Expected OutputExpected
postgrescluster.postgres-operator.crunchydata.com/my-postgres created
This command lists the pods created by the operator for the PostgreSQL cluster to check if the database is running.
Terminal
kubectl get pods -l postgres-operator.crunchydata.com/cluster=my-postgres
Expected OutputExpected
NAME READY STATUS RESTARTS AGE my-postgres-instance1-0 1/1 Running 0 30s
-l - Filter pods by label to show only those belonging to the PostgreSQL cluster
This command shows detailed information about the PostgreSQL cluster resource, including status, events, and conditions managed by the operator.
Terminal
kubectl describe postgrescluster my-postgres
Expected OutputExpected
Name: my-postgres Namespace: default Labels: Annotations: API Version: postgres-operator.crunchydata.com/v1beta1 Kind: PostgresCluster Spec: Instances: Name: instance1 Replicas: 1 Storage: Volumes: Name: data Storage Request: 1Gi Status: State: Running Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal SuccessfulCreate 1m postgres-operator-controller Created pod my-postgres-instance1-0
Key Concept

If you remember nothing else from this pattern, remember: database operators automate complex database tasks inside Kubernetes so you can manage databases like regular apps.

Common Mistakes
Applying the operator resource without installing the database operator first
Kubernetes does not recognize the custom resource and returns an error because the operator controller is missing.
Install the database operator in the cluster before applying any database custom resources.
Not specifying persistent storage in the YAML file
The database pods will not have storage to save data, causing data loss on pod restarts.
Always define persistent volume claims in the storage section to keep data safe.
Summary
Create a database cluster resource YAML file for the operator to manage.
Apply the YAML file with kubectl to start the database cluster.
Check pods with kubectl get pods to verify the database is running.
Use kubectl describe on the cluster resource to see detailed status and events.