This repository provides a guide on how to experiment with CloudNativePG on a local machine and manage PostgreSQL clusters the GitOps way using ArgoCD.
It is based on the official documentation of CloudNativePG and ArgoCD, as well as on Gabriele Bartolini’s blog.
⚠️ Please note that this is intended for experimental evaluation and should not be used in a production setting.
https://docs.docker.com/get-started/get-docker
Included with Docker Desktop, otherwise:
https://kubernetes.io/docs/tasks/tools/install-kubectl-linux
https://cloudnative-pg.io/documentation/current/kubectl-plugin
curl -sSfL \
https://github.com/cloudnative-pg/cloudnative-pg/raw/main/hack/install-cnpg-plugin.sh | \
sudo sh -s -- -b /usr/local/binhttps://kind.sigs.k8s.io/docs/user/quick-start
[ $(uname -m) = x86_64 ] && curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.27.0/kind-linux-amd64
sudo mv ./kind /usr/local/bin/kind
chmod 744 /usr/local/bin/kind kind create cluster --config ./kind/five-nodes.yamlOutput:
Creating cluster "cnpg" ...
Creating cluster "kind" ...
✓ Ensuring node image (kindest/node:v1.32.2) 🖼
✓ Preparing nodes 📦 📦 📦 📦 📦
✓ Writing configuration 📜
✓ Starting control-plane 🕹️
✓ Installing CNI 🔌
✓ Installing StorageClass 💾
✓ Joining worker nodes 🚜
Set kubectl context to "kind-kind"
You can now use your cluster with:
kubectl cluster-info --context kind-kind
Thanks for using kind! 😊https://helm.sh/docs/intro/install/#from-script
$ curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
$ chmod 700 get_helm.sh
$ ./get_helm.sh# Add kubernetes-dashboard repository
helm repo add kubernetes-dashboard https://kubernetes.github.io/dashboard/
# Deploy a Helm Release named "kubernetes-dashboard" using the kubernetes-dashboard chart
helm upgrade --install kubernetes-dashboard kubernetes-dashboard/kubernetes-dashboard --create-namespace --namespace kubernetes-dashboard
# Create a sample user with long-lived Bearer Token for ServiceAccount
kubectl apply -f ./kube-dashboard/admin-user.yaml
# Get the token from the Secret
kubectl get secret admin-user -n kubernetes-dashboard -o jsonpath="{.data.token}" | base64 -d
# Start the command line proxy
kubectl -n kubernetes-dashboard port-forward svc/kubernetes-dashboard-kong-proxy 8443:443Kubectl will make the Dashboard available at https://localhost:8443.
Check for the latest version: https://cloudnative-pg.io/documentation/current/installation_upgrade/#directly-using-the-operator-manifest
kubectl apply --server-side -f \
https://raw.githubusercontent.com/cloudnative-pg/cloudnative-pg/release-1.25/releases/cnpg-1.25.1.yamlCheck the Deployment status:
kubectl get deployment -n cnpg-system cnpg-controller-managerkubectl apply -f ./cnpg/cluster-example.yamlCheck the status of the pods:
kubectl get pods -wCheck the cluster:
kubectl get cluster cluster-exampleCheck the status of the cluster with the cnpg plugin:
kubectl cnpg status cluster-exampleCheck the components:
# Pods
kubectl get pods
# Nodes
kubectl get nodes
# Custom Resource Definitions
kubectl get crds
# Namespaces
kubectl get namespaces
# Services
kubectl get svc
# Persistent Volume Claims
kubectl get pvc
# Secrets
kubectl get secretDelete the cluster:
kubectl delete cluster cluster-examplehttps://argo-cd.readthedocs.io/en/latest/getting_started/
# ArgoCD
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml# Argo CD CLI
curl -sSL -o argocd-linux-amd64 https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64
sudo install -m 555 argocd-linux-amd64 /usr/local/bin/argocd
rm argocd-linux-amd64Port Forwarding to connect to the API and UI:
kubectl port-forward svc/argocd-server -n argocd 8080:443Get the admin password:
argocd admin initial-password -n argocd
# or
kubectl get secret argocd-initial-admin-secret -o jsonpath='{.data.password}' -n argocd | base64 --decode && echoThe API server and UI can then be accessed using https://localhost:8080 with username admin and the password from above.
Create an simple app:
kubectl apply -f ./argocd-app/cnpg.yamlThe ArgoCD application should be now visible in the UI, but without the CNPG cluster. The cluster will be deployed with the first sync. By default the automated sync is turned off. Use the "automated" attribute under syncPolicy to turn it on.
Sync the manifest of the cluster manually:
- Push "Sync" in the UI.
- Sync from local manifests directly, only for development purposes:
argocd login https://localhost:8080
argocd app sync cnpg --local ./argocd-apps/cnpg.ymlTo test cnpg serviceTemplate with LoadBalancer and expose Postgres outside the kubernetes cluster like in the manifest cnpg/cluster-example-lb.yaml
To test cnpg serviceTemplate with LoadBalancer and expose Postgres outside the kubernetes cluster like in the manifest cnpg/cluster-example-lb.yaml
https://github.com/kubernetes-sigs/cloud-provider-kind?tab=readme-ov-file#install
go install sigs.k8s.io/cloud-provider-kind@latestOn macOS and WSL2 you must run cloud-provider-kind using sudo
sudo ~/go/bin/cloud-provider-kindGet the app user password:
kubectl get secret cluster-example-app -o jsonpath='{.data.password}' | base64 --decode && echoUse the IP provided by the cloud-provider-kind to connect with psql:
psql -h 172.19.0.7 -p 5432 -U app -d app