<p>I'm running a simple spring microservice project with Minikube. I have two projects: lucky-word-client (on port 8080) and lucky-word-server (on port 8888). lucky-word-client has to communicate with lucky-word-server. I want to inject the static Nodeport of lucky-word-server (<a href="http://192" rel="nofollow noreferrer">http://192</a>.<strong>*.</strong>.100:32002) as an environment variable in my Kuberenetes deployment script of lucky-word-client. How I could do?</p>

<p>This is deployment of lucky-word-server:</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: lucky-server
spec:
  selector:
      matchLabels:
        app: lucky-server
  replicas: 1
  template:
    metadata:
      labels:
        app: lucky-server
    spec:
      containers:
        - name: lucky-server
          image: lucky-server-img
          imagePullPolicy: Never
          ports:
            - containerPort: 8888
</code></pre>

<p>This is the service of lucky-word-server:</p>

<pre><code>kind: Service
apiVersion: v1
metadata:
  name: lucky-server
spec:
  selector:
    app: lucky-server
  ports:
  - protocol: TCP
    targetPort: 8888
    port: 80
    nodePort: 32002
  type: NodePort
</code></pre>

<p>This is the deployment of lucky-word-client:</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: lucky-client
spec:
  selector:
      matchLabels:
        app: lucky-client
  replicas: 1
  template:
    metadata:
      labels:
        app: lucky-client
    spec:
      containers:
        - name: lucky-client
          image: lucky-client-img
          imagePullPolicy: Never
          ports:
            - containerPort: 8080
</code></pre>

<p>This is the service of lucky-word-client:</p>

<pre><code>kind: Service
apiVersion: v1
metadata:
  name: lucky-client
spec:
  selector:
    app: lucky-client
  ports:
  - protocol: TCP
    targetPort: 8080
    port: 80
  type: NodePort
</code></pre>

Danny
<p>Kubernetes automatically injects services as environment variables. <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></p>
<p><strong>But you should not use this.</strong> This won't work unless all the services are in place when you create the pod. It is inspired by &quot;docker&quot; which also moved on to DNS based service discovery now. So &quot;environment based service discovery&quot; is a thing of the past.</p>
<p>Please rely on DNS service discovery. Minikube ships with <code>kube-dns</code> so you can just use the <code>lucky-server</code> hostname (or one of <code>lucky-server[.default[.svc[.cluster[.local]]]]</code> names). Read the documentation: <a href="https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/</a></p>
