How to pass Dynamic Environment Variable in Kubernetes

10/15/2020

I have two replica statefulset and I want to pass health URL of other pod(I mean in replica1 I want to know health URL of replica2 and vice versa), i did following

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: dapi
  labels:
    app: dapi
spec:
  serviceName: dapi
  selector:
    matchLabels:
      app: dapi
  template:
    metadata:
      labels:
        app: dapi
    spec:
      containers:
        - name: test-container
          image: busybox:latest
          imagePullPolicy: IfNotPresent
          command: [ "sh", "-c"]
          args:
          - while true; do
              echo -en '\n';
              printenv MY_POD_NAME MASTER_HEALTH_URL;
              sleep 10;
            done;
          env:
            - name: MY_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: MASTER_HEALTH_URL
              value: "http://$(MY_POD_NAME).dapi:8181/health/readiness"

In above code I am able to pass pod name, as its statueflset so it has predictable pod name so i tried following and its not working

value: "http://$(for i in 0 1; do echo dapi-$i;done | grep -v $MY_POD_NAME).dapi:8181/health/readiness"

So above is not working, is there any way I can pass this dynamic behavior?

PS: Above is sample manifest, for real application I don't have control on entrypoint.sh so cant over ride it.

-- ImranRazaKhan
environment-variables
kubernetes
kubernetes-pod

0 Answers