<p>I developed two web apps(we call them A and B) and deployed them to k8s. The web app A refers to the images generated by web app B. In previous environment, B has the fixed IP and port which makes it very easier to reach the images hosted on B.</p>

<p>In the K8S environment, I use the service type - NodePort. In that case, the port is randomly generated upon deployment each time. </p>

<p>The question is whether there is a way to fetch B service's node port programmatically from the A Pod. Or is it possible to fix the node port for one service? </p>

<p>There are three answers to your question:</p>

<p>The first is that one should not be destroying the <code>Service</code> just because the <code>Pod</code>s behind it are redeployed. The <code>Service</code> is designed to be the long-lived contract that abstracts over the placement (or count) of the servicing <code>Pod</code>s</p>

<p>The second is that if you do wish to have web-app A use a known-to-you <code>NodePort</code>, you can put a value in the <a href="https://kubernetes.io/docs/api-reference/v1.7/#serviceport-v1-core" rel="nofollow noreferrer">nodePort:</a> field of the <code>ports:</code> mapping inside the <code>Service</code>, and assuming it is not already assigned, kubernetes will defer to your judgement </p>

<p>The third answer is that every <code>Pod</code> that runs within the cluster automatically gains an API token for communication with the kubernetes API (it lives in <code>/run/secrets/kubernetes.io</code> or similar), so you can ask the API from within the Pods to give you the current value of the Service's nodePort</p>
