kubernetes部署可以将服务节点端口注入为环境变量吗? [英] Can a kubernetes Deployment inject the service nodeport as an environment variable?

查看:116
本文介绍了kubernetes部署可以将服务节点端口注入为环境变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在服务yaml文件中,您可以使用jsonpath选择命名的nodeport值,如下所示:

    - name: MY_NODE_PORT
      valueFrom:
        fieldRef:
          fieldPath: spec.ports[?(@.name=="http")].nodePort

但是,在我的部署yaml文件中,我希望有一个类似于MY_NODE_PORT的环境变量,该变量暴露给容器的容器.我碰巧将我的服务和部署组合到一个文件中,该文件供我kubectl create -f使用.是否可以在部署"部分而不是服务"部分中选择命名的服务节点端口?

我的目的是注册Kubernetes服务并将其部署到旧版服务发现机制(在本例中为Netflix OSS Eureka).

解决方案

Janos Lenart 的回答和 Marc Sl​​uiter 指出,服务和部署是不同的资源,可以很容易地在单独的文件中指定.他们彼此之间没有直接的知识,即使您为服务NodePort端口命名,除非您明确指定服务NodePort端口值(例如30000到32767之间的值),否则您将无法指定pod环境变量应该匹配它.虽然您可以像这样对NodePort端口值进行硬编码,但建议使用 Janos Lenart ,但这很脆弱,不建议这样做.

虽然Kubernetes为Pod提供了许多方便的环境变量,但Pod环境变量无法引用由Kubernetes动态分配的服务节点端口端口值.

但是,Pod 确实有权与Kubernetes API服务器进行对话,并且该API服务器将能够使用有关服务的信息(例如节点端口)进行回复.因此,Pod可以向API服务器询问服务的NodePort端口值.我创建了使用简单的sidecar pod的kubernetes服务和部署作为概念证明. /p>

此处的示例用于向(外部或内部)Kubernetes NodePort服务注册)Netflix OSS Eureka服务注册表.这对于其他人将Kubernetes桥接到其他旧式服务发现机制可能很有用.

Inside a service yaml file, you can use jsonpath to select a named nodeport value like so:

    - name: MY_NODE_PORT
      valueFrom:
        fieldRef:
          fieldPath: spec.ports[?(@.name=="http")].nodePort

However, in my deployment yaml file, I would like to have an environment variable like MY_NODE_PORT above that is exposed to the container to the pod. I happen to have combined my service and deployment into a single file for me to kubectl create -f. Is it possible to select the named service nodeport in the deployment section, rather than in the service section?

My purpose is to register a Kubernetes service and deployment to a legacy service discovery mechanism, in this case Netflix OSS Eureka.

解决方案

As in Janos Lenart's answer and Marc Sluiter commented, a service and a deployment are different resources and might as easily be specified in separate files. They have no direct knowledge of one another, and, even if you name the service NodePort port, unless you explicitly specify the service NodePort port value (e.g. something from 30000 to 32767) you won't be able to specify what the pod environment variable should be to match it. While you can hard code the NodePort port value like that, as Janos Lenart suggested, but it's brittle and not recommended.

While Kubernetes provides a number of handy environment variables to pods, it is not possible for a pod environment variable to reference a service nodeport port value that was dynamically assigned by kubernetes.

However, the Pod does have access to talk to the Kubernetes API server, and the API server will be able to reply back with information about the service, such as the nodeport. So a Pod can ask the API server for a service's NodePort port value. I created a kubernetes service and deployment with simple sidecar pod as a proof of concept.

My example here handles registering a Kubernetes NodePort service with an (external or internal) Netflix OSS Eureka service registry. This might be useful to others for bridging Kubernetes to other legacy service discovery mechanisms.

这篇关于kubernetes部署可以将服务节点端口注入为环境变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆