<p>I'm very new to kubernetes/docker, so apologies if this is a silly question.</p>
<p>I have a pod that is accessing a few services. In my container I'm running a python script and need to access the service. Currently I'm doing this using the services' IP addresses.</p>
<p>Is the service IP address stable or is it better to use <a href="http://kubernetes.io/docs/user-guide/services/#environment-variables" rel="noreferrer">environment variables</a>? If so, some tips on doing that would be great.</p>
<p>The opening paragraph of the <a href="http://kubernetes.io/docs/user-guide/services/" rel="noreferrer">Services Documentation</a> gives a motivation for services which implies stable IP addresses, but I never see it explicitly stated:</p>
<blockquote>
<p>While each Pod gets its own IP address, <strong>even those IP addresses cannot be relied upon to be stable over time</strong>. This leads to a problem: if some set of Pods (let’s call them backends) provides functionality to other Pods (let’s call them frontends) inside the Kubernetes cluster, how do those frontends find out and keep track of which backends are in that set?</p>
<p>Enter Services.</p>
</blockquote>
<p>My pod spec for reference:</p>
<pre><code>kind: Pod
apiVersion: v1
metadata:
  name: fetchdataiso 
  labels:
    name: fetchdataiso
spec:
  containers:
    - name: fetchdataiso 
      image: 192.111.1.11:5000/ncllc/fetch_data
      command: [&quot;python&quot;]
      args: [&quot;feed/fetch_data.py&quot;, &quot;-hf&quot;, &quot;10.222.222.51&quot;, &quot;-pf&quot;, &quot;8880&quot;, &quot;-hi&quot;, &quot;10.223.222.173&quot;, &quot;-pi&quot;,&quot;9101&quot;]
</code></pre>

<p>The short answer is "Yes, the service IP can change"</p>

<pre><code>$ kubectl apply -f test.svc.yml
service "test" created

$ kubectl get svc
NAME         CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes   10.12.0.1       &lt;none&gt;        443/TCP   10d
test         10.12.172.156   &lt;none&gt;        80/TCP    6s

$ kubectl delete svc test
service "test" deleted

$ kubectl apply -f test.svc.yml
service "test" created

$ kubectl get svc
NAME         CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes   10.12.0.1       &lt;none&gt;        443/TCP   10d
test         10.12.254.241   &lt;none&gt;        80/TCP    3s
</code></pre>

<p>The long answer is that if you use it right, you will have no problem with it. What is even more important in scope of your question is that ENV variables are way worse then DNS/IP coupling.
You should refer to your service by <strong>service</strong> or <strong>service.namespace</strong> or even full path like something along the lines of <strong>test.default.svc.cluster.local</strong>. This will get resolved to service ClusterIP, and in opposite to your ENVs it can get re-resolved to a new IP (which will probably never happen unless you explicitly delete and recreate service) while ENV of a running process will not be changed</p>
