<p>I am working on a Multi-Container Flask App, which involves a Web container(Flask app), Postgres container(for DB services), and a Redis container(for Caching services).</p>
<p>Web app has <code>web_deployment.yaml</code> and <code>web_service.yaml</code> files.
Postgres app has <code>postgres_deployment.yaml</code> and <code>postgres_service.yaml</code> files.
Redis app has <code>redis_deployment.yaml</code> and <code>redis_service.yaml</code> files.</p>
<p>My <code>web_deployment.yaml</code> file looks like this:</p>
<pre><code>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
</code></pre>
<p>The <code>postgres_deployment.yaml</code>  file looks like this:</p>
<pre><code>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
</code></pre>
<p>While trying to establish connection for web container with the postgres container, I got the following issue:</p>
<pre><code>  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py&quot;, line 211, in raise_
    raise exception
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/base.py&quot;, line 599, in __connect
    connection = pool._invoke_creator(self)
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/create.py&quot;, line 578, in connect
    return dialect.connect(*cargs, **cparams)
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/default.py&quot;, line 584, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File &quot;/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py&quot;, line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)

psycopg2.OperationalError: FATAL:  password authentication failed for user &quot;username&quot;
</code></pre>

vagdevi k
<p>I successfully fixed it!</p>
<p>The mistake was that, I just mentioned the password in the <code>posgres_deployment.yaml</code> file, but I should also mention the database name and the username, using which the <code>web_deployment.yaml</code> is trying to access this db service.</p>
<p>Now the new <code>postgres_deployment.yaml</code> file, after the correction, looks like this:</p>
<pre><code>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
</code></pre>
