User1984
User1984

Reputation: 787

Configure Postgresql in Kubernetes

I am configuring postgres data base and below the configuration I have created.I have pushed my docker postgres image to docker hub and I am using that container as image.In docker file I am using postgres host as db hence I have used same in my db yaml file. Already my application running in docker.Now we are moving to kubernates.How to copy exiting db data and move to kubernetes? Above configuration is correct way to deploy pg database?

I am getting below error when I try to execute in database pod

psql -U postgres -d devapp
psql: error: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  namespace: dev-app
  name: dev-database
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: dev-database
    spec:
      nodeSelector:
        cloud.io/sec-zone-green: "true"
      containers:
        - name: postgres
          image:  hub.docker.net/automation/dev.postgres.1.0:latest
          env:
            - name: POSTGRES_DB
              value: devapp
            - name: POSTGRES_USER
              value: postgres
            - name: PGDATA
              value: /var/lib/postgresql/data
            - name: POSTGRES_HOST_AUTH_METHOD
              value: trust

          volumeMounts:
            - mountPath: "/var/lib/postgresql/data"
              name: storage-vol
          resources:
            limits:
              memory: 5Gi
              cpu: 5
            requests:
              memory: 5Gi
              cpu: 5
          ports:
            - containerPort: 5432
      volumes:
        - name: storage-vol
          persistentVolumeClaim:
            claimName: dev-nfs

Service

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

development yaml file

development:
adapter: postgresql
encoding: unicode
database: devapp
username: postgres
password:
pool: 500
host: db

Upvotes: 0

Views: 1497

Answers (1)

Harshit
Harshit

Reputation: 1565

I would suggest to setup PostgresSQL in production as an external service and not in the Kubernetes cluster. Imagine in the worst case, your cluster goes down - your PostgresSQL PVC will also be gone forever and you'll not be able to retrieve back your data.

  1. Create PostgresSQL as an external service.

  2. Mention the DNS of external service on which Postgres is running.

external-service.yml

---
apiVersion: v1
kind: Service
metadata:
  name: my-postgres-db
spec:
  type: ExternalName
  externalName: my-postgres-db-dns.xyz.io
  ports:
    - port: 5432
      targetPort: 5432
  selector:
    app: my-postgres-db

Upvotes: 1

Related Questions