How do I customize PostgreSQL configurations using helm chart?

6/19/2019

I'm trying to deploy an application that uses PostgreSQL as a database to my minikube. I'm using helm as a package manager, and add have added PostgreSQL dependency to my requirements.yaml. Now the question is, how do I set postgres user, db and password for that deployment? Here's my templates/applicaion.yaml

apiVersion: v1
kind: Service
metadata:
  name: {{ template "sgm.fullname" . }}-service
spec:
  type: NodePort
  selector:
    app: {{ template "sgm.fullname" . }}
  ports:
  - port: 80
    targetPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "sgm.fullname" . }}-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: {{ template "sgm.fullname" . }}
  template:
    metadata:
      labels:
        app: {{ template "sgm.fullname" . }}
    spec:
      containers:
      - name: sgm
        image: mainserver/sgm
        env:
        - name: POSTGRES_HOST
          value: {{ template "postgres.fullname" . }}.default.svc.cluster.local

I've tried adding a configmap as it is stated in the postgres helm chart github Readme, but seems like I'm doing something wrong

-- Carmine
kubernetes
kubernetes-helm

1 Answer

6/19/2019

This is lightly discussed in the Helm documentation: your chart's values.yaml file contains configuration blocks for the charts it includes. The GitHub page for the Helm stable/postgresql chart lists out all of the options.

Either in your chart's values.yaml file, or in a separate YAML file you pass to the helm install -f option, you can set parameters like

postgresql:
  postgresqlDatabase: stackoverflow
  postgresqlPassword: enterImageDescriptionHere

Note that the chart doesn't create a non-admin user (unlike its sibling MySQL chart). If you're okay with the "normal" database user having admin-level privileges (like creating and deleting databases) then you can set postgresqlUser here too.

In your own chart you can reference these values like any other

- name: PGUSER
  value: {{ .Values.postgresql.postgresqlUser }}
-- David Maze
Source: StackOverflow