<p>In my AKS cluster I have 2 services (API and Redis DB). I want to connect to the Redis DB from the API pod.</p>
<p>I got the Redis Env using <code>kubectl exec redis-pod -- printenv</code> and the host variable is <code>REDIS_SERVICE_HOST</code>, is it possible to inject this env into the API deployment?</p>
<p>I tried something like this but it didn't work:</p>
<pre><code>env:
   - name: REDIS_HOST
     value: ${REDIS_SERVICE_HOST}:6379
</code></pre>

capiono
<p>You can store the redis host IP in a configMap and use that as env</p>
<pre><code>  export REDISHOST_IP=XXX.XXX.XXX.XXX
  kubectl create configmap redishost --from-literal=REDISHOST=${REDISHOST_IP}
</code></pre>
<hr />
<pre><code>env:
- name: REDISHOST
  valueFrom:
    configMapKeyRef:
      name: redishost
      key: REDISHOST
</code></pre>
<p>A better approach would be to use kubernetes service to expose redis</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: redis
  namespace: default
  labels:
    app: redis
  ports:
  - port: 6379
    targetPort: 6379
  selector:
    app: redis
</code></pre>
<p>Note: in this example the redis pod need to have label <code>app: redis</code></p>
<p>Then inject <code>redis.default.svc.cluster.local</code> as redis host in other pods using env.</p>
<p>You also use configMap to store <code>redis.default.svc.cluster.local</code> and use it as env.</p>
<pre><code>kubectl create configmap redishost --from-literal=REDISHOST=redis.default.svc.cluster.local
</code></pre>
<hr />
<pre><code>env:
- name: REDISHOST
  valueFrom:
    configMapKeyRef:
      name: redishost
      key: REDISHOST
</code></pre>
<p>This way even if redis pod is restarted and pod IP changes the other pods connecting to redis is not impacted and no change would be necessary.</p>
