Kubernetes如何从一个命名空间访问另一个命名空间中的应用程序

huangapple go评论88阅读模式
英文:

Kubernetes how to access application in one namespace from another

问题

我在一个 Kubernetes 集群中运行了以下组件:

  • 一个 GoLang 应用程序,将数据写入到命名空间为 app1 的 mongodb statefulset replicaset 中。
  • 一个运行在 ng-mongo 命名空间中的 mongodb replicaset(1 个副本)。

我需要做的是,让 golang 应用程序能够通过访问 mongodb 数据库进行读写操作,所以我做了以下操作:

  1. 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
  1. 然后,我部署了 mongodb statefulset 并初始化了 replicaset,内容如下:
kubectl exec -it mongo-0 -n ng-mongo mongosh
rs.initiate({_id: "rs0",members: [{_id: 0, host: "mongo-0"}]})

// 输出结果
{ ok: 1 }
  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
  1. 最后,我对我的 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;

  1. Create a headless service for the mongodb in the ng-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
  1. 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: &quot;rs0&quot;,members: [{_id: 0, host: &quot;mongo-0&quot;}]})


// gives output 
{ ok: 1 }
  1. Then I created an ExternalName service in the app1 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
  1. And at last, I instrumented my golang application as follows;
// Connection URI 
const mongo_uri = &quot;mongodb://app1&quot; &lt;-- Here I used the app1, as the ExternalName service&#39;s name is `app1`

&lt;RETRACTED-CODE&gt;

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: &quot;rs0&quot;,members: [{_id: 0, host: &quot;mongo-0.mongo.ng-mongo.svc.cluster.local:27017&quot;}]})

答案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.

huangapple
  • 本文由 发表于 2022年11月22日 20:55:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/74532966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定