6

I am setting port value in an environment property while generating Pod yaml.

master $ kubectl run nginx --image=nginx --restart=Never --env=MY_PORT=8080 --dry-run -o yaml  > Pod.yaml

I am trying to use the environment property MY_PORT in the ports section of my Pod yaml.

spec:
     containers:
     - env:
       - name: MY_PORT
         value: "8080"
       image: nginx
       name: nginx
       ports:
       - containerPort: $(MY_PORT)

When i try to create the Pod i am getting following error message.

error: error validating "Pod.yaml": error validating data: ValidationError(Pod.spec.containers[0].ports[0].containerPort): invalid type for io.k8s.api.core.v1.ContainerPort.containerPort: got "string", expected "integer"; if you choose to ignore theseerrors, turn validation off with --validate=false

I tried referencing like ${MY_PORT} , MY_PORT etc.. but all the time same error as above.

How i can use an environment variable value in an integer field.

2
  • why do you want to do it? Commented May 3, 2020 at 7:23
  • @Arghya Sandhu No real life use case actually. I was trying this one as at times we can dynamically use some of the parameters from a configmap in our definition yaml , similarly i was trying to see if i can able to dynamically pick the port value from environment property. Commented May 3, 2020 at 13:10

1 Answer 1

6

You can't use an environment variable there. In the ContainerPort API object the containerPort field is specified as an integer. Variable substitution is only support in a couple of places, and where it does it is called out; see for example args and command in the higher-level Container API object.

There's no reason to make this configurable. In a Kubernetes environment the pod will have its own IP address, so there's no risk of conflict; if you want to use a different port number to connect, you can set up a service where e.g. port 80 on the service forwards to port 8080 in the pod. (In plain Docker, you can do a similar thing with a docker run -p 80:8080 option: you can always pick the external port even if the port number inside the container is fixed.) I'd delete the environment variable setting.

1
  • "no reason to make this configurable." - I'd like to give you one: If you could define a variable for this and pass it down from top into the container, You'd be sure the ports do match (I assume the container would error out with a good description if you don't pass in the correct env variables). The way it is now, you tend to duplicate the port number again and again: 1 in the implementation of the service, 2 in the docker file (EXPOSE), 3 in the pod template containerPort and 4 in the service. Mistake is unlikely, but if, would be way more pain than the nice error the container gives. Commented Mar 14 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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