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.
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.