How to test if container is running postgres

3/5/2019

I just deployed a docker with Postgres on it on AWS EKS.

Below is the descriptionenter image description here details.

How do i access or test if postgres is working. I tried accessing both IP with post within VPC from worker node.

 psql -h #IP -U #defaultuser -p 55432

Below is the deployment.

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  template:
    metadata:
      labels:
    app: postgres
spec:
  containers:
    - name: postgres
      image: postgres:10.4
      imagePullPolicy: "IfNotPresent"
      ports:
        - containerPort: 55432
      # envFrom:
      #   - configMapRef:
      #       name: postgres-config
      volumeMounts:
        - mountPath: /var/lib/postgresql/data
          name: postgredb
  volumes:
    - name: postgredb
      persistentVolumeClaim:
        claimName: efs

Surprisingly I am able to connect to psql but on 5432. :( Not sure what I am doing wrong. I passed containerPort as 55432

-- Pat
amazon-eks
aws-eks
kubernetes

1 Answer

3/6/2019

In short, you need to run the following command to expose your database on 55432 port.

kubectl expose deployment postgres --port=55432 --target-port=5432 --name internal-postgresql-svc

From now on, you can connect to it via port 55432 from inside your cluster by using the service name as a hostname, or via its ClusterIP address:

kubectl get internal-postgresql-svc

What you did in your deployment manifest file, you just attached additional information about the network connections a container uses, between misleadingly, because your container exposes 5432 port only (you can verify it by your self here).

You should use a Kubernetes Service - abstraction which enables access to your PODs, and does the necessary port mapping behind the scene.

Please check also different port Types, if you want to expose your postgresql database outside of the Kubernetes cluster.


To test if progress is running fine inside POD`s container:

kubectl run postgresql-postgresql-client --rm --tty -i --restart='Never' --namespace default --image bitnami/postgresql --env="PGPASSWORD=<HERE_YOUR_PASSWORD>" --command -- psql --host <HERE_HOSTNAME=SVC_OR_IP> -U <HERE_USERNAME> 
-- Nepomucen
Source: StackOverflow