Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> SQL

ปรับใช้ PostgreSQL บน Kubernetes: คู่มือทีละขั้น สำหรับฐานข้อมูลที่น่าเชื่อถือและขยายตัวได้

PostgreSQL เป็น RDBMS โอเพนซอร์สที่รู้จักในการจัดการชุดข้อมูลที่ซับซ้อน Kubernetes ทำให้การปรับใช้, ขยายตัว และจัดการแอปพลิเคชันในคอนเทนเนอร์เป็นอัตโนมัติ การปรับใช้ PostgreSQL บน Kubernetes ให้การขยายตัวอัตโนมัติ Rolling Updates และความน่าเชื่อถือที่ดีขึ้นผ่าน Replicas และ Failover

ขั้นตอนการปรับใช้ (Deployment Steps)

Cluster → Image → Manifest → Deploy → Monitor

ขั้นตอนที่ 1: ตั้งค่า Kubernetes Cluster

ใช้ Cloud Provider (AWS EKS, GCP GKE, Azure AKS) หรือตั้งค่าท้องถิ่นกับ Minikube ติดตั้ง kubectl และ Helm (ถ้าต้องการ)

ขั้นตอนที่ 2: สร้าง Deployment Manifest

กำหนดการปรับใช้ PostgreSQL ในไฟล์ YAML พร้อม Environment Variables, Volumes และ Ports

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres:16
        ports:
        - containerPort: 5432
        env:
        - name: POSTGRES_USER
          value: "admin"
        - name: POSTGRES_PASSWORD
          value: "secretpass"
        - name: POSTGRES_DB
          value: "mydb"
        volumeMounts:
        - mountPath: /var/lib/postgresql/data
          name: postgres-storage
      volumes:
      - name: postgres-storage
        persistentVolumeClaim:
          claimName: postgres-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: postgres-svc
spec:
  selector:
    app: postgres
  ports:
  - port: 5432
    targetPort: 5432
  type: ClusterIP

ขั้นตอนที่ 3: ปรับใช้และตรวจสอบ (Deploy and Monitor)

# Deploy
kubectl apply -f postgres-manifest.yaml
# Check pod status
kubectl get pods
# View logs
kubectl logs 
# Monitor resources
kubectl top pods

ขั้นตอนที่ 4: ขยายตัวและสำรองข้อมูล (Scale and Backup)

# Scale replicas
kubectl scale deployment postgres --replicas=3
# Backup using pg_dump
kubectl exec  -- pg_dump -U admin mydb > backup.sql

สรุป

การปรับใช้ PostgreSQL บน Kubernetes รวมการจัดการฐานข้อมูลที่ทนทานกับประโยชน์การออร์เคสเตรตคอนเทนเนอร์: การขยายตัวอัตโนมัติ Rolling Updates, Persistent Storage และ Failover กำหนดการปรับใช้ใน YAML Manifest, ปรับใช้ด้วย kubectl apply และตรวจสอบด้วยคำสั่ง Kubernetes มาตรฐาน

ปรับใช้ PostgreSQL บน Kubernetes: คู่มือทีละขั้น สำหรับฐานข้อมูลที่น่าเชื่อถือและขยายตัวได้