9951 explained code solutions for 126 technologies


postgresqlHow to deploy a PostgreSQL application using Kubernetes?


  1. Create a Kubernetes cluster with the desired number of nodes.
  2. Install the PostgreSQL application on the cluster nodes.
  3. Create a ConfigMap for the PostgreSQL configuration:
    apiVersion: v1
    kind: ConfigMap
    metadata:
     name: postgres-config
    data:
     POSTGRES_DB: mydb
     POSTGRES_USER: myuser
     POSTGRES_PASSWORD: mypassword
  4. Create a Persistent Volume for the PostgreSQL data:
    apiVersion: v1
    kind: PersistentVolume
    metadata:
     name: postgres-pv
    spec:
     capacity:
       storage: 10Gi
     accessModes:
       - ReadWriteOnce
     hostPath:
       path: "/mnt/data"
  5. Create a deployment for the PostgreSQL application:
    apiVersion: apps/v1
    kind: Deployment
    metadata:
     name: postgres-deployment
    spec:
     replicas: 1
     selector:
       matchLabels:
         app: postgres
     template:
       metadata:
         labels:
           app: postgres
       spec:
         containers:
         - name: postgres
           image: postgres
           envFrom:
           - configMapRef:
               name: postgres-config
           volumeMounts:
           - mountPath: /var/lib/postgresql/data
             name: postgres-pv
         volumes:
         - name: postgres-pv
           persistentVolumeClaim:
             claimName: postgres-pv
  6. Create a service for the PostgreSQL application:
    apiVersion: v1
    kind: Service
    metadata:
     name: postgres-service
    spec:
     selector:
       app: postgres
     ports:
     - protocol: TCP
       port: 5432
       targetPort: 5432
  7. Apply the configuration to the cluster:
    kubectl apply -f postgres-config.yaml
    kubectl apply -f postgres-pv.yaml
    kubectl apply -f postgres-deployment.yaml
    kubectl apply -f postgres-service.yaml

Helpful links

Edit this code on GitHub