How to apply the imported realm configuration file of keycloak when deploying on k8s

Viewed 32

My file directory looks like below:

  • deployment.yaml
  • config.yaml
    • import
      • realm.json

This is the deployment.yaml file that I used based on the suggestion from Harsh Manvar:

apiVersion: v1
kind: Service
metadata:
  name: keycloak
  labels:
    app: keycloak
spec:
  selector:
    app: keycloak
  type: NodePort
  ports:
    - port: 8080
      targetPort: http
      protocol: TCP
      name: http
      nodePort: 32488
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: keycloak
  labels:
    app: keycloak
spec:
  replicas: 1
  selector:
    matchLabels:
      app: keycloak
  template:
    metadata:
      labels:
        app: keycloak
    spec:
      containers:
      - name: keycloak
        image: quay.io/keycloak/keycloak:17.0.1
        args:
        - "start-dev"
        - "--import-realm"
        env:
        - name: KEYCLOAK_ADMIN
          valueFrom:
            secretKeyRef:
              name: mysecret
              key: username
        - name: KEYCLOAK_ADMIN_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysecret
              key: password
        - name: KC_PROXY
          value: "edge"
        volumeMounts:
        - name: keycloak-volume
          mountPath: "/import/realm.json"
          name: "keycloak-volume"
          readOnly: true
          subPath: "realm.json"
        ports:
        - name: http
          containerPort: 8080
        readinessProbe:
          httpGet:
            path: /realms/master
            port: 8080
          initialDelaySeconds: 120
      volumes:
      - name: keycloak-volume
        configMap:
          name: keycloak-configmap

And my config.ymal looks like this (where the json_content is where I copy paste the content of the imported realm JSON file):

apiVersion: v1
data:
  realm.json: |
    {json_content}
kind: ConfigMap
metadata:
  name: keycloak-configmap

But when I accessed to the keycloak dash's web GUI, the imported realm did not show up.

1 Answers

try with once

- mountPath: "/import/realm.json"
            name: "keycloak-volume"
            readOnly: true
            subPath: "realm.json"

On older version(i think widelyfy onces) it was supported to import the keycloak realm using environment variables however it is stopped now : https://github.com/keycloak/keycloak/issues/10216

also, it's supported in version 18 you are using the 17

still with 17 you can give it try by passing an argument to the deployment config : official import doc

        args:
          - "start-dev"
          - "--import-realm"

also if you also check thread some are suggesting to use variable : KEYCLOAK_REALM_IMPORT

i also come across this blog which point legacy option to import the realm do check it out once: http://www.mastertheboss.com/keycloak/keycloak-with-docker/

Related