<p>Say if I have a <code>rabbitmq</code> service as follows:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: my-rabbitmq
spec:
  ports:
  - port: 6379
  selector:
    app: my-rabbitmq
</code></pre>

<p>And I have another deployment:</p>

<pre><code>apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: A-worker
spec:
  replicas: 1
  containers:
  - name: a-worker
    image: worker-image
    ports:
    - containerPort: 80
    env:
    - name: rabbitmq_url
      value: XXXXXXXXXXXXX
</code></pre>

<p>Is there any way to set the service ip as environment variable in my second deployment by some kind of selector? In other words what should go to the <code>value: XXXXXXXXXX</code> in the second deployment yaml. (Note I know I can get the service ip by <code>kubectl get services</code>, but I'd like to know how to set this by the service name or label). Any advice is welcome!</p>

<p>Reading between the lines (and I hope this helps):</p>

<p>K8s automatically creates service environment variables for you inside each pod. See <a href="https://kubernetes.io/docs/concepts/services-networking/service/#environment-variables" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/service/#environment-variables</a> for details.</p>

<p>The other route is to enable kube dns, in which case one can contact a service IP simply by using the service name.</p>
