Access my Postgresql database inside a Kubernates pod from my computer with pgAdmin

10/18/2020

I installed a Postgresql database with Kubernetes into one of my pod. The communication works fine beetwen application in the other pods and my database and I can access it with those commands:

kubectl exec -it name_of_my_pod -n name_of_my_namespace -- "/bin/bash"
psql

I would like now to access the database with IntelliJ, pgAdmin or something like that. How can I do this?

Secondary question: Is it really recommended to do it this way (postgres into a pod)? Or is better to have the database out of Kubernetes?

-- Antoine
kubernetes
postgresql

3 Answers

11/13/2020

i guess you should have service object to make it accessable via ingress or nodePort.

For secound question: answer is not so clear and it depends on what you want achive.

As Ben said above, you have to make sure that you have peristent volume on which you can trust. As i know amazon could deliver some solutions but i dont konow if you want to store youre data into third party companies.

-- grzesiek16016
Source: StackOverflow

1/29/2021

I have a similar kind of setup. PostgreSQL db pod is deployed in kubernetes cluster running in AWS cloud. And my pgAdmin is running in docker container in my local machine. Following are the steps that I performed to access this remote database from pgAdmin running in my local machine (Followed this video).

1 -> Modify the Service type of db from ClusterIP to NodePort as shown below.

apiVersion: v1
kind: Service
metadata:
  name: db

spec:
  selector:
    app: db
  ports:
    - name: dbport
      port: 5432
      nodePort: 31000
  type: NodePort

2-> Add new rule to security group of any node(EC2 Instance) of your Kubernetes cluster as shown below.

enter image description here

3-> Connect to this remote database using the public IPv4 address of the node(EC2 Instance).

HostAddress: IPv4 address of the node ec2 instance.

Port: 31000

-- HarishVijayamohan
Source: StackOverflow

10/18/2020

To access the database, you can expose the pgadmin tool via a Load-Balancer or Ingress controller. This is not always ideal from a security standpoint- as you are trusting the pgadmin tool to properly handle security. Another way to do this is to expose the pgadmin tool via cluster IP - or an internal IP address. This means, that those people outside of your clusters network, cannot access it. Then for you to access the tool, you would have to add a ssh/vpn tunnel or use kubernetes port-forwarding tool.

For deployment, this helm chart seems to be fairly reasonable, and you can control how you expose the admin utility.

Personally, if security is not of the upmost importance, I would expose pgadmin via an ingress controller. This would be the most convenient way to access the tool.

As for your second question, you can run postgress inside of a pod - but you need to make sure you have a persistent volume setup as the datastore location - as pods should be considered ephemeral. If you are struggling with how to start, I usually find Bitnami's offerings (free) a really good place to begin. Here is the link to the helm chart for postgress SQL. With Bitnami's charts, make sure to set the passwords and accounts during the deployment process- otherwise the deployment will work the first time, but not if you have to redeploy - as the randomly generated passwords will change.

As a note, with some helm charts I use the --dry-run flag to get the raw yaml which I use to do a more standard kubernetes deployment. Make sure to set your helm options properly, and then add that flag. This can be easier if you are just starting and are trying to customize something that does not have a built-in argument.

-- Ben W
Source: StackOverflow