> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Database CloudNativePG

> Guide to installing and using the CloudNativePG Kubernetes operator to deploy and manage PostgreSQL clusters with StatefulSets, Services, and monitoring via Glasskube.

Welcome back. In this lesson we'll add a managed PostgreSQL database to the cluster using the CloudNativePG operator. CloudNativePG is a Kubernetes operator that simplifies running and operating PostgreSQL clusters on Kubernetes by provisioning StatefulSets, Services, and handling failover and replica management.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/9wfunwr4SxNjNTYq/images/Kubernetes-Administration-Package-Management-with-Glasskube/Package-Installation/Database-CloudNativePG/single-node-kubernetes-cluster-diagram.jpg?fit=max&auto=format&n=9wfunwr4SxNjNTYq&q=85&s=e45309a94485d40486904d2237e86f6b" alt="The image depicts a diagram of a Single-Node Kubernetes Cluster, highlighting observability, continuous deployment, and database components. It is labeled &#x22;Cluster Scoped&#x22; with icons representing each component." width="1920" height="1080" data-path="images/Kubernetes-Administration-Package-Management-with-Glasskube/Package-Installation/Database-CloudNativePG/single-node-kubernetes-cluster-diagram.jpg" />
</Frame>

Overview: this is the fourth and final cluster-scoped package we install. Key characteristics of this package:

| Property     | Details                |
| ------------ | ---------------------- |
| Scope        | Cluster-scoped package |
| Values       | No value definitions   |
| Entry points | No entry points        |
| Dependencies | No dependencies        |

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/9wfunwr4SxNjNTYq/images/Kubernetes-Administration-Package-Management-with-Glasskube/Package-Installation/Database-CloudNativePG/postgresql-database-operator-workloads-features.jpg?fit=max&auto=format&n=9wfunwr4SxNjNTYq&q=85&s=f5912a6b5dda2a3366ed81c9d9b82b3e" alt="The image is about a database operator for managing PostgreSQL workloads, highlighting features like &#x22;Cluster scoped,&#x22; &#x22;No value definitions,&#x22; and &#x22;No entry points.&#x22;" width="1920" height="1080" data-path="images/Kubernetes-Administration-Package-Management-with-Glasskube/Package-Installation/Database-CloudNativePG/postgresql-database-operator-workloads-features.jpg" />
</Frame>

We install the CloudNativePG operator into the `cnpg-system` namespace. The operator watches Postgres Cluster custom resources and creates StatefulSet-backed PostgreSQL instances and the Service endpoint applications use to connect. The diagram below shows the operator running in a system namespace while a sample application in a different namespace connects to the database cluster.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/9wfunwr4SxNjNTYq/images/Kubernetes-Administration-Package-Management-with-Glasskube/Package-Installation/Database-CloudNativePG/single-node-kubernetes-cluster-diagram-2.jpg?fit=max&auto=format&n=9wfunwr4SxNjNTYq&q=85&s=c678526b0cf9e675a8fa7d213007a5bf" alt="The image is a diagram illustrating a single-node Kubernetes cluster setup, showing a database with PostgreSQL primary instances in separate namespaces and a development namespace containing an application interacting with the database cluster." width="1920" height="1080" data-path="images/Kubernetes-Administration-Package-Management-with-Glasskube/Package-Installation/Database-CloudNativePG/single-node-kubernetes-cluster-diagram-2.jpg" />
</Frame>

Create a Postgres cluster by applying a Cluster manifest. The operator will translate this manifest into StatefulSets, PersistentVolumes (via PVCs), Services, and monitoring resources.

Example minimal Cluster manifest (3 instances, 1Gi storage, PodMonitor enabled):

```yaml theme={null}
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: cluster-with-metrics
spec:
  instances: 3
  storage:
    size: 1Gi
  monitoring:
    enablePodMonitor: true
```

You can install CloudNativePG from the Glasskube UI or via the Glasskube CLI. This is the installation screen in Glasskube:

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/9wfunwr4SxNjNTYq/images/Kubernetes-Administration-Package-Management-with-Glasskube/Package-Installation/Database-CloudNativePG/glasskube-ui-cloudnative-pg-installation.jpg?fit=max&auto=format&n=9wfunwr4SxNjNTYq&q=85&s=5d9fb3a517237fe190f359e924b24ecf" alt="The image shows the Glasskube UI for installing the &#x22;cloudnative-pg&#x22; package, with version selection and installation details." width="1920" height="1080" data-path="images/Kubernetes-Administration-Package-Management-with-Glasskube/Package-Installation/Database-CloudNativePG/glasskube-ui-cloudnative-pg-installation.jpg" />
</Frame>

Confirm the operator package is available in the Glasskube catalog with `glasskube list`. Sample output showing the `cloudnative-pg` entry:

```bash theme={null}
glasskube list
PACKAGE NAME        NAMESPACE        NAME                             VERSION       AUTO-UPDATE        REPOSITORY         STATUS
ingress-nginx                        glasskube                       Not installed
quickwit                              glasskube                       Not installed

NAME                            VERSION          AUTO-UPDATE        REPOSITORY           STATUS
akri                            -                -                  glasskube            Not installed
argo-cd                         v2.11.7+1       Enabled            glasskube            Ready
caddy-ingress-controller        -                -                  glasskube            Not installed
cert-manager                    -                -                  glasskube            Not installed
cloudnative-pg                  -                -                  glasskube            Not installed
cyclops                         -                -                  glasskube            Not installed
kube-prometheus-stack           v61.6.0+1       -                  glasskube            Ready
kubetail                        v0.6.0+1       Enabled            glasskube            Ready
```

In this walkthrough we enabled automatic updates and installed CloudNativePG from the Glasskube catalog. After installation, the package will appear as installed in the UI.

Before applying the Cluster manifest, switch your kubectl context to the `cnpg-system` namespace so commands default there:

```bash theme={null}
kubectl config set-context --current --namespace=cnpg-system
```

Apply the Cluster manifest using a heredoc to kubectl:

```bash theme={null}
kubectl apply -f - <<EOF
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: cluster-with-metrics
spec:
  instances: 3
  storage:
    size: 1Gi
  monitoring:
    enablePodMonitor: true
EOF
```

Watch the operator and StatefulSet pods initialize. If you have an alias like `kgp` for `kubectl get pods`, you can use that; otherwise run:

```bash theme={null}
kubectl get pods -n cnpg-system
```

Initial sample output while pods initialize (you will see init containers and join phases):

```text theme={null}
NAME                                      READY   STATUS            RESTARTS   AGE
cluster-with-metrics-1-initdb-lgsdq      0/1     Completed         0          23s
cluster-with-metrics-2-join-8dp7k        0/1     Init:0/1          0          5s
cnpg-controller-manager-987494b46-p7hqk  1/1     Running           0          2m6s
```

After a short time, the StatefulSet pods become Ready and the operator controller-manager is running:

```text theme={null}
NAME                                      READY   STATUS    RESTARTS   AGE
cluster-with-metrics-1                    1/1     Running   0          73s
cluster-with-metrics-2                    1/1     Running   0          44s
cluster-with-metrics-3                    1/1     Running   0          25s
cnpg-controller-manager-987494b46-p7hqk  1/1     Running   0          3m2s
```

<Callout icon="lightbulb" color="#1CB2FE">
  After the cluster is ready, CloudNativePG creates a Kubernetes Service for client connections and maintains that Service through failover events. Inspect the Service and Endpoints in `cnpg-system` to get host and port information used by applications.
</Callout>

That completes the demo. Practice installing the CloudNativePG operator and creating a Postgres Cluster using the manifest above.

Links and references:

* CloudNativePG documentation: [https://cloudnative-pg.io/](https://cloudnative-pg.io/)
* Kubernetes StatefulSets: [https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/](https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/)
* Glasskube project: [https://github.com/traefik/glasskube](https://github.com/traefik/glasskube) (Glasskube UI/CLI)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/k8s-administration-package-management-with-glasskube/module/c3806869-7f9e-4cc2-8dc5-aa10304e3d1c/lesson/2f665637-d054-4a1e-815d-1dc8926e9fe6" />

  <Card title="Practice Lab" icon="flask-conical" cta="Learn more" href="https://learn.kodekloud.com/user/courses/k8s-administration-package-management-with-glasskube/module/c3806869-7f9e-4cc2-8dc5-aa10304e3d1c/lesson/8cc03408-c700-46e0-b7fd-f4023f25df1d" />
</CardGroup>
