Adding arrays as Kubernetes environment variables

5/23/2018

I'm working on a Java Play project, and in my application.conf file I have a Redis cluster set-up that receives an array of Redis server nodes.

Now, I want to inject that value in Kubernetes deployment as an environment variable and can't find the right syntax to do so.

My current application.conf looks something like this:

play.cache.redis {
  # enable cluster mode
  source: cluster

  # nodes are defined as a sequence of objects:
  cluster:  [
    {
      # required string, defining a host the node is running on
      host:        localhost
      # required integer, defining a port the node is running on
      port:        6379
      # optional string, defines a password to use
      password:    null
    }
  ]
}

Can someone please tell me how to pass the play.cache.redis.cluster variable to a Kubernetes deployment so it stays like this?

-- Jonathan Perry
java
kubernetes
playframework
redis

2 Answers

5/24/2018

You can inject your entire application.conf with a mechanism of ConfigMaps:

apiVersion: v1
kind: ConfigMap
metatada:
  name: app-config
data:
  application.conf: |
    play.cache.redis {
      # enable cluster mode
      source: cluster
      # nodes are defined as a sequence of objects:
      cluster:  [
        {
          # required string, defining a host the node is running on
          host:        localhost
          # required integer, defining a port the node is running on
          port:        6379
          # optional string, defines a password to use
          password:    null
        }
      ]
    }

and then mount it directly to your container:

apiVersion: v1
kind: Pod
metadata:
  name: ....
spec:
  containers:
    - name: ...
      image: ...
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: app-config

The app can access it then at /etc/config/application.conf.

-- Alexandr Lurye
Source: StackOverflow

5/24/2018

You can define any env variable into a pod. For this, you need to update your spec section in deployment:

Here is an example:

spec:
  containers:
  - name: container-name
    image: gcr.io/google-samples/node-hello:1.0
    env:
    - name: VAR_NAME
      value: "any var value"
    - name: VAR_NAME2
      value: "another value"
-- Nick Rak
Source: StackOverflow