"Failed to connect to database" : Keycloak Operator external database Config not working

8/12/2021

I checked this How to use for Keycloak operator custom resource using external database connection. I am using CloudSQL from Google platform as the external database source.

My configurations are

  • keycloak-idm
apiVersion: keycloak.org/v1alpha1
kind: Keycloak
metadata:
  name: kiwigrid-keycloak-idm
spec:
  instances: 3
  externalAccess:
    enabled: false
  externalDatabase:
    enabled: true
  • external db storage secret
apiVersion: v1
kind: Secret
metadata:
    name: keycloak-db-secret
    namespace: kiwios-application
type: Opaque
stringData:
    POSTGRES_DATABASE: keycloak-storage
    POSTGRES_EXTERNAL_ADDRESS: pgsqlproxy.infra
    POSTGRES_EXTERNAL_PORT: "5432"
    POSTGRES_HOST: keycloak-postgresql
    POSTGRES_USERNAME: keycloak-user
    POSTGRES_PASSWORD: S1ly3AValJYBNR-fsptLYdT74
    POSTGRES_SUPERUSER: "true"
  • storage database
apiVersion: sql.cnrm.cloud.google.com/v1beta1
kind: SQLDatabase
metadata:
  name: keycloak-storage
  namespace: kiwios-application
  annotations:
    cnrm.cloud.google.com/deletion-policy: "abandon"
spec:
  charset: UTF8
  collation: en_US.UTF8
  instanceRef:
    name: keycloak-storage-instance-pg
    namespace: infra
  • storage users
apiVersion: sql.cnrm.cloud.google.com/v1beta1
kind: SQLUser
metadata:
  name: keycloak-user
  namespace: kiwios-application
  annotations:
    cnrm.cloud.google.com/deletion-policy: "abandon"
spec:
  instanceRef:
    name: keycloak-storage-instance-pg
    namespace: infra
  password:
    valueFrom:
      secretKeyRef:
        name: keycloak-db-secret
        key: POSTGRES_PASSWORD

And the error shown in Kubernetes console error

It is not working. Anyone please help me to figure out what I am doing wrong.

Update: I deep dived with k9s console. As per keycloak-operator functionality it creates a external name for the database connection. which is here keycloak-postgresql

check image below keycloak-postgresql works

There is no error showing in keycloak-operator console. Only the keycloak-idm is not able to make a connection using this external name. It shows the below error. error

-- Sankha
keycloak
keycloak-operator
kubernetes

2 Answers

12/7/2021

Have you tried this way..!

<!-- begin snippet: js hide: false console: true babel: false --><!-- language: lang-html -->
apiVersion: v1
kind: Secret
metadata:
    name: keycloak-db-secret
    namespace: kiwios-application
type: Opaque
stringData:
    POSTGRES_DATABASE: "keycloak-storage"
    POSTGRES_EXTERNAL_ADDRESS: "pgsqlproxy.infra"
    POSTGRES_EXTERNAL_PORT: "5432"
    POSTGRES_HOST: "keycloak-postgresql"
    POSTGRES_USERNAME: "keycloak-user"
    POSTGRES_PASSWORD: "S1ly3AValJYBNR-fsptLYdT74"
    POSTGRES_SUPERUSER: "true"
<!-- end snippet -->
-- Rakesh
Source: StackOverflow

8/12/2021

This is what i am using for keycloak setup, also if you have read the question he has mention secret issue issue in update section

apiVersion: v1
kind: Service
metadata:
  name: keycloak
  labels:
    app: keycloak
spec:
  ports:
  - name: http
    port: 8080
    targetPort: 8080
  selector:
    app: keycloak
  type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: keycloak
  namespace: default
  labels:
    app: keycloak
spec:
  replicas: 1
  selector:
    matchLabels:
      app: keycloak
  template:
    metadata:
      labels:
        app: keycloak
    spec:
      containers:
      - name: keycloak
        image: quay.io/keycloak/keycloak:10.0.0
        env:
        - name: KEYCLOAK_USER
          value: "admin"
        - name: KEYCLOAK_PASSWORD
          value: "admin"
        - name: PROXY_ADDRESS_FORWARDING
          value: "true"
        - name: DB_VENDOR
          value: POSTGRES
        - name: DB_ADDR
          value: postgres
        - name: DB_DATABASE
          value: keycloak
        - name: DB_USER
          value: root
        - name: DB_PASSWORD
          value: password
        - name : KEYCLOAK_HTTP_PORT
          value : "80"
        - name: KEYCLOAK_HTTPS_PORT
          value: "443"
        - name : KEYCLOAK_HOSTNAME
          value : keycloak.harshmanvar.tk #replace with ingress URL
        ports:
        - name: http
          containerPort: 8080
        - name: https
          containerPort: 8443
        readinessProbe:
          httpGet:
            path: /auth/realms/master
            port: 8080

You can try changing the ENV variables into the secret you are using.

Example files : https://github.com/harsh4870/Keycloack-postgres-kubernetes-deployment

Environment variables that Keycloak support : https://github.com/keycloak/keycloak-containers/blob/master/server/README.md#environment-variables

-- Harsh Manvar
Source: StackOverflow