[Statefulset]Setting environment variables specific to each HOSTNAME loading from certain PATH mounted to container

1,736 views
Skip to first unread message

ilter P

unread,
Aug 17, 2018, 6:28:48 PM8/17/18
to Kubernetes user discussion and Q&A
Hi,

I have statefulset which has specific configuration for each host.
I created different Configmap with each hostname and mounted them under the path with the hostnames as following:
<STATIC_PATH>/$(HOSTNAME)/<STATIC_PATH

Now I need to pass this into a configuration file like:

Variable inside the conf file**

OUTSIDE://<VALUE_FROM_THE_MOUNTED_FILE>

I tried following options:

1- OUTSIDE://$(eval cat <PATH>) . --> did not work
2- exported another env variable from initContainer --> did not work

Is there a way to set environment variables by using a command as:
- name: ENV_VARIABLE
  valueFrom:
    command:
      - sh
      - export X=`cat <PATH>`

Please help

Thanks

Rodrigo Campos

unread,
Aug 17, 2018, 8:11:15 PM8/17/18
to kubernet...@googlegroups.com
Sorry, what is thing number 1 you tried? Is that in the yaml or where? Not sure what you mean

As a hack, can't you change your docker file to do on startup (CMD): export var=$(...); ./your-app ? Does it work?

Probably, he simplest thing might be for the app to read the proper variable using it's host name. I think that would be the best, bit you can try the hack above in the meantime or to experiment :-)
--
You received this message because you are subscribed to the Google Groups "Kubernetes user discussion and Q&A" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kubernetes-users+unsubscribe@googlegroups.com.
To post to this group, send email to kubernetes-users@googlegroups.com.
Visit this group at https://groups.google.com/group/kubernetes-users.
For more options, visit https://groups.google.com/d/optout.

ilter P

unread,
Aug 17, 2018, 9:07:30 PM8/17/18
to Kubernetes user discussion and Q&A
Hi Rodrigo,

Thanks for the reply
First of all this is stable external image and I dont want to maintain my version.
Secondly, the values that I would like to get are created during my deployment (ingress external host urls) and I can only map them during container initializing.

What i tried in 1st option is trying to create environment variable with the value of executing the command $(eval cat <PATH>)

Here is the block I am trying in statefulset
- name: KAFKA_ADVERTISED_LISTENERS
value: INSIDE://$(POD_IP):9092,OUTSIDE://$(eval cat /opt/conf/ext-url):9093


And ext-url is mounted via dynamic configMaps
- name: cm-vol
        configMap:
          name: kafka-0
          items:
          - key: kafka-ext-host
            path: ext-host

Note: I went to this direction after observing that reading from dynamic configMap is not working
valueFrom:
            configMapKeyRef:
              key: kafka-ext-host
             name: $(POD_NAME) . --> Kubernetes giving error to here

So the question is that, can I use "cat <PATH>" command to environment variable ?

I hope it is little bit clear now :) Sorry for the mess

Regards,
To unsubscribe from this group and stop receiving emails from it, send an email to kubernetes-use...@googlegroups.com.
To post to this group, send email to kubernet...@googlegroups.com.

Rodrigo Campos

unread,
Aug 17, 2018, 10:11:44 PM8/17/18
to kubernet...@googlegroups.com
On Friday, August 17, 2018, ilter P <ilterp...@gmail.com> wrote:
Hi Rodrigo,

Thanks for the reply
First of all this is stable external image and I dont want to maintain my version.
Secondly, the values that I would like to get are created during my deployment (ingress external host urls) and I can only map them during container initializing.

What i tried in 1st option is trying to create environment variable with the value of executing the command $(eval cat <PATH>)

Here is the block I am trying in statefulset
- name: KAFKA_ADVERTISED_LISTENERS
value: INSIDE://$(POD_IP):9092,OUTSIDE://$(eval cat /opt/conf/ext-url):9093


So, it's the $(pod_ip) part working?
If so, why not have the file /opt/conf/ext-url be an env var? Is that file coming from a configMap volume? Or how is it created?
 
And ext-url is mounted via dynamic configMaps
- name: cm-vol
        configMap:
          name: kafka-0
          items:
          - key: kafka-ext-host
            path: ext-host

Why not use an env variable from the configMap? If that is what you want in an env var, you can just have it. Instead of using it as a volume, use it as an env var.

What am I missing?
 

Note: I went to this direction after observing that reading from dynamic configMap is not working
valueFrom:
            configMapKeyRef:
              key: kafka-ext-host
             name: $(POD_NAME) . --> Kubernetes giving error to here

Yeah, that won't work
 

So the question is that, can I use "cat <PATH>" command to environment variable ?

Not as part of the yaml. But if you have it mounted as the item you want, you may just have it as an env var.

And if it's not the case, the hack I mentioned in my previous mail I think should work.
 

I hope it is little bit clear now :) Sorry for the mess

Thanks!

Niranjan Kolly

unread,
Aug 19, 2018, 3:11:34 AM8/19/18
to kubernet...@googlegroups.com
Use hostpath directory mount if you have more than one config file for each env.. and use  node  antiaffinity.


Thanks,
Niranjan

--

ilter P

unread,
Aug 20, 2018, 5:30:57 PM8/20/18
to Kubernetes user discussion and Q&A
Hi,

Thanks for the reply..
I managed to do it as per Rodrigo's recommendation

1- I loaded the configmaps into different paths by using HostName
2- I read the values from the paths before starting the image as following:

- command:

    - sh

    - -exc

    - |

      export KAFKA_EXT_HOST=`cat /opt/data/shared/$HOSTNAME` && \

      export KAFKA_ADVERTISED_LISTENERS=INSIDE://$(POD_IP):9092,OUTSIDE://$KAFKA_EXT_HOST:9094 && \

      start-kafka.sh



Thanks again and have a good day

Reply all
Reply to author
Forward
0 new messages