Tom Hadlaw
Tom Hadlaw

Reputation: 133

Kubernetes environment variable not being correctly used in command

I'm trying to bootstrap a etcd cluster within my kubernetes cluster, here is the relevant section of the pod definition

  - name: etcd
    image: quay.io/coreos/etcd:v2.2.0
    ports:
    - containerPort: 2379
    - containerPort: 2380
    - containerPort: 4001
    env:
    - name: POD_IP
      valueFrom:
        fieldRef:
          fieldPath: status.podIP
    args:
    - "-name"
    - "etcd0"
    - "-advertise-client-urls"
    - http://${POD_IP}:2379,http://${POD_IP}:4001
    - "-initial-advertise-peer-urls"
    - "http://${POD_IP}:2380"
    - "-listen-peer-urls"
    - "http://0.0.0.0:2380"
    - "-initial-cluster"
    - 'etcd0=http://${POD_IP}:2380'
    - "-initial-cluster-state"
    - "new"

However when I apply the POD_IP environment variable seems to get mangled, evidenced by the log:

 advertise URLs of "etcd0" do not match in --initial-advertise-peer-urls [http://$%7BPOD_IP%7D:2380] and --initial-cluster [http://$%7BPOD_IP%7D:2380]

Has anyone seen anything similar to this?

Upvotes: 3

Views: 1315

Answers (2)

Jordan Liggitt
Jordan Liggitt

Reputation: 18111

The arguments are not interpreted by a shell, so curly braces don't get you the behavior you want. If you want to use an envvar value in an arg, variable references like $(VAR_NAME) are expanded using the container's environment.

Upvotes: 6

Ijaz Ahmad
Ijaz Ahmad

Reputation: 12100

Init containers use cases:

https://kubernetes.io/docs/concepts/workloads/pods/init-containers/

Place values into a configuration file and run a template tool to dynamically generate a configuration file for the main app Container. For example, place the POD_IP value in a configuration and generate the main app configuration file using Jinja

Upvotes: 1

Related Questions