Kubernetes 自动生成的环境变量设置错误?

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

Kubernetes autogenerated environment variables set wrong?

问题

在Pod启动期间,Kubernetes会基于我创建的服务(通过下行API?)创建一些环境变量。问题是其中一个变量MY_APPLICATION_PORT的初始化似乎不正确,看起来是这样的:

MY_APPLICATION_PORT=tcp://192.168.0.5:7777

而我希望它只包含7777这个值。问题是,我有一个Spring Boot应用,在application.properties文件中有这个属性:

my.application.port=7777

所以当Spring解析它的属性时,它会优先选择环境变量中的值,而不是.properties文件中的值,从而用错误的值覆盖它。

我的问题是 - 你们知道如何控制Kubernetes环境变量的创建吗?我可以在deployment.yaml文件中覆盖它,但我想知道是否还有其他方法。

编辑:

我在网上找到了一个描述与我的问题最接近的问题:
https://github.com/kubernetes/kubernetes/issues/65130

英文:

During pod startup Kubernetes is creating some environment variables based on services i created (via downward API?). Problem is that one of them, MY_APPLICATION_PORT, seems to be initialized incorrectly, it looks like:

MY_APPLICATION_PORT=tcp://192.168.0.5:7777

whereas i expect it to hold only 7777 value. The problem is that i have a Spring Boot application that has this property in application.properties:

my.application.port=7777

So when spring resolves it's properties, it prefers value from environment variable over one from .properties file, thus overwriting it with incorrect value.

My question is - do you guys know how to control creation of kubernetes env variables? I can overwrite it in my deployment.yaml, but I wonder if there's another way.

EDIT:

I've found this as a closest description of my issue I've seen online:
https://github.com/kubernetes/kubernetes/issues/65130

答案1

得分: 3

这个环境变量源自与一个非常旧的Docker功能的兼容性。在Kubernetes中,您可以通过在Pod规范的Container对象上设置enableServiceLinks: false来禁用它,无论它在哪里出现。例如:

apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      containers:
        - name: ...
          enableServiceLinks: false
          env: [...]

具体来说,语法旨在与第一代Docker网络中的容器链接生成的环境变量兼容。自那时以来,Docker还将DNS系统引入其核心,在纯Docker中使用链接现在被视为过时的做法。尤其是如果它导致冲突,像您在这里描述的那样,将此Kubernetes属性设置为始终安全的做法。

英文:

This environment variable comes from compatibility with a very old Docker feature. You can disable it in Kubernetes by setting enableServiceLinks: false on a Container object in a Pod spec, anywhere that may appear. For example:

apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      containers:
        - name: ...
          enableServiceLinks: false
          env: [...]

In particular the syntax is intended to be compatible with the environment variables generated by container links in first-generation Docker networking. Since then Docker has also introduced a DNS system into its core, and in pure Docker using links at all is now considered obsolete. It should be safe to always set this Kubernetes property, especially if it causes conflicts like what you describe here.

答案2

得分: 0

只是提一下它正在运行,但是在我的情况下,对于 StatefulSet,我不得不将 enableServiceLinks 标志移动到 spec 级别:

apiVersion: apps/v1
kind: StatefulSet
spec:
  template:
    spec:
      enableServiceLinks: false
      containers:
        - name: ...
英文:

Just to mention that it is working,

but in my case for a StatefulSet I had to move the enableServiceLinks flag at the spec level:

apiVersion: apps/v1
kind: StatefulSet
spec:
  template:
    spec:
      enableServiceLinks: false
      containers:
        - name: ...

huangapple
  • 本文由 发表于 2023年2月7日 00:44:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364184.html
匿名

发表评论

匿名网友

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

确定