簡體   English   中英

無法在 k8s 中與 Postgres 對話 Django

[英]Couldn't Make Django Talk with Postgres in k8s

我在 minikube 中部署了一個簡單的 Django 應用程序。 它有兩個容器,一個用於 Django 應用程序,一個用於 postgres 數據庫。 它與 docker-compose 一起工作,但無法在 minikube k8s 集群中工作。 當我打開容器的終端並 ping 服務時,它沒有成功。 我沒有找到導致此通信錯誤的原因。

這是一個基本的應用程序,只有一個用於登錄或注冊的登錄頁面。 登錄后,您可以創建一些筆記。 部署到 minikube 后,我可以訪問 127.0.0.1:8000,但是當我輸入信息注冊時,出現以下錯誤。 顯然,它無法將數據存儲在數據庫中。

django項目settings.py中的DATABASES部分:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'postgres',
    'USER': 'postgres',
    'PASSWORD': 'postgres',
    'HOST': 'db',
    'PORT': '5432',
}
}

Dockerfile 用於構建應用程序的圖像:

FROM python:2.7

WORKDIR /notejam

COPY ./ ./

RUN pip install -r requirements.txt

EXPOSE 8000

CMD python manage.py runserver 0.0.0.0:8000

部署文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: notejam-deployment
  labels: 
    app: notejam-app
spec:
  selector:
    matchLabels:
      app: notejam-app
  template:
    metadata:
      labels:
        app: notejam-app
    spec:
      volumes:
        - name: postgres-pvc
          persistentVolumeClaim:
            claimName: postgres-pvc
      containers:
      - name: notejam
        image: notejam_k8s
        imagePullPolicy: Never
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 8000
      - name: postgres-db
        image: postgres
        imagePullPolicy: Never
        ports:
        - containerPort: 5432
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        env:
        - name: POSTGRES_USERNAME
          valueFrom:
            configMapKeyRef:
              name: postgres-configmap
              key: postgres-username
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: postgres-secret
              key: postgres-password
        - name: POSTGRES_DB
          valueFrom:
            configMapKeyRef:
              name: postgres-configmap
              key: postgres-db
        volumeMounts:
          - mountPath: /notejam-db
            name: postgres-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: db
spec:
  selector:
    app: notejam-app
  ports:
  - port: 5432
    targetPort: 5432
---
apiVersion: v1
kind: Service
metadata:
  name: notejam-external-service
spec:
  selector:
    app: notejam-app
  type: LoadBalancer
  ports:
  - protocol: TCP 
    port: 8000
    targetPort: 8000

錯誤:

OperationalError at /signup/

could not connect to server: Connection timed out
    Is the server running on host "db" (10.96.150.207) and accepting
    TCP/IP connections on port 5432?

Request Method:     POST
Request URL:    http://127.0.0.1:8000/signup/
Django Version:     1.6.5
Exception Type:     OperationalError
Exception Value:    

could not connect to server: Connection timed out
    Is the server running on host "db" (10.96.150.207) and accepting
    TCP/IP connections on port 5432?

Exception Location:     /usr/local/lib/python2.7/site-packages/psycopg2/__init__.py in connect, line 127
Python Executable:  /usr/local/bin/python
Python Version:     2.7.18
Python Path:    

['/notejam',
'/usr/local/lib/python27.zip',
'/usr/local/lib/python2.7',
'/usr/local/lib/python2.7/plat-linux2',
'/usr/local/lib/python2.7/lib-tk',
'/usr/local/lib/python2.7/lib-old',
'/usr/local/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/site-packages']

Server time:    Thu, 26 Aug 2021 00:40:58 +0300

您正在嘗試將 postgres 作為同一容器中的輔助容器運行。 應該是自己的部署和服務。 多容器 pod 不像 docker compose :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM