psycopg2.OperationalError: FATAL: password authentication failed for user "username" in Minikube

psycopg2.OperationalError: FATAL: password authentication failed for user "username" in Minikube

我正在开发一个多容器 Flask 应用程序,它涉及一个 Web 容器(Flask 应用程序)、Postgres 容器(用于数据库服务)和一个 Redis 容器(用于缓存服务)。

Web 应用程序有 web_deployment.yamlweb_service.yaml 个文件。 Postgres 应用有 postgres_deployment.yamlpostgres_service.yaml 文件。 Redis 应用有 redis_deployment.yamlredis_service.yaml 个文件。

我的 web_deployment.yaml 文件如下所示:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-deployment
spec:
  replicas: 3
  template:
    metadata:
      labels:
        component: my-web-app
    spec:
      containers:
      - name: my-web-app-container
        image: web_app_image:latest
        ports:
        - containerPort: 80
        env:
          - name: REDIS_HOST
            value: redis-service
          - name: REDIS_PORT
            value: '6379'
          - name: POSTGRES_USER
            value: username
          - name: POSTGRES_HOST
            value: postgres-service
          - name: POSTGRES_PORT
            value: '5432'
          - name: POSTGRES_DB
            value: postgres_db
          - name: PGPASSWORD
            valueFrom:
              secretKeyRef:
                name: pgpassword
                key: PGPASSWORD
  selector:
    matchLabels:
      component: my-web-app

postgres_deployment.yaml 文件如下所示:

apiVersion: apps/v1
kind: Deployment 
metadata:
  name: postgres-deployment 
spec: 
  replicas: 1 
  selector:
    matchLabels:
      component: postgres 
  template:
    metadata:
      labels:
        component: postgres
    spec: 
      volumes: 
        - name: postgres-storage
          persistentVolumeClaim:
            claimName: database-persistent-volume-claim 
      containers:
        - name: postgres
          image: postgres:12-alpine
          ports:
            - containerPort: 5432
          volumeMounts: 
            - name: postgres-storage
              mountPath: /var/lib/postgresql/data 
              subPath: postgres
          env:
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: pgpassword
                  key: PGPASSWORD

在尝试为 Web 容器与 postgres 容器建立连接时,我遇到了以下问题:

  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 211, in raise_
    raise exception
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 599, in __connect
    connection = pool._invoke_creator(self)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/create.py", line 578, in connect
    return dialect.connect(*cargs, **cparams)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 584, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)

psycopg2.OperationalError: FATAL:  password authentication failed for user "username"

我修复成功了!

错误在于,我刚刚提到了 posgres_deployment.yaml 文件中的密码,但我还应该提到数据库名称和用户名,web_deployment.yaml 正试图访问该数据库服务。

现在新的postgres_deployment.yaml文件,修正后是这样的:

apiVersion: apps/v1
kind: Deployment 
metadata:
  name: postgres-deployment 
spec: 
  replicas: 1 
  selector:
    matchLabels:
      component: postgres 
  template:
    metadata:
      labels:
        component: postgres
    spec: 
      volumes: 
        - name: postgres-storage
          persistentVolumeClaim:
            claimName: database-persistent-volume-claim 
      containers:
        - name: postgres
          image: postgres:12-alpine
          ports:
            - containerPort: 5432
          volumeMounts: 
            - name: postgres-storage
              mountPath: /var/lib/postgresql/data 
              subPath: postgres
          env:
            - name: POSTGRES_USER
              value: username
            - name: POSTGRES_DB
              value: postgres_db
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: pgpassword
                  key: PGPASSWORD