简体   繁体   English

Kubernetes通过环境与NodeJS共享Mongodb变量

[英]Kubernetes share Mongodb variables with NodeJS via environment

I have a simple setup in kubernetes hosting a NodeJS application using deployment + service and a mongo with its own deployment + service available in the same kubernetes cluster. 我在kubernetes中进行了一个简单的设置,使用部署+服务托管了一个NodeJS应用程序,在同一个kubernetes集群中使用了具有自己的部署+服务的mongo。

My problem is how do I get the mongo ip into my nodeJS env file as : DB_URL=mongo:27017/test via mongo´s kubernetes env to the nodeJS app ENV? 我的问题是如何通过mongo的kubernetes env将mongo ip输入到我的nodeJS env文件中: DB_URL=mongo:27017/test到nodeJS应用ENV?

I assume the Service IP for mongo is not constant and may change. 我认为mongo的服务IP不是恒定的,可能会更改。

You can access service host/ports as so: 您可以这样访问服务主机/端口:

this.url = 'mongodb://'
+ process.env.MYAPP_TRANSACTIONS_MONGO_SERVICE_HOST + ':'
+ process.env.MYAPP_TRANSACTIONS_MONGO_SERVICE_PORT;

Read more in the docs here 此处阅读更多文档

You can print your env variables for a given service with this command: 您可以使用以下命令为给定服务打印环境变量:

kubectl exec myapp-deployment-3599435869-fn70t   -- printenv | grep SERVICE

you can't base your connection on POD IP. 您不能基于POD IP建立连接。 You should either stick to service (preferably by using the service name) or consider StatefulSet that will make it possible to always reach your mongo 1st pod as mongo-1 or something like that 您应该坚持服务(最好使用服务名称),或者考虑使用StatefulSet,这样可以始终将mongo 1st pod作为mongo-1或类似的东西到达

One way would be to specify your own cluster IP address as part of a Service creation request. 一种方法是在服务创建请求中指定您自己的群集IP地址。 To do this, set the spec.clusterIP field. 为此,请设置spec.clusterIP字段。 The IP address that a user chooses must be a valid IP address and within the service-cluster-ip-range CIDR range that is specified by flag to the API server 用户选择的IP地址必须是有效的IP地址,并且必须在API标记所指定的service-cluster-ip-range CIDR范围内

Another menthod would be enable DNS throughout the cluster then all Pods should be able to do name resolution of Services automatically. 另一种方法是在整个群集中启用DNS,然后所有Pod都应该能够自动对服务进行名称解析。 For example, if you have a Service called "my-service" in Kubernetes Namespace "my-ns" a DNS record for "my-service.my-ns" is created. 例如,如果您在Kubernetes命名空间"my-ns"有一个名为"my-service" ,则会创建"my-service.my-ns"的DNS记录。 Pods which exist in the "my-ns" Namespace should be able to find it by simply doing a name lookup for "my-service" . 通过简单地对"my-service"进行名称查找, "my-service" "my-ns"命名空间中存在的Pod应该能够找到它。 Pods which exist in other Namespaces must qualify the name as "my-service.my-ns" . 存在于其他命名空间中的Pod必须将该名称限定为"my-service.my-ns" The result of these name lookups is the cluster IP. 这些名称查找的结果是群集IP。

https://kubernetes.io/docs/concepts/services-networking/service/#discovering-services https://kubernetes.io/docs/concepts/services-networking/service/#discovering-services

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM