<p>I'm using <a href="https://kubernetes.io/docs/setup/minikube/" rel="nofollow noreferrer">minikube</a> to test out a local Kubernetes scenario.  I have 2 <a href="http://flask.pocoo.org/" rel="nofollow noreferrer">Python Flask</a> applications named 'frontend.py' and 'interceptor.py'.  Basically I want to do a GET to the frontend application which will then perform a POST to the interceptor application, and receive a response back from the interceptor.  I have this working if I manually find the interceptor application's IP and provide it in my 'frontend.py' script.  </p>

<p>My question: <strong>Is there a recommended way to automatically determine the IP address of an application running in one pod's container from an application inside another pod's container?</strong>  </p>

<p>I'm wondering if I might not even need to gather the IP in the application themselves if there is some other way to expose the pods' info to each other via the <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">Kubernetes Service</a> that I create, but I haven't had any luck yet.</p>

<p>Below are my services, and deployments.</p>

<p><strong>flask-frontend-deployment.yaml</strong></p>

<pre><code>apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: frontend-deployment
  labels:
    app: flask
spec:
  selector:
    matchLabels:
      app: flask
  replicas: 1
  template:
    metadata:
      labels:
        app: flask
        tier: frontend
    spec:
      containers:
      - name: flask-frontend
        image: alec/flask/frontend:1.0
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        env:
        - name: GET_HOSTS_FROM
          value: dns
        ports:
        - containerPort: 5001
</code></pre>

<p><strong>flask-frontend-service.yaml</strong></p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: frontend-service
spec:
  type: NodePort
  selector:
    app: flask
  ports:
  - protocol: TCP
    port: 5001
    targetPort: 5001
</code></pre>

<p><strong>flask-interceptor-deployment.yaml</strong></p>

<pre><code>apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: interceptor-deployment
  labels:
    app: flask
spec:
  selector:
    matchLabels:
      app: inter
      role: mid
      tier: middle
  replicas: 1
  template:
    metadata:
      labels:
        app: inter
        role: mid
        tier: middle
    spec:
      containers:
      - name: flask-interceptor
        image: alec/flask/interceptor:1.0
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        env:
        - name: GET_HOSTS_FROM
          value: env
        ports:
        - containerPort: 5002
</code></pre>

<p><strong>flask-interceptor-service.yaml</strong></p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: interceptor-service
spec:
  ports:
  - protocol: TCP
    port: 5002
</code></pre>

<p><strong>frontend.py</strong></p>

<pre><code>from flask import Flask
from flask import request
import requests

app = Flask(__name__)

@app.route("/")
def hello():
    address = 'http://172.17.0.5:5002/' #&lt;--- this is the IP I want to get automatically 
    r = requests.post(address, data="---[Token]---")
    return "Frontend Here. Response from Backend = "+str(r.content)

if __name__ == "__main__":
    app.run(debug=True,host='0.0.0.0',port=5001)
</code></pre>

<p><strong>interceptor.py</strong></p>

<pre><code>from flask import Flask
from flask import request
import requests

app = Flask(__name__)

content = ""

@app.route("/", methods=['POST'])
def testPost():
    if request.method == 'POST':
        return "Received POST ---&gt;&gt;&gt; " + str(request.data)
    else:
        return "Post didnt work"

@app.route("/", methods=['GET'])
def hello():
    return "Hello from the interceptor!"

if __name__ == "__main__":
    app.run(debug=True,host='0.0.0.0',port=5002)
</code></pre>

Cheen
<p>As long as you have the <code>kube-dns</code> add-on enabled in <code>minikube</code> you should be able to perform DNS-based service discovery. I would say that this is the recommended way of getting hold of the IP addresses of your resources.</p>

<p>To double check that you have <code>kube-dns</code> enabled run <code>minikube addons list</code>, it should be enabled by default.</p>

<p>The DNS names created for you will default be derived from the <code>metadata: name</code> field of your resource(s). Assuming you'll be using the <code>default</code> namespace then your Services DNS-names would be:</p>

<p><code>interceptor-service.default.svc.cluster.local</code></p>

<p>and</p>

<p><code>frontend-service.default.svc.cluster.local</code></p>

<p>You could use these fully qualified domain-names within your application to reach your Services or use a shorter version e.g. <code>frontend-service</code> as-is. This is possible since your Pods <code>resolv.conf</code> will be configured to look in <code>default.svc.cluster.local</code> when querying for <code>frontend-service</code>.</p>

<p>To test this (and noted in the comments), you should now be able to change this line:</p>

<p><code>address = 'http://172.17.0.5:5002/'</code></p>

<p>to</p>

<p><code>address = 'http://interceptor-service:5002/'</code></p>

<p>in your Flask app code.</p>

<p>Please see <a href="https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/" rel="nofollow noreferrer">this</a> page for more detailed information about DNS for Services and Pods in Kubernetes.</p>
