How to run a keycloak as second container after first container postgres Database start up at multi-container pod environment of kubernetes?

4/19/2021

In a multi-container pod:

step-1: Deploy first container Postgres Database and create a schema

step-2: Wait until the Postgres pod came up

step-3: then start deploying second container keycloak

I have written below deployment file to run :

apiVersion: apps/v1
kind: Deployment
metadata:
  name: idms
  namespace: default
  labels:
    app: idms
spec:
  replicas: 1
  selector:
    matchLabels:
      app: idms
  template:
    metadata:
      labels:
        app: idms
    spec:
      containers:
       - name: postgres
         image: registry.prod.srv.da.nsn-rdnet.net/edge/postgres:12.3-alpine
         imagePullPolicy: "IfNotPresent"
         ports:
           - containerPort: 5432
         lifecycle:
            postStart:
              exec:
                command: ["/bin/bash","-c","sleep 5 && PGPASSWORD=$POSTGRES_PASSWORD psql $POSTGRES_DB -U $POSTGRES_USER -c \'CREATE SCHEMA IF NOT EXISTS keycloak;\'"]  
         envFrom:
           - configMapRef:
               name: postgres-config
       - name: keycloak
         image: quay.io/keycloak/keycloak:10.0.1
         env:
          - name: KEYCLOAK_USER
            value: "admin"
          - name: KEYCLOAK_PASSWORD
            value: "admin"
          - name: REALM
            value: "ntc"
          - name: PROXY_ADDRESS_FORWARDING
            value: "true"
          - name: DB_ADDR
            value: "localhost"
          - name: DB_PORT
            value: "5432"
          - name: DB_DATABASE
            value: "postgresdb"
          - name: DB_USER
            value: "xxxxxxxxx"
          - name: DB_PASSWORD
            value: "xxxxxxxxx"
          - name: DB_SCHEMA
            value: "keycloak"    
          - name: KEYCLOAK_IMPORT
            value: "/opt/jboss/keycloak/startup/elements/realm.json"
         volumeMounts:
           - mountPath: /var/lib/postgresql/data
             name: postgredb 
           - mountPath: /opt/jboss/keycloak/startup/elements
             name: elements 
         ports:
          - name: http
            containerPort: 8080
          - name: https
            containerPort: 8443
         readinessProbe:
            httpGet:
              path: /auth/realms/master
              port: 8080
      volumes:
        - name: elements
          configMap:
            name: keycloak-elements
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pv-claim   

but keycloak is starting with H2 embedded database instead of Postgres. if I am using init-container to nslookup on Postgres on deployment file like below :

initContainers:
      - name: init-postgres
        image: busybox
        command: ['sh', '-c', 'until nslookup postgres; do echo waiting for postgres; sleep 2; done;']

pod is getting stuck at "podinitialization"

-- Siddhanta Rath
keycloak
kubernetes
postgresql

1 Answer

4/19/2021

you forget to add the

- name: DB_VENDOR
  value: POSTGRES

in the deployment YAML file due to that keycloak by default using the H2 database mode.

YAML ref file : https://github.com/harsh4870/Keycloack-postgres-kubernetes-deployment/blob/main/keycload-deployment.yaml

-- Harsh Manvar
Source: StackOverflow