How to use env values to Job?

2/20/2021

Hello I am trying to passing a env values to a job in order to do this I use the following kubernetes template:

apiVersion: batch/v1
kind: Job
metadata:
  name: socksdownloader
spec:
  template:
    spec:
      containers:
      - name: socksdownloader
        image: socksdownloader:0.0.1
   #     env:
   #     - name: REDIS_HOST
   #       value: redis
   #     - name: REDIS_PORT
   #       value: 6379
   #     - name: REDIS_DB
   #       value: 0
   #     - name: REDIS_KEY
   #       value: "SOCK:"
        command: ["python",  "src/main.py"]
      restartPolicy: Never
  backoffLimit: 4

If I uncoment the env entries of the yml I got the following error:

The request is invalid: patch: Invalid value: "map[metadata:map[annotations:map[kubectl.kubernetes.io/last-applied-configuration:{\"apiVersion\":\"batch/v1\",\"kind\":\"Job\",\"metadata\":{\"annotations\":{},\"name\":\"socksdownloader\",\"namespace\":\"default\"},\"spec\":{\"backoffLimit\":4,\"template\":{\"spec\":{\"containers\":[{\"command\":\"python\",\"src/main.py\",\"env\":{\"name\":\"REDIS_HOST\",\"value\":\"redis\"},{\"name\":\"REDIS_PORT\",\"value\":6379},{\"name\":\"REDIS_DB\",\"value\":0},{\"name\":\"REDIS_KEY\",\"value\":\"SOCK:\"},\"image\":\"socksdownloader:0.0.1\",\"name\":\"socksdownloader\"}],\"restartPolicy\":\"Never\"}}}}\n]] spec:map[template:map[spec:map[]]]]": cannot convert int64 to string

The question is how can I pass that values to a Job in order to that can get connection to redis.

Thanks

-- Tlaloc-ES
kubernetes

2 Answers

2/20/2021

Kubernetes' envvar specification requires environment variable values to be coerced as strings, so integers need to be passed through quote.

Use quotes with the integer values:

- name: REDIS_PORT
  value: "6379"
- name: REDIS_DB
  value: "0"
-- Krishna Chaurasia
Source: StackOverflow

2/20/2021

For the port it needs to be value: "6379" and similar for the DB. YAML automatically guesses if something looks like a number but environment variables must be strings. Hence "cannot convert int64 to string".

-- coderanger
Source: StackOverflow