Helm/PostgreSQL/PV

From Chorke Wiki
Revision as of 03:23, 13 June 2025 by Shahed (talk | contribs) (Install)
Jump to navigation Jump to search
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update && helm repo list
kubectl config get-contexts

Config

export KUBECONFIG="${HOME}/.kube/aws-kubeconfig.yaml"
export KUBECONFIG="${HOME}/.kube/dev-kubeconfig.yaml"
export KUBECONFIG="${HOME}/.kube/gcp-kubeconfig.yaml"
export KUBECONFIG="${HOME}/.kube/config"
cat <<'EXE'| sudo bash
          mkdir -p /var/minikube/pvc/postgresql/data-postgresql-0/
chown -R 1001:1001 /var/minikube/pvc/postgresql/
EXE

Install

helm show values bitnami/postgresql --version=15.5.20|less
helm show values bitnami/postgresql --version=15.5.21|less

kubectl get ns|grep postgresql
kubectl delete ns   postgresql

kubectl get ns|grep postgresql
kubectl create ns   postgresql
cat <<ENV | kubectl -n postgresql create secret generic postgresql --from-env-file=/dev/stdin
replication-password=sadaqah!
postgres-password=sadaqah!
password=sadaqah!
ENV




cat <<'YML'| kubectl apply -f -

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgresql-data-postgresql-0
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: hostpath
  hostPath:
    path: /var/hostpath_pv/postgresql/data-postgresql-0
    type: DirectoryOrCreate
YML
cat << YML | kubectl apply -f -
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  labels:
    app.kubernetes.io/name: postgresql
  name: data-postgresql-0
  namespace: postgresql
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  storageClassName: hostpath
  volumeName: postgresql-data-postgresql-0
YML

cat <<YML | helm -n postgresql install    postgresql bitnami/postgresql --version=15.5.21 -f -
---
global:
  defaultStorageClass: hostpath
  postgresql:
    auth:
      existingSecret: postgresql
      username: academia
      database: academia
primary:
  service:
    type: LoadBalancer
    loadBalancerIP: 192.168.49.102
  persistence:
    size: 10Gi
    storageClass: hostpath
    existingClaim: data-postgresql-0
initdbScripts:
  initdb:
    scripts:
      init-chorke.sql: |
         -- create database
         CREATE DATABASE academia_flair_staging;
         CREATE DATABASE academia_audit_staging;
         CREATE DATABASE academia_quote_staging;
         CREATE DATABASE academia_users_staging;
         
         -- create user
         CREATE USER chorke WITH ENCRYPTED PASSWORD 'sadaqah!';
         -- ALTER USER chorke WITH NOSUPERUSER;
         -- ALTER USER chorke WITH SUPERUSER;

         -- grant owner
         ALTER DATABASE academia_flair_staging OWNER TO chorke;
         ALTER DATABASE academia_audit_staging OWNER TO chorke;
         ALTER DATABASE academia_quote_staging OWNER TO chorke;
         ALTER DATABASE academia_users_staging OWNER TO chorke;
         
         -- grant access
         GRANT ALL PRIVILEGES ON DATABASE academia_flair_staging TO chorke;
         GRANT ALL PRIVILEGES ON DATABASE academia_audit_staging TO chorke;
         GRANT ALL PRIVILEGES ON DATABASE academia_quote_staging TO chorke;
         GRANT ALL PRIVILEGES ON DATABASE academia_users_staging TO chorke;
YML


echo -n password: ; read -s PGPASSWORD; export PGPASSWORD; echo
psql -h192.168.49.102 -p5432 -Uacademia academia
psql -h192.168.49.102 -p5432 -Upostgres postgres
echo -n password: ; read -s PGPASSWORD; export PGPASSWORD; echo
psql 'postgres://academia:@192.168.49.102:5432/academia'
psql 'postgres://postgres:@192.168.49.102:5432/postgres'

Uninstall

kubectl delete namespace postgresql
helm uninstall -n postgresql postgresql
kubectl delete pv postgresql-data-postgresql-0

Swiss Knife

AWS » EKS

Playground

helm -n postgresql install    postgresql bitnami/postgresql --version=15.5.20
helm -n postgresql upgrade -i postgresql bitnami/postgresql --version=15.5.21
helm show values bitnami/postgresql --version=15.5.21|less

kubectl -n postgresql get secret postgresql -o json|jq -r '.data."postgres-password"'|base64 -d;echo
kubectl -n postgresql get secret postgresql -o json|jq -r '.data.password'|base64 -d;echo

kubectl -n postgresql exec -it svc/postgresql -c postgresql -- psql -Upostgres
kubectl -n postgresql exec -it svc/postgresql -c postgresql -- bash
kubectl -n postgresql exec -it svc/postgresql -- psql -Upostgres

kubectl config --kubeconfig=${HOME}/.kube/aws-kubeconfig.yaml view --flatten
kubectl config --kubeconfig=${HOME}/.kube/dev-kubeconfig.yaml view --flatten
kubectl config --kubeconfig=${HOME}/.kube/gcp-kubeconfig.yaml view --flatten
kubectl config --kubeconfig=${HOME}/.kube/config view --flatten

kubectl -n postgresql delete all --all
kubectl -n postgresql delete ing --all
kubectl -n postgresql delete sts --all
kubectl    delete  pv postgresql-data-postgresql-0
kubectl -n postgresql delete svc --all
kubectl -n postgresql delete pvc --all

kubectl -n postgresql rollout history sts postgresql
kubectl -n postgresql rollout restart sts postgresql
kubectl -n postgresql rollout status  sts postgresql
kubectl -n postgresql exec -it postgresql-0  -- psql -Upostgres
kubectl -n postgresql logs -f svc/postgresql -c postgresql
kubectl -n postgresql logs -f svc/postgresql

References