postgresql-single

by serge.logvinov

Helm chartv1.16.0App 16.16Verified publisher

Postgres with backup/restore and replication

About this chart

postgresql-single

Postgres with backup/restore and replication

Homepage: https://github.com/sergelogvinov/helm-charts

Maintainers

NameEmailUrl
sergelogvinovhttps://github.com/sergelogvinov

Source Code

A single-node PostgreSQL database is straightforward and relatively easy to maintain. You don't need to worry about configuring a distributed database or managing multiple nodes.

  • Support backup/restore to the extrernal storage, using wal-g
  • Automatically tuning PostgreSQL parameters base on postgresqlServerMemory value.
# create backup-s3 secret
kubectl create secret generic backup-s3 --from-file=credentials
# Helm values

installationType: standalone # or cnpg

pgHbaConfiguration:
  # host    database    user                  address       auth-method
  - hostssl app         app                   10.0.0.0/8    md5

postgresqlServerMemory: 2048
resources:
  limits:
    memory: 4Gi
    cpu: 4
  requests:
    memory: 2Gi
    cpu: 2

extraVolumes:
  - name: s3-certs
    secret:
      defaultMode: 256
      secretName: backup-s3
extraVolumeMounts:
  - name: s3-certs
    mountPath: /var/lib/postgresql/.aws
    readOnly: true

backup:
  enabled: true

  recovery: true # If PVC empty - it will use latest backup.

  walpush: true  # Send wal to the s3.
  walg:
    WALG_TAR_DISABLE_FSYNC: true
    WALG_UPLOAD_WAL_METADATA: INDIVIDUAL
    WALG_PREVENT_WAL_OVERWRITE: true
    WALG_COMPRESSION_METHOD: brotli
    WALG_LIBSODIUM_KEY_TRANSFORM: hex
    WALG_LIBSODIUM_KEY: "" # generate it: openssl rand -hex 32
    WALG_S3_PREFIX: s3://backup/postgrtes-backup

backupCheck:
  enabled: true

metrics:
  enabled: true

Values

KeyTypeDefaultDescription
replicaCountint1
image.repositorystring"ghcr.io/sergelogvinov/postgresql"
image.pullPolicystring"IfNotPresent"
image.tagstring""
imagePullSecretslist[]
nameOverridestring""
fullnameOverridestring""
installationTypestring"standalone"Installation type can be "cnpg" or "standalone"
commandlist[]command Override default container command
envlist[]Environment variables
postgresqlUsernamestring"postgres"PostgreSQL admin user ref: https://hub.docker.com/_/postgres
postgresqlMaxConnectionsint150Create a database ref: https://hub.docker.com/_/postgres postgresqlDatabase:
pgHbaConfigurationlist[]Postgres auth ref: https://www.postgresql.org/docs/current/auth-pg-hba-conf.html
initdb.argsstring"--data-checksums --auth-host=scram-sha-256"PostgreSQL initdb parameters ref: https://www.postgresql.org/docs/current/app-initdb.html
initdb.databasestring"app"Create owner and database
initdb.usernamestring"app"
initdb.passwordstringnil
initdb.scriptlist[]PostgreSQL initdb scripts ref: https://hub.docker.com/_/postgres
tlsCerts.createboolfalse
tlsCerts.modestring"require"
pooler.enabledboolfalse
pooler.replicaCountint1
pooler.types[0]string"rw"
pooler.types[1]string"ro"
pooler.modestring"session"
pooler.parametersobject{}
pooler.service.typestring"ClusterIP"Service type ref: https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types
pooler.service.labelsobject{}Extra labels for load balancer service. ref: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
pooler.service.annotationsobject{}Extra annotations for load balancer service. ref: https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/
pooler.service.externalTrafficPolicystring"Cluster"Traffic policies. ref: https://kubernetes.io/docs/reference/networking/virtual-ips/#traffic-policies
pooler.service.internalTrafficPolicystring"Cluster"
pooler.service.ipFamilieslist["IPv4"]IP families for service possible values: IPv4, IPv6 ref: https://kubernetes.io/docs/concepts/services-networking/dual-stack/
pooler.autoscalingobject{"controlledResources":["cpu","memory"],"controlledValues":"RequestsOnly","enabled":false,"maxAllowed":{},"minAllowed":{},"updatePolicy":{"updateMode":"InPlaceOrRecreate"}}Vertical pod autoscaler
pooler.autoscaling.controlledResourceslist["cpu","memory"]Resource to control Possible values are "cpu" and "memory"
pooler.autoscaling.controlledValuesstring"RequestsOnly"Controls which resource value should be autoscaled Possible values are "RequestsAndLimits" and "RequestsOnly"
pooler.autoscaling.maxAllowedobject{}Max allowed resources for the pod default is resources.limits
pooler.autoscaling.minAllowedobject{}Min allowed resources for the pod default is resources.requests
pooler.autoscaling.updatePolicyobject{"updateMode":"InPlaceOrRecreate"}Update policy Possible values are "Off", "Initial", "Recreate", "InPlaceOrRecreate" and "Auto"
pooler.resources.limits.cpustring"500m"
pooler.resources.limits.memorystring"64Mi"
pooler.resources.requests.cpustring"100m"
pooler.resources.requests.memorystring"32Mi"
pooler.priorityClassNamestringnilPriority Class Name ref: https://kubernetes.io/docs/concepts/configuration/pod-priority-preemption/#priorityclass
pooler.podAntiAffinityPresetstring"soft"Pod Anti Affinity soft/hard
pooler.podAntiAffinityPresetKeystring"kubernetes.io/hostname"
pooler.nodeSelectorobject{}Node labels for pod assignment. ref: https://kubernetes.io/docs/user-guide/node-selection/
pooler.tolerationslist[]Tolerations for pod assignment. ref: https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/
pooler.affinityobject{}Affinity for pod assignment. ref: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#affinity-and-anti-affinity
initContainerslist[]
serviceAccountobject{"annotations":{},"create":true,"name":""}Pods Service Account. ref: https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/
podlabelsobject{}Extra labels for pod. ref: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
podAnnotationsobject{}Annotations for pod. ref: https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/
podSecurityContextobject{"fsGroup":999,"fsGroupChangePolicy":"OnRootMismatch","runAsGroup":999,"runAsNonRoot":true,"runAsUser":999}Pod Security Context. ref: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-pod
securityContextobject{"allowPrivilegeEscalation":false,"capabilities":{"drop":["ALL"]},"seccompProfile":{"type":"RuntimeDefault"}}Container Security Context. ref: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-pod
serviceobject{"ipFamilies":["IPv4"],"port":5432,"type":"ClusterIP"}Service parameters ref: https://kubernetes.io/docs/user-guide/services/
service.ipFamilieslist["IPv4"]IP families for service possible values: IPv4, IPv6 ref: https://kubernetes.io/docs/concepts/services-networking/dual-stack/
postgresqlServerMemorystring"128"Specifies the expected memory usage
postgresqlServerWalWriterDelaystring"200ms"Specifies how often the WAL writer flushes WAL (wal_writer_delay) ref: https://www.postgresql.org/docs/current/runtime-config-wal.html
autoscalingobject{"controlledResources":["cpu","memory"],"controlledValues":"RequestsOnly","enabled":false,"maxAllowed":{},"minAllowed":{},"updatePolicy":{"updateMode":"InPlaceOrRecreate"}}Vertical pod autoscaler
autoscaling.controlledResourceslist["cpu","memory"]Resource to control Possible values are "cpu" and "memory"
autoscaling.controlledValuesstring"RequestsOnly"Controls which resource value should be autoscaled Possible values are "RequestsAndLimits" and "RequestsOnly"
autoscaling.maxAllowedobject{}Max allowed resources for the pod default is resources.limits
autoscaling.minAllowedobject{}Min allowed resources for the pod default is resources.requests
autoscaling.updatePolicyobject{"updateMode":"InPlaceOrRecreate"}Update policy Possible values are "Off", "Initial", "Recreate", "InPlaceOrRecreate" and "Auto"
resourcesobject{"requests":{"cpu":"100m","memory":"128Mi"}}Resource requests and limits. ref: https://kubernetes.io/docs/user-guide/compute-resources/
persistenceobject{"accessModes":["ReadWriteOnce"],"annotations":{},"enabled":false,"mountPath":"/database","size":"8Gi"}Persistence parameters ref: https://kubernetes.io/docs/user-guide/persistent-volumes/
priorityClassNamestringnilPriority Class Name ref: https://kubernetes.io/docs/concepts/configuration/pod-priority-preemption/#priorityclass
terminationGracePeriodSecondsint120The time in seconds that is allowed for a PostgreSQL instance to gracefully shutdown ref: https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks
updateStrategy.typestring"RollingUpdate"
backup.enabledboolfalse
backup.recoveryboolfalse
backup.recoveryMethodstring"backup"
backup.walpushboolfalse
backup.walgSecretsstring""
backup.walgobject{}
backup.walgSourceConfigstringnil
backup.cleanPolicystring"retain FULL 3"
backup.schedulestring"15 4 * * *"Backup schedule. set value "" to disable cron backup refs: https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/
backup.resourcesobject{"limits":{"cpu":2,"memory":"4Gi"},"requests":{"cpu":"1500m","memory":"768Mi"}}Resource requests and limits. ref: https://kubernetes.io/docs/user-guide/compute-resources/
backup.priorityClassNamestringnilPriority Class Name ref: https://kubernetes.io/docs/concepts/configuration/pod-priority-preemption/#priorityclass
backup.podAffinityPresetstring"hard"
backupCheck.enabledboolfalse
backupCheck.schedulestring"15 8 * * *"
backupCheck.resourcesobject{"limits":{"cpu":2,"memory":"1Gi"},"requests":{"cpu":"100m","memory":"512Mi"}}Resource requests and limits. ref: https://kubernetes.io/docs/user-guide/compute-resources/
backupCheck.persistenceobject{"accessModes":["ReadWriteOnce"],"annotations":{},"size":"8Gi"}Persistence parameters ref: https://kubernetes.io/docs/user-guide/persistent-volumes/
backupCheck.nodeSelectorobject{}Node labels for pod assignment. ref: https://kubernetes.io/docs/user-guide/node-selection/
backupCheck.tolerationslist[]Tolerations for pod assignment. ref: https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/
backupCheck.affinityobject{}Affinity for pod assignment. ref: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#affinity-and-anti-affinity
metrics.enabledboolfalse
metrics.image.repositorystring"quay.io/prometheuscommunity/postgres-exporter"
metrics.image.pullPolicystring"IfNotPresent"
metrics.image.tagstring"v0.11.1"
metrics.databasestring"postgres"
metrics.usernamestring"postgres"
metrics.slowQueriesbooltrue
metrics.queriesstring`"{{- if eq .Values.installationType "standalone" }}\npg_replication:\n query: SELECT CASE WHEN NOT pg_is_in_recovery() THEN 0 ELSE GREATEST (0, EXTRACT(EPOCH FROM (now() - pg_last_xact_replay_timestamp()))) END AS lag\n metrics:\n - lag:\n usage: "GAUGE"\n description: "Replication lag behind master in seconds"\npg_postmaster:\n query: SELECT pg_postmaster_start_time as start_time_seconds from pg_postmaster_start_time()\n metrics:\n - start_time_seconds:\n usage: "GAUGE"\n description: "Time at which postmaster started"\n{{- end }}\n{{- if and .Values.metrics.slowQueries (regexMatch "^[0-9]+\\.[0-9]+$" .Values.image.tag) (semverCompare ">=14.0" (default .Chart.AppVersion .Values.image.tag)) }}\npg_stat_slow_queries:\n query: SELECT pg_get_userbyid(userid) as rolname,t3.datname,queryid,calls,max_exec_time / 1000 as max_time_seconds,REGEXP_REPLACE(substring(query,1,200),'["\n\s\t]+',' ','g') as sql FROM pg_stat_statements t1 JOIN pg_database t3 ON (t1.dbid=t3.oid) WHERE datname != 'postgres' AND rows != 0 AND max_exec_time > 1000 ORDER BY max_exec_time DESC LIMIT 10\n metrics:\n - rolname:\n usage: "LABEL"\n description: "Name of user"\n - datname:\n usage: "LABEL"\n description: "Name of database"\n - queryid:\n usage: "LABEL"\n description: "Query ID"\n - sql:\n usage: "LABEL"\n description: "SQL"\n - calls:\n usage: "COUNTER"\n description: "Number of times executed"\n - max_time

Deploy postgresql-single with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has postgresql-single on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartpostgresql-single is pinned at v1.16.0 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below
opentenant validate opentenant.yaml
terraform init && terraform apply