英文:
Kubernetes how to access application in one namespace from another
问题
我在一个 Kubernetes 集群中运行了以下组件:
- 一个 GoLang 应用程序,将数据写入到命名空间为
app1
的 mongodb statefulset replicaset 中。 - 一个运行在
ng-mongo
命名空间中的 mongodb replicaset(1 个副本)。
我需要做的是,让 golang
应用程序能够通过访问 mongodb 数据库进行读写操作,所以我做了以下操作:
- 在
ng-mongo
命名空间中为mongodb
创建了一个无头服务,内容如下:
# Source: mongo/templates/svc.yaml
apiVersion: v1
kind: Service
metadata:
name: mongo
namespace: ng-mongo
labels:
app: mongo
spec:
ports:
- port: 27017
targetPort: 27017
name: mongo
clusterIP: None
selector:
role: mongo
- 然后,我部署了
mongodb
statefulset 并初始化了 replicaset,内容如下:
kubectl exec -it mongo-0 -n ng-mongo mongosh
rs.initiate({_id: "rs0",members: [{_id: 0, host: "mongo-0"}]})
// 输出结果
{ ok: 1 }
- 接下来,我在
app1
命名空间中创建了一个ExternalName
服务,将其与步骤 1 中的 mongo 服务进行关联,内容如下:
# Source: app/templates/svc.yaml
kind: Service
apiVersion: v1
metadata:
name: app1
namespace: app1
spec:
type: ExternalName
externalName: mongo.ng-mongo.svc.cluster.local
ports:
- port: 27017
- 最后,我对我的
golang
应用程序进行了仪表化处理,如下所示:
// 连接 URI
const mongo_uri = "mongodb://app1" // 这里我使用了 app1,因为 ExternalName 服务的名称是 `app1`
<RETRACTED-CODE>
然后我运行了应用程序,并检查了日志。以下是我发现的内容:
> 2022/11/22 12:49:47 server selection error: server selection timeout, current topology: { Type: ReplicaSetNoPrimary, Servers: [{ Addr: mongo-0:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: dial tcp: lookup mongo-0 on 10.96.0.10:53: no such host }, ] }
更新:我没有为 mongodb 设置任何用户名或密码。
有人可以帮我解释为什么会出现这种情况吗?
英文:
I have the following components up and running in a kubernetes cluster
- A GoLang Application writing data to a mongodb statefulset replicaset in namespace
app1
- A mongodb replicaset (1 replica) running as a statefulset in the namespace
ng-mongo
What I need to do is, I need to access the mongodb database by the golang
application for write/read opeations, so what I did was;
- Create a headless service for the
mongodb
in theng-mongo
namespace as below:
# Source: mongo/templates/svc.yaml
apiVersion: v1
kind: Service
metadata:
name: mongo
namespace: ng-mongo
labels:
app: mongo
spec:
ports:
- port: 27017
targetPort: 27017
name: mongo
clusterIP: None
selector:
role: mongo
- And then I deployed the
mongodb
statefulset and initialized the replicaset as below:
kubectl exec -it mongo-0 -n ng-mongo mongosh
rs.initiate({_id: "rs0",members: [{_id: 0, host: "mongo-0"}]})
// gives output
{ ok: 1 }
- Then I created an
ExternalName
service in theapp1
namespace linking the above mongo service in step 1, look below:
# Source: app/templates/svc.yaml
kind: Service
apiVersion: v1
metadata:
name: app1
namespace: app1
spec:
type: ExternalName
externalName: mongo.ng-mongo.svc.cluster.local
ports:
- port: 27017
- And at last, I instrumented my
golang
application as follows;
// Connection URI
const mongo_uri = "mongodb://app1" <-- Here I used the app1, as the ExternalName service's name is `app1`
<RETRACTED-CODE>
And then I ran the application, and checked the logs. Here is what I found:
> 2022/11/22 12:49:47 server selection error: server selection timeout, current topology: { Type: ReplicaSetNoPrimary, Servers: [{ Addr: mongo-0:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: dial tcp: lookup mongo-0 on 10.96.0.10:53: no such host }, ] }
Update: I haven't set any usernames or passwords for the mongodb
Can someone help me why this is happening?
答案1
得分: 1
经过一番调查,我找到了问题所在。
在为rs.initiate({})
指定host
条目时,我应该提供相关mongodb
实例的FQDN
(在我的情况下是mongo-0
pod)。因此,我的初始化命令应该像这样:
rs.initiate({_id: "rs0", members: [{_id: 0, host: "mongo-0.mongo.ng-mongo.svc.cluster.local:27017"}]})
英文:
After some digging, I was able to find the issue.
When specifying the host
entry for the rs.initiate({})
, I should provide the FQDN
of the relevant mongodb
instance (in my case it is the mongo-0
pod). Therefore, my initialisation command should look like this;
rs.initiate({_id: "rs0",members: [{_id: 0, host: "mongo-0.mongo.ng-mongo.svc.cluster.local:27017"}]})
答案2
得分: 0
根据我对你的意图的理解,
你的Pod(golang应用程序)和app1
服务已经在同一个命名空间中。
然而,从日志来看,
> 2022/11/22 12:49:47 服务器选择错误:服务器选择超时,当前拓扑:{ 类型:ReplicaSetNoPrimary,服务器:[{ 地址:mongo-0:27017,类型:未知,最后错误:connection() error occurred during connection handshake: dial tcp: lookup mongo-0 on 10.96.0.10:53: no such host }, ] }
这个日志意味着在DNS中找不到名为'mongo-0'的域名。(注意10.96.0.10 IP可能是kube-dns)
你的应用程序尝试连接到域名mongo-0
,但是域名mongo-0
在DNS中不存在(这意味着在app1
命名空间中没有名为mongo-0
的服务)。
你的应用程序试图访问的mongo-0
是什么?
(显然,日志显示了对域名mongo-0
的访问尝试,而你的golang应用程序的mongo_uri
指示为mongodb://app1
)
找出为什么你的应用程序尝试连接到mongo-0
域名将有助于解决这个问题。
希望对你有所帮助。
英文:
From my understanding of what you are trying to do,
Your Pod(golang application) and app1
Service are already in the same namespace.
However, looking at the log,
> 2022/11/22 12:49:47 server selection error: server selection timeout, current topology: { Type: ReplicaSetNoPrimary, Servers: [{ Addr: mongo-0:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: dial tcp: lookup mongo-0 on 10.96.0.10:53: no such host }, ] }
The log means that the domain named 'mongo-0' could not be found in DNS. (Note that 10.96.0.10 IP is probably kube-dns)
Your application tries to connect to the domain mongo-0
, but the domain mongo-0
does not exist in DNS (It means there is no service named mongo-0
on app1
namespace).
What is the 'mongo-0' that your Application trying to access?
(Obviously the log shows an attempt to access the domain mongo-0
and your golang applications mongo_uri
indicates mongodb://app1
)
Finding out why your application are trying to connect to the mongo-0
domain will help solve the problem.
Hope this helps you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论