java.lang.RuntimeException:无法连接到数据库

I have installed https://github.com/zalando/postgres-operator on my K8S cluster have created a database as follows:

kind: "postgresql"
apiVersion: "acid.zalan.do/v1"

metadata:
  name: "acid-databaker-db"
  namespace: "dev"
  labels:
    team: acid

spec:
  teamId: "acid"
  postgresql:
    version: "12"
  numberOfInstances: 2
  volume:
    size: "5Gi"
  users:
    admin: []
  databases:
    keycloak: admin
  allowedSourceRanges:
    # IP ranges to access your cluster go here

  resources:
    requests:
      cpu: 100m
      memory: 100Mi
    limits:
      cpu: 500m
      memory: 500Mi 

数据库实例已启动并正在运行:

NAME                       TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
acid-databaker-db          ClusterIP   10.245.205.143   <none>        5432/TCP   10h
acid-databaker-db-config   ClusterIP   None             <none>        <none>     10h
acid-databaker-db-repl     ClusterIP   10.245.152.182   <none>        5432/TCP   10h 

然后,我启动了Keycloak,它正在使用我在上面创建的数据库:

# Source: keycloak/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: keycloak
  namespace: dev
  labels:
    app: keycloak
spec:
  ports:
  - name: http
    port: 80
    targetPort: 8080
  selector:
    app: keycloak
---
# Source: keycloak/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: keycloak
  namespace: dev
  labels:
    app: keycloak
spec:
  replicas: 1
  selector:
    matchLabels:
      app: keycloak
  template:
    metadata:
      labels:
        app: keycloak
    spec:
      imagePullSecrets:
        - name: regcred
      containers:
      - name: keycloak
        image: "hub.svc.databaker.io/service/keycloak:0.1.8"
        imagePullPolicy: "IfNotPresent"
        args:
         - "-Dkeycloak.migration.action=import"
         - "-Dkeycloak.migration.provider=dir"
         - "-Dkeycloak.profile.feature.upload_scripts=enabled"
         - "-Dkeycloak.migration.dir=/opt/jboss/keycloak/import-dir"
         - "-Dkeycloak.migration.strategy=OVERWRITE_EXISTING"
        env:
        - name: DB_VENDOR
          value: "postgres"
        - name: DB_ADDR
          value: "acid-databaker-db"
        - name: DB_DATABASE
          value: "keycloak"
        - name: DB_USER
          value: "admin"
        - name: DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: postgres.acid-databaker-db.credentials.postgresql.acid.zalan.do
              key: password                    
        - name: KEYCLOAK_USER
          valueFrom:
            secretKeyRef:
              name: keycloak
              key: kc_user
        - name: KEYCLOAK_PASSWORD
          valueFrom:
            secretKeyRef:
              name: keycloak
              key: kc_pw
        - name: PROXY_ADDRESS_FORWARDING
          value: "true"
        ports:
        - name: http
          containerPort: 8080
        - name: https
          containerPort: 8443
        readinessProbe:
          httpGet:
            path: /auth/realms/master
            port: 8080

查看Keycloak日志,它说:

08:18:28,124 FATAL [org.keycloak.services] (ServerService Thread Pool -- 61) java.lang.RuntimeException: Failed to connect to database

为什么Keycloak找不到数据库? URL错误吗?

评论
bipsam
bipsam

The port 5432 need to be supplied as a parameter via POSTGRES_PORT environment variable.

点赞
评论