How to connect PostgresSQL pod to Ruby on Rails pod in Kubernetes

260 Views Asked by At

I have 3 pods in total.

  1. Ruby on Rails APP-1
  2. Ruby on Rails APP-2
  3. PostgreSQL database

APP-1 and the postgreSQL database in the same namespace but different nodes APP-2 is in its own namespace and is on the same node as APP-1

I have 3 nodes in total. I want my Ruby on Rails app(s) to connect to the PostgreSQL database.

But currently this is failing with the error msg:

ActiveRecord::ConnectionNotEstablished (could not translate host name "myteam-demodb-0" to address: Name or service not known
):

database.yml:

development:
  adapter: postgresql
  encoding: utf8
  database: demodb
  username: myowner
  password: <%= ENV["db_secret"] %>
  host: myteam-demodb
  port: 5432

This is my kubectl get services output -

Namespace 1:

NAME                     TYPE       CLUSTER-IP     EXTERNAL-IP       PORT(S) 
APP-1                  ClusterIP   172.20.19.234    <none>          80/TCP    126m
myteam-demodb          ClusterIP   172.20.147.0     <none>        5432/TCP   126m

Namespace 2:

> NAME     TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S) 
> APP-2   ClusterIP   172.20.234.194        <none>    80/TCP    94m

kubectl describe service myteam-demodb output:

Name:              myteam-demodb
Namespace:         jx-staging
Labels:            application=spilo
                   cluster-name=myteam-demodb
                   spilo-role=master
                   team=myteam
Annotations:       <none>
Selector:          <none>
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                172.20.147.0
IPs:               172.20.147.0
Port:              postgresql  5432/TCP
TargetPort:        5432/TCP
Endpoints:         10.0.3.194:5432
Session Affinity:  None
Events:            <none>

postgreSQL.yaml:

apiVersion: "acid.zalan.do/v1"
kind: postgresql
metadata:
  name: "myteam-demodb"
  annotations:
    meta.helm.sh/release-name: 'election-web-app'
  namespace: jx-staging
  labels:
    gitops.jenkins-x.io/pipeline: 'namespaces'
spec:
  teamId: "myteam"
  volume:
    size: 1Gi
  numberOfInstances: 2
  users:
    myowner: # database owner
      - superuser
      - createdb
    foo_user: [] # role for application foo
  databases:
    demodb: myowner # dbname: owner
  preparedDatabases:
    bar: {}
  postgresql:
    version: "13"

Edit: kubectl get service kube-dns -n kube-system output:

NAME     TYPE      CLUSTER-IP    EXTERNAL-IP   PORT(S)        
kube-dns ClusterIP 172.20.0.10   <none>    53/UDP,53/TCP   
1

There are 1 best solutions below

0
On

Try:

development:
  ...
  host: myteam-demodb.jx-staging.svc.cluster.local
  ...