<p>I have the following pods <code>hello-abc</code> and <code>hello-def</code>.</p>

<p>And I want to send data from <code>hello-abc</code> to <code>hello-def</code>.</p>

<p>How would pod <code>hello-abc</code> know the IP address of <code>hello-def</code>?</p>

<p>And I want to do this programmatically.</p>

<p>What's the easiest way for <code>hello-abc</code> to find where <code>hello-def</code>?</p>

<pre><code>---
apiVersion: extensions/v1beta1
kind: Deployment

metadata:
name: hello-abc-deployment

spec:
replicas: 1

template:
    metadata:
    labels:
        app: hello-abc
    spec:
    containers:
    - name: hello-abc
        image: hello-abc:v0.0.1
        imagePullPolicy: Always
        args: ["/hello-abc"]
        ports:
        - containerPort: 5000

---
apiVersion: extensions/v1beta1
kind: Deployment

metadata:
name: hello-def-deployment

spec:
replicas: 1

template:
    metadata:
    labels:
        app: hello-def
    spec:
    containers:
    - name: hello-def
        image: hello-def:v0.0.1
        imagePullPolicy: Always
        args: ["/hello-def"]
        ports:
        - containerPort: 5001

---
apiVersion: v1
kind: Service

metadata:
name: hello-abc-service

spec:
ports:
- port: 80
    targetPort: 5000
    protocol: TCP

selector:
    app: hello-abc
type: NodePort

---
apiVersion: v1
kind: Service

metadata:
name: hello-def-service

spec:
ports:
- port: 80
    targetPort: 5001
    protocol: TCP

selector:
    app: hello-def
type: NodePort
</code></pre>

<p><strong>Preface</strong></p>

<p>Since you have defined a service that routes to each deployment, if you have deployed both services and deployments into the same namespace, you can in many modern kubernetes clusters take advantage of kube-dns and simply refer to the service by name. </p>

<p>Unfortunately if <code>kube-dns</code> is not configured in your cluster (although it is unlikely) you cannot refer to it by name.</p>

<p>You can read more about DNS records for services <a href="https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#services" rel="noreferrer">here</a></p>

<p>In addition Kubernetes features "Service Discovery" Which exposes the ports and ips of your services into any container which is deployed into the same namespace.</p>

<p><strong>Solution</strong></p>

<p>This means, to reach hello-def you can do so like this </p>

<p><code>curl http://hello-def-service:${HELLO_DEF_SERVICE_PORT}</code> </p>

<p>based on Service Discovery <a href="https://kubernetes.io/docs/concepts/services-networking/service/#environment-variables" rel="noreferrer">https://kubernetes.io/docs/concepts/services-networking/service/#environment-variables</a></p>

<p><strong>Caveat</strong>: Its very possible that if the Service port changes, only pods that are created after the change in the same namespace will receive the new environment variables.</p>

<p><strong>External Access</strong></p>

<p>In addition, you can also reach this your service externally since you are using the NodePort feature, as long as your NodePort range is accessible from outside.</p>

<p>This would require you to access your service by node-ip:nodePort</p>

<p>You can find out the NodePort which was randomly assigned to your service with <code>kubectl describe svc/hello-def-service</code></p>

<p><strong>Ingress</strong></p>

<p>To reach your service from outside you should implement an ingress service such as nginx-ingress</p>

<p><a href="https://github.com/helm/charts/tree/master/stable/nginx-ingress" rel="noreferrer">https://github.com/helm/charts/tree/master/stable/nginx-ingress</a>
<a href="https://github.com/kubernetes/ingress-nginx" rel="noreferrer">https://github.com/kubernetes/ingress-nginx</a></p>

<p><strong>Sidecar</strong></p>

<p>If your 2 services are tightly coupled, you can include both in the same pod using the Kubernetes Sidecar feature. In this case, both containers in the pod would share the same virtual network adapter and accessible via <code>localhost:$port</code></p>

<p><a href="https://kubernetes.io/docs/concepts/workloads/pods/pod/#uses-of-pods" rel="noreferrer">https://kubernetes.io/docs/concepts/workloads/pods/pod/#uses-of-pods</a></p>

<hr>

<p><strong>Service Discovery</strong></p>

<blockquote>
  <p>When a Pod is run on a Node, the kubelet adds a set of environment
  variables for each active Service. It supports both Docker links
  compatible variables (see makeLinkVariables) and simpler
  {SVCNAME}_SERVICE_HOST and {SVCNAME}_SERVICE_PORT variables, where the
  Service name is upper-cased and dashes are converted to underscores.</p>
</blockquote>

<p>Read more about service discovery here: 
<a href="https://kubernetes.io/docs/concepts/services-networking/service/#environment-variables" rel="noreferrer">https://kubernetes.io/docs/concepts/services-networking/service/#environment-variables</a></p>
