Kubernetes (Minikube): environment variable

6/5/2018

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 (http://192.*..100:32002) as an environment variable in my Kuberenetes deployment script of lucky-word-client. How I could do?

This is deployment of lucky-word-server:

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

This is the service of lucky-word-server:

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

This is the deployment of lucky-word-client:

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

This is the service of lucky-word-client:

kind: Service
apiVersion: v1
metadata:
  name: lucky-client
spec:
  selector:
    app: lucky-client
  ports:
  - protocol: TCP
    targetPort: 8080
    port: 80
  type: NodePort
-- Danny
docker
kubernetes
microservices

1 Answer

6/5/2018

Kubernetes automatically injects services as environment variables. https://kubernetes.io/docs/concepts/services-networking/service/#environment-variables

But you should not use this. This won't work unless all the services are in place when you create the pod. It is inspired by "docker" which also moved on to DNS based service discovery now. So "environment based service discovery" is a thing of the past.

Please rely on DNS service discovery. Minikube ships with kube-dns so you can just use the lucky-server hostname (or one of lucky-server[.default[.svc[.cluster[.local]]]] names). Read the documentation: https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/

-- AhmetB - Google
Source: StackOverflow