Azure

Software Deployment On Azure Kubernetes Service – Half Two

Introduction 

 

 

 

What we are going to cowl on this article:

  • What’s ConfigMap?
  • Redis grasp with a ConfigMap
  • Making a ConfigMap from a file
  • Making a ConfigMap from a YAML file
  • Utilizing a ConfigMap to learn in configuration knowledge

Conditions

Within the earlier article of Software Deployment on AKS half 1, you examined the Redis grasp deployment you created. You noticed how a deployment pertains to a ReplicaSet and the way a ReplicaSet pertains to Pods. On this half 2, you’ll recreate this Redis grasp with an environment-specific configuration supplied through a ConfigMap. However first, let’s discover what a ConfigMap is.

 

What’s ConfigMap?

 

Additionally, you will edit the pattern functions to offer configuration particulars utilizing a ConfigMap. A ConfigMap is an object that’s used to offer configuration particulars to Pods. It permits you to preserve configuration settings exterior of the particular container. You may then present these configuration particulars to your utility by connecting the ConfigMap to your deployment.

 

Redis grasp with a ConfigMap

 

There was nothing improper with the earlier deployment. In sensible use instances, it might be uncommon that you’d launch an utility with out some configuration settings. On this case, we’re going to set the configuration settings for redis-master utilizing a ConfigMap. A ConfigMap is a conveyable method of configuring containers with out having specialised photographs for every configuration. It has a key-value pair for knowledge that must be set on a container. A ConfigMap is used for non-secret configuration. Kubernetes has a separate object referred to as a Secret. A Secret is used for configurations that comprise vital knowledge resembling passwords.

 

On this instance, we’re going to create a ConfigMap. On this ConfigMap, we are going to configure redis-config as the important thing and the worth might be:

  1. maxmemory 2mb  
  2. maxmemory-policy allkeys-lru  

Now, let’s create this ConfigMap. There are two methods to create a ConfigMap:

  • Making a ConfigMap from a file
  • Making a ConfigMap from a YAML file 

We are going to discover every one intimately.

 

Making a ConfigMap from a file

 

The next steps will assist us create a ConfigMap from a file:

 

Step 1

 

Open the Azure Cloud Shell code editor by typing code redis-config within the terminal. Copy and paste the next two traces and put it aside as redis-config,

  1. maxmemory 2mb    
  2. maxmemory-policy allkeys-lru   

 

Step 2

 

Now you’ll be able to create the ConfigMap utilizing the next code:

  1. kubectl create configmap example-redis-config –from-file=redis-config  

It’s best to get an output as proven beneath:

  1. configmap/example-redis-config created  

 

Step 3

 

You need to use the identical command to explain this ConfigMap:

  1. kubectl describe configmap/example-redis-config  

The output might be as proven within the screenshot beneath:

 

 

Making a ConfigMap from a YAML file

 

On this part, you’ll recreate the ConfigMap from the earlier part utilizing a YAML file.

 

Step 1

 

To start out, delete the beforehand created ConfigMap:

  1. kubectl delete configmap/example-redis-config  

 

Step 2

 

Copy and paste the next traces right into a file named example-redis-config.yaml, after which save the file:

  1. apiVersion: v1  
  2. knowledge:  
  3.   redis-config: |-  
  4.  maxmemory 2mb  
  5.  maxmemory-policy allkeys-lru  
  6. sort: ConfigMap  
  7. metadata:  
  8.   title: example-redis-config  
  9.  namespace: default  

Step 3

 

Now you can recreate your ConfigMap through the next command:

  1. kubectl create -f example-redis-config.yaml  

It’s best to get an output as proven beneath:

  1. configmap/example-redis-config created  

 

Step 5

 

This command returns the identical output because the earlier one.

 

As you’ll be able to see, utilizing a YAML file, you had been in a position to create the identical ConfigMap.  

 

Notice

kubectl get has the helpful possibility -o, which can be utilized to get the output of an object in both YAML or JSON. That is very helpful in instances the place you could have made guide adjustments to a system and wish to see the ensuing object in YAML format. You may get the present ConfigMap in YAML utilizing the next command:

  1. kubectl get -o yaml configmap/example-redis-config  

 

Now that you’ve got the ConfigMap outlined, let’s use it.

 

Utilizing a ConfigMap to learn in configuration knowledge

 

On this part, you’ll reconfigure the redis-master deployment to learn configuration from the ConfgMap.

 

Step 1

 

To start out, modify redis-master-deployment.yaml to make use of the ConfigMap as follows. The adjustments you’ll want to make might be defined after the supply code.

 

Notice

When you downloaded the supply code from GitHub, there’s a file within the deployment folder: Software deployment on AKS, referred to as redis-master-deployment_ Modified.yaml, which has the required adjustments utilized to it.

  1. apiVersion: apps/v1 # for variations earlier than 1.9.0 use apps/v1beta2  
  2. sort: Deployment  
  3. metadata:  
  4.   title: redis-master  
  5.   labels:  
  6.     app: redis  
  7. spec:  
  8.   selector:  
  9.     matchLabels:  
  10.       app: redis  
  11.       function: grasp  
  12.       tier: backend  
  13.   replicas: 1  
  14.   template:  
  15.     metadata:  
  16.       labels:  
  17.         app: redis  
  18.         function: grasp  
  19.         tier: backend  
  20.     spec:  
  21.       containers:  
  22.       – title: grasp  
  23.         picture: k8s.gcr.io/redis:e2e  
  24.         command:  
  25.         – redis-server  
  26.         – “/redis-master/redis.conf”  
  27.         env:  
  28.         – title: MASTER  
  29.           worth: “true”  
  30.         volumeMounts:  
  31.         – mountPath: /redis-master  
  32.           title: config  
  33.         assets:  
  34.           requests:  
  35.             cpu: 100m  
  36.             reminiscence: 100Mi  
  37.         ports:  
  38.         – containerPort: 6379  
  39.       volumes:  
  40.         – title: config  
  41.           configMap:  
  42.             title: example-redis-config  
  43.             objects:  
  44.             – key: redis-config  
  45.               path: redis.conf  

Let’s dive deeper into the code to grasp the completely different sections,

 

Strains 24-26

 

These traces introduce a command that might be executed when your Pod begins. On this case, this can begin the redis-server pointing to a particular configuration file.

  1. command:    
  2.         – redis-server    
  3.         – “/redis-master/redis.conf”   

Strains 27-29

 

Exhibits the right way to move configuration knowledge to your operating container. This technique makes use of surroundings variables. In Docker kind, this could be equal to docker run -e “MASTER=true”. –name grasp -p 6379:6379 -m 100M -c 100m -d Kubernetes /redis:v1. This units the surroundings variable MASTER to true. Your utility can learn the surroundings variable settings for its configuration.

  1. env:    
  2.         – title: MASTER    
  3.           worth: “true”   

Strains 30-32

 

These traces mount the amount referred to as config (this quantity is outlined in traces 39-45) on the /redis-master path on the operating container. It can disguise no matter exists on /redis-master on the unique container. In Docker phrases, it might be equal to docker run -v config:/redis-master. -e “MASTER=TRUE” –name grasp -p 6379:6379 -m 100M -c 100m -d Kubernetes / redis:v1.

  1. volumeMounts:    
  2.         – mountPath: /redis-master    
  3.           title: config  

Line 40

 

Offers the amount the title config. This title might be used throughout the context of this Pod. 
Strains 41-42

 

Declare that this quantity ought to be loaded from the example-redis config ConfigMap. This ConfigMap ought to exist already within the system. You may have already outlined this, so you’re good. 

  1. configMap:    
  2.             title: example-redis-config  

Strains 43-45

 

Right here, you’re loading the worth of the redis-config key (the two-line maxmemory settings) as a redis.conf file. 
  1. objects:    
  2.             – key: redis-config    
  3.               path: redis.conf  

Step 2

 

Let’s create this up to date deployment:

  1. kubectl create -f redis-master-deployment_Modified.yml  

This could output the next:

  1. deployment.apps/redis-master created  
 

 

Step 3

 

Let’s now be sure that the configuration was efficiently utilized. First, get the Pod’s title:

 

 

Step 4

 

Then exec into the Pod and confirm that the settings had been utilized:

  1. kubectl exec -it redis-master-<pod-id> redis-cli  
  2. 127.0.0.1:6379> CONFIG GET maxmemory  
  3.  1) “maxmemory” 2) “2097152”  
  4. 127.0.0.1:6379> CONFIG GET maxmemory-policy  
  5.  “maxmemory-policy”  
  6.  “allkeys-lru” 127.0.0.1:6379>exit  

 

To summarize, you could have simply carried out an necessary and difficult a part of configuring cloud-native functions. You should have additionally observed that the apps should be configured to learn config dynamically. After you arrange your app with configuration, you accessed a operating container to confirm the operating configuration. 

 

On this half 2, you configured the Redis Grasp to load configuration knowledge from a ConfigMap. Within the subsequent last half 3, we are going to deploy the end-to-end utility.

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button