这些变量从哪里出现在我的 kubernetes 容器中?

From where appeared these variables in my kubernetes container?

我正在部署简单的 hello-world 微服务,它监听以下变量给定的端口:

PORT = os.environ.get("TEST_SERVICE_PORT", "").strip() or "50001"

我部署它时没有在容器上配置任何变量,并希望它在默认的 50001 端口上提供服务,但却出现了错误

socket.gaierror: [Errno -8] Servname not supported for ai_socktype

登录container查看环境,发现环境中填充了不同的变量(有些属于其他服务),TEST_SERVICE_PORT变量存在且绝对不是端口:

root@test-service-697464787c-xpd6k:/opt/app/src# env | grep TEST
TEST_SERVICE_PORT_7002_TCP_ADDR=10.145.23.43
TEST_SERVICE_SERVICE_PORT_GRPC_API=7002
TEST_SERVICE_PORT_7002_TCP_PORT=7002
TEST_SERVICE_PORT=tcp://10.145.23.43:7002
TEST_SERVICE_SERVICE_HOST=10.145.23.43
TEST_SERVICE_PORT_7002_TCP=tcp://10.145.23.43:7002
TEST_SERVICE_PORT_7002_TCP_PROTO=tcp
TEST_SERVICE_SERVICE_PORT=7002

我有以下问题,但无法在文档中找到答案:

是什么创造了这个变量?我能以某种方式将容器与它们隔离开来吗?或者它们是由 kubernetes 有意设置的,并且服务于我不知道的某些目的?我应该如何命名我的配置变量以避免命名冲突?我应该使用该变量而不是使用服务名称作为主机名吗?

following documentation,但只解释变量TEST_SERVICE_SERVICE_PORTTEST_SERVICE_SERVICE_HOST。那么 TEST_SERVICE_PORT 和其他人是什么意思呢?什么添加了 TEST_SERVICE_SERVICE_PORT_GRPC_API

我正在使用的集群上还安装了 Istio 和 Ambassador 网关。

Q: What created this variables?

Adiscovery-service(更多在最后)

Q: Could I somehow isolate container from them?

A:如果您想禁用它,您可以在 PodSpec

上设置 enableServiceLinks: false

Q: Or are they set intentionally by kubernetes, and serve some purpose I don't know about?

A:不,它们只是在那里提供除了 DNS 和名称之外的选项,kubernetes 不使用它们

Q: How should I name my configuration variables to avoid naming collisions?

A:要么使用 enableServiceLinks: false,要么使用与文档中描述的模式不冲突的命名模式,通常我更喜欢使用 _SVC_PORT 当我需要像你一样做事时

Q: Should I use that variables istead of using services names as hostnames?

A:来自文档:"You can (and almost always should) set up a DNS service for your Kubernetes cluster",

Q: There is following documentation, but it only explains variable TEST_SERVICE_SERVICE_PORT and TEST_SERVICE_SERVICE_HOST. What TEST_SERVICE_PORT and others mean then? What adds TEST_SERVICE_SERVICE_PORT_GRPC_API?

A:您有一个名为 grpc-api 的命名端口,在这种情况下,它使用的是命名协议 + 端口号。注意:我在文档中找不到任何参考资料,所以我深入研究了 code


来自文档 discovery-service

When a Pod is run on a Node, the kubelet adds a set of environment variables for each active Service. ... simpler {SVCNAME}_SERVICE_HOST and {SVCNAME}_SERVICE_PORT variables, where the Service name is upper-cased and dashes are converted to underscores...

For example, the Service "redis-master" which exposes TCP port 6379 and has been allocated cluster IP address 10.0.0.11, produces the following environment variables:

REDIS_MASTER_SERVICE_PORT=6379 REDIS_MASTER_PORT=tcp://10.0.0.11:6379
REDIS_MASTER_PORT_6379_TCP=tcp://10.0.0.11:6379
REDIS_MASTER_PORT_6379_TCP_PROTO=tcp
REDIS_MASTER_PORT_6379_TCP_PORT=6379
REDIS_MASTER_PORT_6379_TCP_ADDR=10.0.0.11 

来自 k8s api PodSpec/EnableServiceLinks:

EnableServiceLinks indicates whether information about services should be injected into pod's environment variables, matching the syntax of Docker links. Optional: Defaults to true.

这些环境变量有助于服务发现。您可以通过设置

来禁用它们
$ kubectl explain deployment.spec.template.spec.enableServiceLinks
KIND:     Deployment
VERSION:  extensions/v1beta1

FIELD:    enableServiceLinks <boolean>

DESCRIPTION:
     EnableServiceLinks indicates whether information about services should be
     injected into pod's environment variables, matching the syntax of Docker
     links. Optional: Defaults to true.