You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Kubernetes部署Django应用PostgreSQL数据库的困惑求助

Fixing Your PostgreSQL Deployment for Django on Kubernetes

Hey there, let's work through your PostgreSQL Deployment setup for your Django app on Kubernetes. First, I noticed your YAML cuts off mid-secret reference, and there are a few critical missing pieces that will keep your Postgres instance from being functional (or persistent) long-term. Let's break this down with complete, working configurations and explanations.

First: Fix the Outdated API Version

Your current Deployment uses apps/v1beta1, which has been deprecated for years. Modern Kubernetes clusters require apps/v1 (supported since Kubernetes 1.9+), so we'll update that first.

Step 1: Create a Secret for Sensitive Credentials

Never hardcode usernames, passwords, or database names in your Deployment YAML. Instead, use a Kubernetes Secret to store these values:

kubectl create secret generic postgres-secrets \
  --from-literal=postgres-user=django_db_user \
  --from-literal=postgres-password=your_strong_unique_password \
  --from-literal=postgres-db=django_default_db

Replace the placeholder values with your actual Django database credentials.

Step 2: Complete Deployment YAML with Persistence

Postgres needs persistent storage to avoid losing data when the Pod restarts. Here's the full, corrected Deployment definition (deployment-definition.yml):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  labels:
    app: postgres-container
    tier: backend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres-container
      tier: backend
  template:
    metadata:
      labels:
        app: postgres-container
        tier: backend
    spec:
      containers:
      - name: postgres-container
        image: postgres:9.6.6
        ports:
        - containerPort: 5432
        env:
        - name: POSTGRES_USER
          valueFrom:
            secretKeyRef:
              name: postgres-secrets
              key: postgres-user
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: postgres-secrets
              key: postgres-password
        - name: POSTGRES_DB
          valueFrom:
            secretKeyRef:
              name: postgres-secrets
              key: postgres-db
        volumeMounts:
        - name: postgres-storage
          mountPath: /var/lib/postgresql/data
      volumes:
      - name: postgres-storage
        persistentVolumeClaim:
          claimName: postgres-pvc

Key improvements here:

  • Updated to the supported apps/v1 API version
  • Added explicit port mapping for Postgres' default 5432 port
  • Completed the Secret references to pull credentials from the postgres-secrets we created
  • Added volume mounts and a PersistentVolumeClaim (PVC) to persist database data

Step 3: Create the PersistentVolumeClaim

You need a PVC to request storage from your Kubernetes cluster. Save this as postgres-pvc.yml:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

Adjust the storage value based on your app's needs. Cloud Kubernetes clusters (like EKS, GKE) will automatically provision a PersistentVolume (PV) to match this claim; for local clusters, you may need to pre-configure a PV.

Step 4: Create a Service for Django to Access Postgres

Pods have dynamic IPs, so you need a Kubernetes Service to give Postgres a stable network address. Save this as postgres-service.yml:

apiVersion: v1
kind: Service
metadata:
  name: postgres-service
spec:
  selector:
    app: postgres-container
    tier: backend
  ports:
    - protocol: TCP
      port: 5432
      targetPort: 5432

In your Django settings.py, set the database host to postgres-service (the Service name is DNS-resolvable within the cluster):

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'django_default_db',
        'USER': 'django_db_user',
        'PASSWORD': 'your_strong_unique_password',
        'HOST': 'postgres-service',
        'PORT': '5432',
    }
}

Bonus: Production-Grade Notes

  • Upgrade Postgres Version: 9.6.6 is quite old (end-of-life since 2021). Consider upgrading to a supported stable version like postgres:15 for better security and performance.
  • Use StatefulSet for High Availability: If you need a highly available Postgres setup, replace the Deployment with a StatefulSet. StatefulSets are designed for stateful applications like databases, providing stable network identities and persistent storage per Pod.
  • Namespace Isolation: If you're running multiple apps in your cluster, deploy Postgres and Django into the same namespace to keep resources organized.

内容的提问来源于stack exchange,提问作者Stefan Radonjic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:32:58