<p>We have deployed some of the services (like Web API) on Azure Kubernetes. When we logged onto the Azure Kubernetes Pods and execute the printenv on the terminal, it shows environment variable on the screen including the service we deployed like</p>

<pre><code>&lt;SERVICENAME&gt;_PORT=
&lt;SERVICENAME&gt;_HOST=`
.....
</code></pre>

<p>How can we read above variable value in the .Net code? I tried with the below code but it didn't work</p>

<pre><code> var builder = new ConfigurationBuilder();
            builder.AddJsonFile("appsettings_prod.json", true, true).AddEnvironmentVariables();
            Configuration = builder.Build();

var port =  Configuration["&lt;SERVICENAME&gt;_PORT"] 
</code></pre>

HowsTheJosh
<p>To access env from k8s pods, you need to provide those env through pod's <code>spec.containers[].env[]</code>.</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: demo-pod
spec:
  containers:
  - name: mycontainer
    image: demo/new-image
    env:
      # define env from k8s secret (used specially for credentials)
      - name: SECRET_USERNAME
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: username
      # define env from configmap
      - name: SPECIAL_CREDENTIALS
          valueFrom:
            configMapKeyRef: 
              name: configmap-name
              key: config.json
      # define value directly 
      - name: DEMO_GREETING
        value: "Hello from the environment"
</code></pre>
