0

Kubernetes provides information about other services (to all running pods in the same namespace) with use of environment variables. For example:

  • "UIMANAGEMENT_V6_2_2_SERVICE_PORT_HTTP": "80"
  • "UIMANAGEMENT_V6_2_2_SERVICE_PORT_GRPC": "5111"
  • "UIMANAGEMENT_V6_2_2_SERVICE_PORT": "80"
  • "UIMANAGEMENT_V6_2_2_SERVICE_HOST": "10.110.7.1"
  • ...

Where UIMANAGEMENT_V6_2_2 is name of our service deployment.

We would like to use this data to implement simple service discovery, but I just figured out, that this information is not updated inside already running pods at runtime - when a new service is deployed in the cluster. Is this just a k8s configuration issue or the runtime update is not currently supported?

Thank you.

1

Updating environment variables on running pods isn't supported in Kubernetes.

However, if you deploy your pods into Kubernetes as deployment.apps, you can edit the environment variable on the deployment, which will cause Kubernetes to deploy new instances of you pod with the new environment variable's value, and terminate the older pods with the older variable value.

For example, if you deploy a pod using the Pod api like this:

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.14.2
    ports:
    - containerPort: 80
    env:
    - name: TEST_VAR
      value: "123"

You will not be able to kubectl edit pod nginx-pod to update the value set for TEST_VAR. If you ever want to update the TEST_VAR env variable, you'll need to delete the pod and recreate it with an updated value "manually".

You should instead deploy the pod using the Deployment.app api, like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
        env:
        - name: TEST_VAR
          value: "123"

Which will allow you do kubectl edit deployments.apps nginx-deployment, or even easier, kubectl set env deployment/nginx-deployment TEST_VAR="456", to update the TEST_VAR value. Kubernetes will delete and recreate the pods for you.

1
  • Thank you, but we don't want to restart all pods each time we deploy a new service... After some investigation, we will try to communication with kubernetes API to determine information about other deployments... – Sojmic Apr 1 '20 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.