santosh
santosh

Reputation: 123

access postgres in kubernetes from an application outside the cluster

Am trying to access postgres db deployed in kubernetes(kubeadm) on centos vms from another application running on another centos vm. I have deployed postgres service as 'NodePort' type. My understanding is we can deploy it as LoadBalancer type only on cloud providers like AWS/Azure and not on baremetal vm. So now am trying to configure 'ingress' with NodePort type service. But am still unable to access my db other than using kubectl exec $Pod-Name on kubernetes master.

My ingress.yaml is

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: postgres-ingress
spec:
  backend:
    serviceName: postgres
    servicePort: 5432

which does not show up any address as below

NAME                    HOSTS   ADDRESS    PORTS   AGE
postgres-ingress        *                  80      4m19s

am not even able to access it from pgadmin on my local mac. Am I missing something?

Any help is highly appreciated.

Upvotes: 12

Views: 20750

Answers (3)

Mircea Sirghi
Mircea Sirghi

Reputation: 616

Just change postgres service type to LoadBalancer, add port 5432 to ingress, and open the tunnel, for minikube it would be:

minikube tunnel

Upvotes: 0

klaudiusnaban
klaudiusnaban

Reputation: 306

for me, it worked by adding nodePort type service

apiVersion: v1
kind: Service
metadata:
  name: postgresql-external
  labels:
    version: 1.0.0
spec:
  selector:
    sfs: postgresql-sfs
  ports:
    - name: postgresql-external
      port: 5432
      targetPort: 5432
      nodePort: 30030
  type: NodePort

enter image description here

don't forget to change the selector. Mine was targeted to the postgres' statefulset.

Upvotes: 3

Dirbaio
Dirbaio

Reputation: 3142

Ingress won't work, it's only designed for HTTP traffic, and the Postgres protocol is not HTTP. You want solutions that deal with just raw TCP traffic:

  • A NodePort service alone should be enough. It's probably the simplest solution. Find out the port by doing kubectl describe on the service, and then connect your Postgres client to the IP of the node VM (not the pod or service) on that port.
  • You can use port-forwarding: kubectl port-forward pod/your-postgres-pod 5432:5432, and then connect your Postgres client to localhost:5432. This is my preferred way for accessing the database from your local machine (it's very handy and secure) but I wouldn't use it for production workloads (kubectl must be always running so it's somewhat fragile and you don't get the best performance).
  • If you do special networking configuration, it is possible to directly access the service or pod IPs from outside the cluster. You have to route traffic for the pod and service CIDR ranges to the k8s nodes, this will probably involve configuring your VM hypervisors, routers and firewalls, and is highly dependent on what networking (CNI) plugin are you using for your Kubernetes cluster.

Upvotes: 25

Related Questions