如何在 Kubernetes 中将 PostgresSQL pod 连接到 Ruby on Rails pod
我总共有 3 个 Pod。
- Ruby on Rails APP-1
- Ruby on Rails APP-2
- 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.
- Ruby on Rails APP-1
- Ruby on Rails APP-2
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试:
Try: