Its possible to set a var for a pod using a command that has to run inside of that pod?

5/24/2019

Its possible to set a var for a pod using a command that has to run inside of that pod?

what i am trying to do is get private and public ip from the node, so i can use them into the app on the allowed host, i can get the private ip using this:

containers:
- env:
  - name: NODE_IP
    valueFrom:
      fieldRef:
         status.hostIP

but only that private ip, and i need the public, my idea is to get them from here:

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-instance-addressing.html with this 2 commands:

curl http://169.254.169.254/latest/meta-data/local-ipv4
curl http://169.254.169.254/latest/meta-data/public-ipv4

doing inside the container will be something like this:

command: MYPRIVATEIP=$(curl http://169.254.169.254/latest/meta-data/local-ipv4)
command: MYPUBLICIP=$(curl http://169.254.169.254/latest/meta-data/public-ipv4)

containers:
- env:
  - name: NODE_IP
    valueFrom:
      fieldRef:
         status.hostIP

i already try to put this like this:

    env:
    - name: MYIP
      value: $(curl http://169.254.169.254/latest/meta-data/local-ipv4)

but this doesnt make anything but putting this like plane text.

what i expect is to have that command output inside of the var so my app can pickup from there and add into the allowed host. thanks in advance for any help! regards

-- Lucas Carrizo
kubernetes
pod

1 Answer

5/25/2019

I think you can use lifecycle events to achieve this. You can read about them here. https://kubernetes.io/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/.

So I am suggesting you to create a shell script init.sh to export all the environment variables you need and run it once the pod is started.

spec:
  containers:
  - name: container-name
    image: image-name
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", "/init.sh"]

You can include the init.sh file in the docker image when you build the image using COPY command.

-- Ambegodas
Source: StackOverflow