Error in setting up PostgreSQL in Kubernates

11/21/2019

I'm trying to setup postgres in my Kubernates enabled Docker Desktop. I pulled images 'postgres:latest' and 'dpage/pgadmin4:latest' from Docker Hub. Below are my yaml files. Everything starts perfectly. I can open the pgAdmin page from Chrome. The creds i have given in the yaml file are not logging me in. What is causing this issue?

postgres Db:-

apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-config
  labels:
    app: postgres
data:
  POSTGRES_DB: app
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: password
---
kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-pv-volume
  labels:
    type: local
    app: postgres
spec:
  storageClassName: manual
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/mnt/data"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pv-claim
  labels:
    app: postgres
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi


---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres:latest
        imagePullPolicy: Never
        ports:
        - containerPort: 5432
        envFrom:
            - configMapRef:
                name: postgres-config
        volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: app
      volumes:
        - name: app
          persistentVolumeClaim:
            claimName: postgres-pv-claim
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  type: NodePort
  ports:
   - port: 5432
  selector:
   app: postgres

pgAdmin :-

apiVersion: v1
kind: ConfigMap
metadata:
  name: pgadmin-config
  labels:
    app: pgadmin4
data:
  PGADMIN_DEFAULT_EMAIL: ap@a.com
  PGADMIN_DEFAULT_PASSWORD: posdt

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pgadmin4
  labels:
    app: pgadmin4
spec:
  replicas: 1
  selector:
    matchLabels:
      app: pgadmin4
  template:
    metadata:
      labels:
        app: pgadmin4
    spec:
      containers:
      - name: pgadmin4
        image: dpage/pgadmin4:latest
        imagePullPolicy: Never
        ports:
        - containerPort: 80
        envFrom:
            - configMapRef:
                name: pgadmin-config
---
apiVersion: v1
kind: Service
metadata:
  name: pgadmin4
  labels:
    app: pgadmin4
spec:
  type: NodePort
  ports:
   - port: 80
  selector:
   app: pgadmin4
-- Arunava Paul
docker
kubernetes
postgresql

1 Answer

11/21/2019

I just deployed and was able to connect the database. see below

postgres-7cb54957dd-qkfd4   1/1     Running             0          13s
# kubectl exec -it postgres-7cb54957dd-qkfd4 sh
# psql -h 127.0.0.1 -U postgres -q -d app -c 'SELECT 1'
 ?column?
----------
        1
(1 row)

used below YAML with slight modifications

apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-config
  labels:
    app: postgres
data:
  POSTGRES_DB: app
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: password
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 5432
        envFrom:
            - configMapRef:
                name: postgres-config
        volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: app
      volumes:
        - name: app
          emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  type: NodePort
  ports:
   - port: 5432
  selector:
   app: postgres
-- P Ekambaram
Source: StackOverflow