Specify an `int` value in Kubernetes ConfigMap?

3/22/2017

Is there a way to specify int values in ConfigMap? When I try to specify a port number I get the following error.

error: error validating "my-deployment.yml": error validating data: expected type int, for field spec.template.spec.containers[0].ports[0].containerPort, got string; if you choose to ignore these errors, turn validation off with --validate=false

Here is the sample config file I am using:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-service
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: poweramp-email-service
    spec:
      containers:
      - name: poweramp-email-service
        env:
          - name: SERVICE_PORT_NUM
            valueFrom:
              configMapKeyRef:
                name: stg-config
                key: poweramp-email-service.port
        image: nginx
        ports:
        - containerPort: my-service.port

And here is the simple stg.properties file I am using to generate the ConfigMap using the command: kubectl create configmap stg-config --from-file stg.properties

my-service.port=9513
-- toidiu
kubernetes

2 Answers

1/14/2019

I was facing a similar issue with setting port number as env variables. I have two services where ServiceB should connect to ServiceA. The error was

/opt/service/config.yml has an error: * Incorrect type of value at: serviceAPort; is of type: String, expected: Integer My config.yml contains something like this - serviceAPort: "${SERVICEA_PORT!'9091'}"

Kubernetes was creating an environment variable SERVICEA_PORT in a format which had Protocol:IP:port. This could not be parsed to an int by ServiceB

I realised this upon using the 'printenv' in ServiceB pod and overrode it with the following config

env:
  - name : SERVICEA_PORT
    value: 9091

Basically, it was a conflict because of env variables created by k8s out of the box.

-- Dilip Murupala
Source: StackOverflow

3/22/2017

You can't use configmap values in spec.template.spec.containers[0].ports[0].containerPort I'm afraid, it's for configuration values.

The options you can use it for (specified in this guide) are

  • As an environment variable
  • As a command line config flag (using environment variables)
  • Using the volume plugin.

If you want to make the port configurable for your deployment/pod, you might consider using Helm. Helm allows you to use Go templates in your manifests/definitions, and then you can override them upon invocation.

Take this MySQL Chart template as an example, you could set the port here as a config option like so:

ports:
  - name: mysql
  containerPort: {{ default 3306 .Values.mysqlPort }}

and then from there, set this value in your values.yaml

-- jaxxstorm
Source: StackOverflow