如何在 Kubernetes 中将 PostgresSQL pod 连接到 Ruby on Rails pod

发布于 2025-01-16 09:41:49 字数 2683 浏览 3 评论 0原文

我总共有 3 个 Pod。

  1. Ruby on Rails APP-1
  2. Ruby on Rails APP-2
  3. PostgreSQL 数据库

APP-1 和 postgreSQL 数据库位于同一命名空间但不同节点 APP-2 位于其自己的命名空间中,并且与 APP-1 位于同一节点上,

我总共有 3 个节点。 我希望我的 Ruby on Rails 应用程序连接到 PostgreSQL 数据库。

但目前失败并出现错误 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 输出:

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"

编辑: kubectl 获取服务 kube-dns -n kube-system 输出:

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

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   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

所谓喜欢 2025-01-23 09:41:50

尝试:

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

Try:

development:
  ...
  host: myteam-demodb.jx-staging.svc.cluster.local
  ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文