Introduction
What we will cover in this article?
- What is ConfigMap?
- Redis master with a ConfigMap
- Creating a ConfigMap from a file
- Creating a ConfigMap from a YAML file
- Using a ConfigMap to read in configuration data
Prerequisites
In the previous article of Application Deployment on AKS part 1, you examined the Redis master deployment you created. You saw how a deployment relates to a ReplicaSet and how a ReplicaSet relates to Pods. In this part 2, you will recreate this Redis master with an environment-specific configuration provided via a ConfigMap. But first, let’s explore what a ConfigMap is.
What is ConfigMap?
You will also edit the sample applications to provide configuration details using a ConfigMap. A ConfigMap is an object that is used to provide configuration details to Pods. It allows you to keep configuration settings outside of the actual container. You can then provide these configuration details to your application by connecting the ConfigMap to your deployment.
Redis master with a ConfigMap
There was nothing wrong with the previous deployment. In practical use cases, it would be rare that you would launch an application without some configuration settings. In this case, we are going to set the configuration settings for redis-master using a ConfigMap. A ConfigMap is a portable way of configuring containers without having specialized images for each configuration. It has a key-value pair for data that needs to be set on a container. A ConfigMap is used for non-secret configuration. Kubernetes has a separate object called a Secret. A Secret is used for configurations that contain critical data such as passwords.
In this example, we are going to create a ConfigMap. In this ConfigMap, we will configure redis-config as the key and the value will be.
maxmemory: "2mb"
maxmemory-policy: "allkeys-lru"
Now, let's create this ConfigMap. There are two ways to create a ConfigMap.
- Creating a ConfigMap from a file
- Creating a ConfigMap from a YAML file
We will explore each one in detail.
Creating a ConfigMap from a file
The following steps will help us create a ConfigMap from a file:
Step 1. Open the Azure Cloud Shell code editor by typing code redis-config in the terminal. Copy and paste the following two lines and save it as redis-config.
maxmemory: "2mb"
maxmemory-policy: "allkeys-lru"
Step 2. Now you can create the ConfigMap using the following code.
kubectl create configmap example-redis-config --from-file=redis-config
You should get an output as shown below
configmap/example-redis-config created
Step 3. You can use the same command to describe this ConfigMap.
kubectl describe configmap/example-redis-config
The output will be as shown in the screenshot below.
Creating a ConfigMap from a YAML file
In this section, you will recreate the ConfigMap from the previous section using a YAML file.
Step 1. To start, delete the previously created ConfigMap.
kubectl delete configmap/example-redis-config
Step 2. Copy and paste the following lines into a file named example-redis-config.yaml, and then save the file.
apiVersion: v1
data:
redis-config: |-
maxmemory 2mb
maxmemory-policy allkeys-lru
kind: ConfigMap
metadata:
name: example-redis-config
namespace: default
Step 3. You can now recreate your ConfigMap via the following command.
kubectl create -f example-redis-config.yaml
You should get an output as shown below.
configmap/example-redis-config created
Step 5. This command returns the same output as the previous one.
As you can see, using a YAML file, you were able to create the same ConfigMap.
Note. kubectl get has the useful option -o, which can be used to get the output of an object in either YAML or JSON. This is very useful in cases where you have made manual changes to a system and want to see the resulting object in YAML format. You can get the current ConfigMap in YAML using the following command.
kubectl get -o yaml configmap/example-redis-config
Now that you have the ConfigMap defined, let's use it.
Using a ConfigMap to read configuration data
In this section, you will reconfigure the redis-master deployment to read the configuration from the ConfgMap.
Step 1. To start, modify redis-master-deployment.yaml to use the ConfigMap as follows. The changes you need to make will be explained after the source code.
Note. If you downloaded the source code from GitHub, there is a file in the deployment folder: Application deployment on AKS, called redis-master-deployment_ Modified.yaml, which has the necessary changes applied to it.
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: redis-master
labels:
app: redis
spec:
selector:
matchLabels:
app: redis
role: master
tier: backend
replicas: 1
template:
metadata:
labels:
app: redis
role: master
tier: backend
spec:
containers:
- name: master
image: k8s.gcr.io/redis:e2e
command:
- redis-server
- "/redis-master/redis.conf"
env:
- name: MASTER
value: "true"
volumeMounts:
- mountPath: /redis-master
name: config
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 6379
volumes:
- name: config
configMap:
name: example-redis-config
items:
- key: redis-config
path: redis.conf
Let's dive deeper into the code to understand the different sections.
Lines 24-26
These lines introduce a command that will be executed when your Pod starts. In this case, this will start the redis-server pointing to a specific configuration file.
command:
- redis-server
- "/redis-master/redis.conf"
Lines 27-29
Shows how to pass configuration data to your running container. This method uses environment variables. In Docker form, this would be equivalent to docker run -e "MASTER=true". --name master -p 6379:6379 -m 100M -c 100m -d Kubernetes /redis:v1. This sets the environment variable MASTER to true. Your application can read the environment variable settings for its configuration.
env:
- name: MASTER
value: "true"
Lines 30-32
These lines mount the volume called config (this volume is defined in lines 39-45) on the /redis-master path on the running container. It will hide whatever exists on /redis-master on the original container. In Docker terms, it would be equivalent to docker run -v config:/redis-master. -e "MASTER=TRUE" --name master -p 6379:6379 -m 100M -c 100m -d Kubernetes / redis:v1.
volumeMounts:
- mountPath: /redis-master
name: config
Line 40
Gives the volume the name config. This name will be used within the context of this Pod.
name: config
Lines 41-42
Declare that this volume should be loaded from the example-redis config ConfigMap. This ConfigMap should already exist in the system. You have already defined this, so you are good.
configMap:
name: example-redis-config
Lines 43-45
Here, you are loading the value of the redis-config key (the two-line maxmemory settings) as a redis.conf file.
items:
- key: redis-config
path: redis.conf
Step 2. Let's create this updated deployment.
kubectl create -f redis-master-deployment_Modified.yml
This should output the following.
deployment.apps/redis-master created
Step 3. Let's now make sure that the configuration was successfully applied. First, get the Pod's name.
kubectl get pods
Step 4. Then exec into the Pod and verify that the settings were applied.
kubectl exec -it redis-master-<pod-id> redis-cli
127.0.0.1:6379> CONFIG GET maxmemory
1) "maxmemory"
2) "2097152"
127.0.0.1:6379> CONFIG GET maxmemory-policy
"maxmemory-policy"
"allkeys-lru"
127.0.0.1:6379> exit
To summarize, you have just performed an important and tricky part of configuring cloud-native applications. You will have also noticed that the apps have to be configured to read config dynamically. After you set up your app with configuration, you accessed a running container to verify the running configuration.
In this part 2, you configured the Redis Master to load configuration data from a ConfigMap. In the next final part 3, we will deploy the end-to-end application.