reactive-tech / kubegres

Kubegres is a Kubernetes operator allowing to deploy one or many clusters of PostgreSql instances and manage databases replication, failover and backup.
https://www.kubegres.io
Apache License 2.0
1.32k stars 74 forks source link

Exposing kubegres as NodePort #44

Closed zenmaster24 closed 3 years ago

zenmaster24 commented 3 years ago

Hi,

I am trying to expose kubegres to traffic external to the cluster by following the guidance provided here and have created a service before creating the resource postgres of kind kubegres.

I have set the service to be the below:

apiVersion: 1
kind: Service
metadata:
  name: postgres
  namespace: kubegres
spec:
  type: NodePort
  selector:
    app: postgres
    replicationRole: primary
  ports:
  - port: 5432
    targetPort: 5432
    nodePort: 30432
    protocol: TCP

I can't seem to connect to the pod via <NODEIP>:30432 however. I can see in the pod logs of the primary that the database system is ready to accept connections. If I use kubectl port forwarding I can connect an SQL client and run queries against it like create db and see some relevant output when tailing the pod logs, so I know its up and responding.

Am I missing something? How do I expose this db to external traffic?

NB: If it matters, I have replicas set to 1 for testing

alex-arica commented 3 years ago

Thank you for your message.

That feature was not implemented. The person who raised the issue didn't reply that our recommendation. And consequently we didn't implement it.

sshekghub commented 3 years ago

Hi,

I am trying to expose kubegres to traffic external to the cluster by following the guidance provided here and have created a service before creating the resource postgres of kind kubegres.

I have set the service to be the below:

apiVersion: 1
kind: Service
metadata:
  name: postgres
  namespace: kubegres
spec:
  type: NodePort
  selector:
    app: postgres
    replicationRole: primary
  ports:
  - port: 5432
    targetPort: 5432
    nodePort: 30432
    protocol: TCP

I can't seem to connect to the pod via <NODEIP>:30432 however. I can see in the pod logs of the primary that the database system is ready to accept connections. If I use kubectl port forwarding I can connect an SQL client and run queries against it like create db and see some relevant output when tailing the pod logs, so I know its up and responding.

Am I missing something? How do I expose this db to external traffic?

NB: If it matters, I have replicas set to 1 for testing

I was able to make it work like this:-

apiVersion: v1 kind: Service metadata: labels: app: mydb replicationRole: primary name: mydb-np namespace: mynamespace ownerReferences:

kubectl edit svc primary-service-name --> note down the UID inside "ownerReferences" and use that for your node port service.

You can do the same thing for replica services.

alex-arica commented 3 years ago

Thank you for those details, it would be helpful for others. If you are happy, I am closing this ticket.

peterbecich commented 1 week ago

This worked for me without the need to set uid: https://github.com/reactive-tech/kubegres/issues/44#issuecomment-925567428

Determine the app label of the Kubegres service:

kubectl describe service ...
Labels:                   app=mypostgres
                          replicationRole=primary

Define a NodePort Service and use that app label as the selector:

apiVersion: v1
kind: Service
metadata:
  name: postgres-node-port
spec:
  type: NodePort
  selector:
    app: mypostgres
    replicationRole: primary
  ports:
  - port: 5432
    targetPort: 5432
    nodePort: 30432
    protocol: TCP