官方文档:Kubernetes - Keycloak

部署MySQL

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: data-keycloak-mysql
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: "longhorn"
  resources:
    requests:
      storage: 50Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  replicas: 1
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql:8.0
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: keycloak
          volumeMounts:
            - name: data
              mountPath: /var/lib/mysql
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: data-keycloak-mysql
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  selector:
    app: mysql
  type: NodePort
  ports:
  - port: 3306
    targetPort: 3306
    nodePort: 30036
连接数据库

创建数据库keycloak,使用utf8mb4字符集

部署KeyCloak

apiVersion: v1
kind: Service
metadata:
  name: keycloak
  labels:
    app: keycloak
spec:
  ports:
    - name: http
      port: 8080
      targetPort: 8080
      nodePort: 30080
  selector:
    app: keycloak
  type: NodePort
---
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:25.0.2
          args: ["start-dev"]
          env:
            - name: KC_DB
              value: "mysql"
            - name: KC_DB_USERNAME
              value: "root"
            - name: KC_DB_PASSWORD
              value: "keycloak"
            - name: KC_DB_URL
              value: "jdbc:mysql://mysql:3306/keycloak?characterEncoding=UTF-8"
            - name: KC_HEALTH_ENABLED
              value: "true"
            - name: KEYCLOAK_ENABLE_STATISTICS
              value: "true"
            - name: KEYCLOAK_HOSTNAME
              value: kc.sreok.cn
            - name: KEYCLOAK_ADMIN
              value: "admin"
            - name: KEYCLOAK_ADMIN_PASSWORD
              value: "admin"
          ports:
            - name: http
              containerPort: 8080
          readinessProbe:
            httpGet:
              path: /realms/master
              port: 8080

参考文档:

Running Keycloak in a container - Keycloak

Configuring Keycloak for production - Keycloak