<p>I have two applications, nginx and redis, where nginx uses redis to cache some data so the redis address must be configured in nginx.</p>
<p>On the one hand, I could first apply the redis deployment and get its IP and then apply the nginx deployment to set up the two application in my <a href="https://kubernetes.io/docs/tasks/tools/install-minikube/" rel="nofollow noreferrer">minikube</a>.</p>
<p>But on the other, to simplify <a href="https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/#deploying-containerized-applications" rel="nofollow noreferrer">installation</a> in the <a href="https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/" rel="nofollow noreferrer">Kubernetes Dashboard</a> for QA, I want to create a single Kubernetes YAML file (like <a href="https://github.com/GoogleCloudPlatform/microservices-demo/blob/master/release/kubernetes-manifests.yaml" rel="nofollow noreferrer">GoogleCloudPlatform/microservices-demo/kubernetes-manifests.yaml</a>) to deploy these two applications on two diverse <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod/" rel="nofollow noreferrer">Pod</a>s. However, if I do it by means of <a href="https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/" rel="nofollow noreferrer">Environment Variables</a>, I cannot get the redis address.</p>
<p>So how do I achieve it?</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
 name: redis-master
 labels:
  app: redis
spec:
 selector:
  matchLabels:
   app: redis
   role: master
   tier: backend
 replicas: 2
 template:
  metadata:
   labels:
    app: redis
    role: master
    tier: backend
  spec:
   containers:
   - name: master-c
     image: docker.io/redis:alpine
     ports:
     - containerPort: 6379
---
apiVersion: apps/v1
kind: Deployment
metadata:
 name: my-nginx
spec:
 selector: # Defines how the Deployment finds which Pods to manage.
  matchLabels:
   app: my-nginx
 template:
  metadata: # Defines what the newly created Pods are labeled.
   labels:
    app: my-nginx
    tier: frontend
  spec:
   terminationGracePeriodSeconds: 5
    containers:
    - name: my-nginx # Defines container name
      image: my-nginx:dev # docker image load -i my-nginx-docker_image.tar
      imagePullPolicy: Never # Always, IfNotPresent (default), Never
      ports:
      env:
      - name: NGINX_ERROR_LOG_SEVERITY_LEVEL
        value: debug
      - name: MY_APP_REDIS_HOST
        # How to use the IP address of the POD with redis-master labeled that is created by the previous deployment?
        value: 10.86.50.235
        # https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/
        # valueFrom:
        #  fieldRef:
        #   fieldPath: status.podIP # this is the current POD IP
      - name: MY_APP_CLIENT_ID
        value: client_id
      - name: MY_APP_CLIENT_SECRET
        # https://kubernetes.io/docs/concepts/configuration/secret
        value: client_secret
---
# https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service
apiVersion: v1
kind: Service
# https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors
# https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/
# metadata - Data that helps uniquely identify the object, including a name string, UID, and optional namespace
metadata:
 name: my-nginx
spec:
 type: NodePort
 selector:
  # Defines a proper selector for your pods with corresponding `.metadata.labels` field. 
  # Verify it using: kubectl get pods --selector app=my-nginx || kubectl get pod -l app=my-nginx
  # Make sure the service points to correct pod by, for example, `kubectl describe pod -l app=my-nginx`
  app: my-nginx
 ports:
 # By default and for convenience, the `targetPort` is set to the same value as the `port` field.
 - name: http
   port: 6080
   targetPort: 80
   # By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
   nodePort: 30080
 - name: https
   port: 6443
   targetPort: 443
   nodePort: 30443
</code></pre>
<p>Added some network output,</p>
<pre><code>    
    Microsoft Windows [Version 10.0.18362.900]
    (c) 2019 Microsoft Corporation. All rights reserved.
    
    PS C:\Users\ssfang&gt; kubectl get pods
    NAME                            READY   STATUS    RESTARTS   AGE
    my-nginx-pod                    1/1     Running   9          5d14h
    redis-master-7db899bccb-npl6s   1/1     Running   3          2d15h
    redis-master-7db899bccb-rgx47   1/1     Running   3          2d15h
    
    C:\Users\ssfang&gt; kubectl exec redis-master-7db899bccb-npl6s -- cat /etc/resolv.conf
    nameserver 10.96.0.10
    search default.svc.cluster.local svc.cluster.local cluster.local
    options ndots:5
    
    C:\Users\ssfang&gt; kubectl exec my-nginx-pod -- cat /etc/resolv.conf
    nameserver 10.96.0.10
    search default.svc.cluster.local svc.cluster.local cluster.local
    options ndots:5
    
    C:\Users\ssfang&gt; kubectl -n kube-system get svc
    NAME                                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                  AGE
    ingress-nginx-controller-admission   ClusterIP   10.108.221.2   &lt;none&gt;        443/TCP                  7d11h
    kube-dns                             ClusterIP   10.96.0.10     &lt;none&gt;        53/UDP,53/TCP,9153/TCP   7d17h
    
    
    C:\Users\ssfang&gt; kubectl get ep kube-dns --namespace=kube-system
    NAME       ENDPOINTS                                                 AGE
    kube-dns   172.17.0.2:53,172.17.0.5:53,172.17.0.2:9153 + 3 more...   7d17h
    
    C:\Users\ssfang&gt; kubectl get ep kube-dns --namespace=kube-system -o=yaml
    apiVersion: v1
    kind: Endpoints
    metadata:
      annotations:
        endpoints.kubernetes.io/last-change-trigger-time: &quot;2020-07-09T02:08:35Z&quot;
      creationTimestamp: &quot;2020-07-01T09:34:44Z&quot;
      labels:
        k8s-app: kube-dns
        kubernetes.io/cluster-service: &quot;true&quot;
        kubernetes.io/name: KubeDNS
      managedFields:
      - apiVersion: v1
        fieldsType: FieldsV1
        fieldsV1:
          f:metadata:
            f:annotations:
              .: {}
              f:endpoints.kubernetes.io/last-change-trigger-time: {}
            f:labels:
              .: {}
              f:k8s-app: {}
              f:kubernetes.io/cluster-service: {}
              f:kubernetes.io/name: {}
          f:subsets: {}
        manager: kube-controller-manager
        operation: Update
        time: &quot;2020-07-09T02:08:35Z&quot;
      name: kube-dns
      namespace: kube-system
      resourceVersion: &quot;523617&quot;
      selfLink: /api/v1/namespaces/kube-system/endpoints/kube-dns
    subsets:
    - addresses:
        nodeName: minikube
        targetRef:
          kind: Pod
          namespace: kube-system
          resourceVersion: &quot;523566&quot;
          uid: ed3a9f46-718a-477a-8804-e87511db16d1
      - ip: 172.17.0.5
        nodeName: minikube
        targetRef:
          kind: Pod
          name: coredns-546565776c-hmm5s
          namespace: kube-system
          resourceVersion: &quot;523616&quot;
          uid: ae21c65c-e937-4e3d-8a7a-636d4f780855
      ports:
      - name: dns-tcp
        port: 53
        protocol: TCP
      - name: metrics
        port: 9153
        protocol: TCP
      - name: dns
        port: 53
        protocol: UDP
    C:\Users\ssfang&gt; kubectl get service
    NAME                 TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)                         AGE
    kubernetes           ClusterIP   10.96.0.1     &lt;none&gt;        443/TCP                         7d20h
    my-nginx-service     NodePort    10.98.82.96   &lt;none&gt;        6080:30080/TCP,6443:30443/TCP   7d13h
    
    PS C:\Users\ssfang&gt; kubectl describe pod/my-nginx-pod | findstr IP
    IP:           172.17.0.8
    IPs:
      IP:  172.17.0.8
    PS C:\Users\ssfang&gt; kubectl describe service/my-nginx-service | findstr IP
    IP:                       10.98.82.96
    
    C:\Users\ssfang&gt; kubectl describe pod/my-nginx-65ffdfb5b5-dzgjk | findstr IP
    IP:           172.17.0.4
    IPs:
      IP:           172.17.0.4
    
</code></pre>
<p>Take two Pods with nginx for example to inspect network,</p>
<ol>
<li>C:\Users\ssfang&gt; kubectl exec my-nginx-pod -it -- bash</li>
</ol>
<pre><code>    
    # How to install nslookup, dig, host commands in Linux
    apt-get install dnsutils -y # In ubuntu
    yum install bind-utils -y # In RHEL/Centos
    
    root@my-nginx-pod:/etc# apt update &amp;&amp; apt-get install -y dnsutils iputils-ping
    
    root@my-nginx-pod:/etc# nslookup my-nginx-service
    Server:         10.96.0.10
    Address:        10.96.0.10#53
    
    Name:   my-nginx-service.default.svc.cluster.local
    Address: 10.98.82.96
    
    root@my-nginx-pod:/etc# nslookup my-nginx-pod
    Server:         10.96.0.10
    Address:        10.96.0.10#53
    
    ** server can't find my-nginx-pod: SERVFAIL
    
    
    root@my-nginx-pod:/etc# ping -c3 -W60 my-nginx-pod
    PING my-nginx-pod (172.17.0.8) 56(84) bytes of data.
    64 bytes from my-nginx-pod (172.17.0.8): icmp_seq=1 ttl=64 time=0.011 ms
    64 bytes from my-nginx-pod (172.17.0.8): icmp_seq=2 ttl=64 time=0.021 ms
    64 bytes from my-nginx-pod (172.17.0.8): icmp_seq=3 ttl=64 time=0.020 ms
    
    --- my-nginx-pod ping statistics ---
    3 packets transmitted, 3 received, 0% packet loss, time 2065ms
    rtt min/avg/max/mdev = 0.011/0.017/0.021/0.005 ms
    
    root@my-nginx-pod:/etc# ping -c3 -W20 my-nginx-service
    PING my-nginx-service.default.svc.cluster.local (10.98.82.96) 56(84) bytes of data.
    
    --- my-nginx-service.default.svc.cluster.local ping statistics ---
    3 packets transmitted, 0 received, 100% packet loss, time 2060ms
    
    
    root@my-nginx-pod:/etc# ping -c3 -W20 my-nginx-pod.default.svc.cluster.local
    ping: my-nginx-pod.default.svc.cluster.local: Name or service not known
    
    root@my-nginx-pod:/etc# ping -c3 -W20 my-nginx-service.default.svc.cluster.local
    PING my-nginx-service.default.svc.cluster.local (10.98.82.96) 56(84) bytes of data.
    
    --- my-nginx-service.default.svc.cluster.local ping statistics ---
    3 packets transmitted, 0 received, 100% packet loss, time 2051ms
    
</code></pre>
<ol start="2">
<li>C:\Users\ssfang&gt; kubectl exec my-nginx-65ffdfb5b5-dzgjk -it -- bash</li>
</ol>
<pre><code>    
    root@my-nginx-65ffdfb5b5-dzgjk:/etc# ping -c3 -W20 my-nginx-pod.default.svc.cluster.local
    ping: my-nginx-pod.default.svc.cluster.local: Name or service not known
    root@my-nginx-65ffdfb5b5-dzgjk:/etc# ping -c3 -W20 my-nginx-service.default.svc.cluster.local
    ping: my-nginx-service.default.svc.cluster.local: Name or service not known
    
    root@my-nginx-65ffdfb5b5-dzgjk:/etc# ping -c3 -W20 172.17.0.8
    PING 172.17.0.8 (172.17.0.8) 56(84) bytes of data.
    64 bytes from 172.17.0.8: icmp_seq=1 ttl=64 time=0.195 ms
    64 bytes from 172.17.0.8: icmp_seq=2 ttl=64 time=0.039 ms
    64 bytes from 172.17.0.8: icmp_seq=3 ttl=64 time=0.039 ms
    
    --- 172.17.0.8 ping statistics ---
    3 packets transmitted, 3 received, 0% packet loss, time 2055ms
    rtt min/avg/max/mdev = 0.039/0.091/0.195/0.073 ms

</code></pre>
<ol start="3">
<li>C:\Users\ssfang&gt; ssh -o StrictHostKeyChecking=no -i C:\Users\ssfang.minikube\machines\minikube\id_rsa docker@10.86.50.252 &amp;:: minikube ssh</li>
</ol>
<pre><code>
                             _             _
                _         _ ( )           ( )
      ___ ___  (_)  ___  (_)| |/')  _   _ | |_      __
    /' _ ` _ `\| |/' _ `\| || , &lt;  ( ) ( )| '_`\  /'__`\
    | ( ) ( ) || || ( ) || || |\`\ | (_) || |_) )(  ___/
    (_) (_) (_)(_)(_) (_)(_)(_) (_)`\___/'(_,__/'`\____)
    
    $ ping default.svc.cluster.local
    ping: bad address 'default.svc.cluster.local'
    $ ping my-nginx-pod.default.svc.cluster.local
    ping: bad address 'my-nginx-pod.default.svc.cluster.local'
    $ ping my-nginx-service.default.svc.cluster.local
    ping: bad address 'my-nginx-service.default.svc.cluster.local'
    $ nslookup whoami
    Server:         10.86.50.1
    Address:        10.86.50.1:53
    
    ** server can't find whoami: NXDOMAIN
    
    ** server can't find whoami: NXDOMAIN
    
    $ ping -c3 -W20 172.17.0.8
    PING 172.17.0.8 (172.17.0.8): 56 data bytes
    64 bytes from 172.17.0.8: seq=0 ttl=64 time=0.053 ms
    64 bytes from 172.17.0.8: seq=1 ttl=64 time=0.035 ms
    64 bytes from 172.17.0.8: seq=2 ttl=64 time=0.040 ms
    
    --- 172.17.0.8 ping statistics ---
    3 packets transmitted, 3 packets received, 0% packet loss
    round-trip min/avg/max = 0.035/0.042/0.053 ms
    $ ping -c3 -W20 172.17.0.4
    PING 172.17.0.4 (172.17.0.4): 56 data bytes
    64 bytes from 172.17.0.4: seq=0 ttl=64 time=0.070 ms
    64 bytes from 172.17.0.4: seq=1 ttl=64 time=0.039 ms
    64 bytes from 172.17.0.4: seq=2 ttl=64 time=0.038 ms
    
    --- 172.17.0.4 ping statistics ---
    3 packets transmitted, 3 packets received, 0% packet loss
    round-trip min/avg/max = 0.038/0.049/0.070 ms

</code></pre>

samm
<p>Hardcoding IP-address is not a good practice. Instead you can create a service for redis as well and configure the service dns name in your nginx deployment using the kubernetes dns config like this <code>my-svc.my-namespace.svc.cluster-domain.example</code>. Your nginx will then communicate to the redis container through this service.</p>
