DEV Community

Tingwei
Tingwei

Posted on

Setting Up CloudNativePG on KIND with pgAdmin

1. Install Prerequisites

Install Helm, KIND, kubectl, and pgAdmin4.

2. Create KIND Cluster

Run kind create cluster --name myk8s --config cluster.yaml

### cluster.yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
  - role: control-plane
  - role: worker
  - role: worker
Enter fullscreen mode Exit fullscreen mode

3. Deploy CloudNativePG Operator

helm repo add cnpg https://cloudnative-pg.github.io/charts
helm repo update
helm install cnpg cnpg/cloudnative-pg --namespace cnpg-system --create-namespace
Enter fullscreen mode Exit fullscreen mode

4. Deploy PostgreSQL Cluster

Run kubectl apply -f postgres.yaml

## postgres.yaml
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: pg-cluster
  labels:
    app: pg
spec:
  instances: 3
  storage:
    pvcTemplate:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 1Gi
      storageClassName: standard
      volumeMode: Filesystem
  postgresql:
    parameters:
      shared_buffers: 256MB
---
# pgBouncer
apiVersion: postgresql.cnpg.io/v1
kind: Pooler
metadata:
  name: pg-pooler
  labels:
    app: pg
spec:
  cluster:
    name: pg-cluster
  instances: 3
  type: rw
  serviceTemplate:
    metadata:
      labels:
        app: pg
    spec:
      type: LoadBalancer
  pgbouncer:
    poolMode: session
    parameters:
      max_client_conn: "1000"
      default_pool_size: "10"
Enter fullscreen mode Exit fullscreen mode

arch

5. Install Kubernetes Cloud Provider for KIND

  • Installation
  • Control-plane nodes need to remove the label to access workloads via a LoadBalancer service.
kubectl label node myk8s-control-plane node.kubernetes.io/exclude-from-external-load-balancers-
Enter fullscreen mode Exit fullscreen mode
  • Run As administrator
./cloud-provider-kind
Enter fullscreen mode Exit fullscreen mode

6. Connect to PostgreSQL with pgAdmin4

Open pgAdmin4, create a new server with PostgreSQL connection details

  • EXTERNAL-IP
kubectl get svc pg-pooler
Enter fullscreen mode Exit fullscreen mode
kubectl get secret pg-cluster-app -o yaml
## check data.password
echo <password> | base64 -d
Enter fullscreen mode Exit fullscreen mode
host=<External IP>
port=5432
database=app
user=app
password=<password>
Enter fullscreen mode Exit fullscreen mode

References

Top comments (0)