<p>I am deploying simple hello-world microservice that listens on port given by following variable:</p>

<pre><code>PORT = os.environ.get("TEST_SERVICE_PORT", "").strip() or "50001"
</code></pre>

<p>I deployed it without configuring any variables on container, and expected it to serve on default 50001 port, but instead got error </p>

<pre><code>socket.gaierror: [Errno -8] Servname not supported for ai_socktype
</code></pre>

<p>When I logged into container and checked environment, I found out that evironment is filled with different variables (some of them belong to other services), and <code>TEST_SERVICE_PORT</code> variable exists and contains definitely not port:</p>

<pre><code>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
</code></pre>

<p>I have following questions and were not able find answers to them in documentation:</p>

<p>What created this variables? Could I somehow isolate container from them? Or are they set intentionally by kubernetes, and serve some purpose I don't know about? How should I name my configuration variables to avoid naming collisions? Should I use that variables istead of using services names as hostnames? </p>

<p>There is <a href="https://kubernetes.io/docs/concepts/containers/container-environment-variables/" rel="nofollow noreferrer">following documentation</a>, but it only explains variable <code>TEST_SERVICE_SERVICE_PORT</code> and <code>TEST_SERVICE_SERVICE_HOST</code>. What <code>TEST_SERVICE_PORT</code> and others mean then? What adds <code>TEST_SERVICE_SERVICE_PORT_GRPC_API</code>?</p>

<p>There is also Istio and Ambassador gateway installed on cluster that I'm using.</p>

<blockquote>
  <p>Q: What created this variables? </p>
</blockquote>

<p>&nbsp;&nbsp; <strong>A</strong>: <a href="https://kubernetes.io/docs/concepts/services-networking/service/#discovering-services" rel="nofollow noreferrer">discovery-service</a> (more at the end)</p>

<blockquote>
  <p>Q: Could I somehow isolate container from them? </p>
</blockquote>

<p>&nbsp;&nbsp; <strong>A</strong>: If you want to disable that, you can set <code>enableServiceLinks: false</code> on your <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.15/#podspec-v1-core" rel="nofollow noreferrer">PodSpec</a></p>

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

<p>&nbsp;&nbsp; <strong>A</strong>: No, they are there just there to give options besides DNS and names, kubernetes does not use them</p>

<blockquote>
  <p>Q: How should I name my configuration variables to avoid naming collisions? </p>
</blockquote>

<p>&nbsp;&nbsp; <strong>A</strong>: Either you use <code>enableServiceLinks: false</code> or use a naming pattern that does not conflict with the pattern described on the docs, usually I prefer use <code>_SVC_PORT</code> when I need to do something like you </p>

<blockquote>
  <p>Q: Should I use that variables istead of using services names as hostnames?</p>
</blockquote>

<p>&nbsp;&nbsp; <strong>A</strong>: From docs: "You can (and almost always should) set up a DNS service for your Kubernetes cluster",</p>

<blockquote>
  <p>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?</p>
</blockquote>

<p>&nbsp;&nbsp; <strong>A</strong>: You have a named port called <code>grpc-api</code>, in that case it is using the named instead protocol + port number. Note: I could not find any references on the docs for that, so I had dig into <a href="https://github.com/kubernetes/kubernetes/blob/master/pkg/kubelet/envvars/envvars.go#L53" rel="nofollow noreferrer">code</a></p>

<hr>

<p>From docs <a href="https://kubernetes.io/docs/concepts/services-networking/service/#discovering-services" rel="nofollow noreferrer">discovery-service</a> </p>

<blockquote>
  <p>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... </p>
  
  <p>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:</p>

<pre><code>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 
</code></pre>
</blockquote>

<p>From k8s api <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.15/#podspec-v1-core" rel="nofollow noreferrer">PodSpec</a>/EnableServiceLinks:</p>

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