A well designed application is likely to have configurations which vary between deployments. Kubernetes makes it easy to override configurations specified in images.
In this guide you will:
- Create a container with the redis image
- Change a variable using the CLI (ephemeral state)
- Add a Config File to change a variable (with persistent state)
- Create an Environment Variable
Example Container Setup
Let's go through an example of configuring Redis. Our goal is to change the
maxmemory parameter. (Imagine that you need a larger amount of memory for production vs dev)
Create a new Application
Create a new Component
Set the image to "redis"
To understand why configuration should be setup outside the container itself, let's first try something that doesn't work: configuring the application right in the container.
Click the Shell icon to open a shell attached to our redis container. In the shell, enter:
Verify that the config has been correctly set
Everything seem to be working. However, let's try deleting the pod.
Go back to your application Components and click on the Delete icon
As soon as the pod is deleted, Kubernetes will schedule a new one to be deployed, automatically maintaining the desired state of 1 pod running.
After the new pod is created, click on the Shell icon next to it.
maxmemory setting via
So what happened to our setting? It was lost when the pod was destroyed and recreated. Since pods and containers are ephemeral, we can't store configuration states within the pod.
Adding a Config File
The correct way to configure containers is via environment variables and configuration files. Redis happens to use config files.
Go to Components, click on Edit
In the Config Files section click New File
For Mount Path, type
Click on the Edit Icon
A modal editor will pop up, inside the editor, enter our desired config
We've just created a config file. Now we need to override the startup command to tell redis to use it.
In the Command input box, type
Click Update Component
After the update is complete, open the shell again and type
Our configuration is picked up, this time via the configuration pod.
Now if we delete the pod again(go ahead and try it), the configuration will still be there.
Other Applications may use environment variables instead of config files. To add an environment variable, click New Variable in the Environment Variables section of the Edit Component Form:
For this example, you can enter a Name of
MY_CUSTOM_SETTING and a Value of
Click Update Component to apply this change
To verify that the Environment Variable has been set, open a shell and type
You should see the value set above(42) as the output