Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding arrays as Kubernetes environment variables

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?

like image 249
Jonathan Perry Avatar asked May 23 '18 13:05

Jonathan Perry


People also ask

Can you have an array as an environment variable?

short answer: yes, you can!

How does Kubernetes set environment variables?

Environment Variables and Kubernetes There are two ways to define environment variables with Kubernetes: by setting them directly in a configuration file, from an external configuration file, using variables, or a secrets file.

How do I set environment variables in Kubernetes deployment?

To set environment variables, include the env or envFrom field in the configuration file. Note: The environment variables set using the env or envFrom field override any environment variables specified in the container image. Note: Environment variables may reference each other, however ordering is important.

How do I set environment variables in Azure Kubernetes?

To set environment variables when you start a container in the Azure portal, specify them in the Advanced page when you create the container. Under Environment variables, enter NumWords with a value of 5 for the first variable, and enter MinLength with a value of 8 for the second variable.


1 Answers

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.

like image 195
Alex Lurye Avatar answered Oct 11 '22 02:10

Alex Lurye