Kubernetes Configmap with Helm Chart


In this article, we explore the integration of ConfigMap, a key Kubernetes resource for managing configuration data, within a Helm chart. The Helm chart includes deployment, service, and ingress manifests, and our goal is to enhance the flexibility and customization of our deployment using ConfigMap | Kubernetes Configmap with Helm Chart.

Prerequisites

  • AWS Account with Ubuntu 22.04 LTS EC2 Instance
  • Minikube and kubectl Installed

Install Minikube and kubectl by following the official documentation for your operating system:

Minikube Installation Guide

Install Minikube on Ubuntu 22.04 LTS

  • Helm Installed:

Install Helm by following the official documentation:

Helm Installation Guide

How to use configmap in Kubernetes with Helm Chart

We begin by creating a dedicated configmap.yaml file within the Helm chart’s “templates” folder. This ConfigMap contains HTML content that serves as a custom index page for an Nginx deployment. Subsequently, we modify the deployment manifest to incorporate this ConfigMap as a volume, ensuring the dynamic injection of configuration data into the Nginx container.

As part of the Helm chart upgrade process, we witness the seamless transition from the default Nginx welcome page to our custom content sourced from the ConfigMap. Additionally, we delve into the use of Go template syntax within the configmap.yaml file, enabling parameterization and enhanced template rendering.

Create a helm chart

Firstly, let’s create a Helm chart

helm create demo
Kubernetes Configmap with Helm Chart 1

open the chart

cd demo
Kubernetes Configmap with Helm Chart 2

run the following command to see the files in it.

ls
Kubernetes Configmap with Helm Chart 3

exit the directory

cd

now let’s deploy the chart

helm install mychart demo
Kubernetes Configmap with Helm Chart 4

now lets see the pod deployment

kubectl get pods
Kubernetes Configmap with Helm Chart 5

get the services

kubectl get svc
Kubernetes Configmap with Helm Chart 6

Use port-forward to forward the local port to service.

kubectl port-forward --address 0.0.0.0 svc/mychart-demo 8888:80
Kubernetes Configmap with Helm Chart 7

Check browser. Then you’ll see Nginx welcome page.

To access the application on Browser write the ip address:port number in url.

ip address is the public ip address of your Minikube EC2 instance created on AWS and port number which is 8888 which we have used in forwarding mychart-demo pod.

Kubernetes Configmap with Helm Chart 8

Create Configmap in Helm Chart

Firstly, we will create a configMap.yaml file in templates folder with the configuration given below.

Enter the demo and templates directory

cd demo/templates
Kubernetes Configmap with Helm Chart 9

create a configMap.yaml here in templates directory.

nano configMap.yaml
Kubernetes Configmap with Helm Chart 10

now add the following content into it.

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-configmap
data:
  index.html: 
    <h1>Welcome to the Helm !!!</h1>
Kubernetes Configmap with Helm Chart 11

And mount this configMap volume to deployment

nano deployment.yaml
Kubernetes Configmap with Helm Chart 12
    volumeMounts:
      - name: nginx
        mountPath: /usr/share/nginx/html
volumes:
  - name: nginx
    configMap:
      name: nginx-configmap
Kubernetes Configmap with Helm Chart 13

exit the directories

cd

Then upgrade the chart

helm upgrade mychart demo
Kubernetes Configmap with Helm Chart 14

Use port-forward to forward the local port to service.

kubectl port-forward --address 0.0.0.0 svc/mychart-demo 8888:80
Kubernetes Configmap with Helm Chart 15

And check browser again, you can see the welcome page was changed to our custom page.

Kubernetes Configmap with Helm Chart 16

Using Go Template syntax

Now again open the demo and templates directory

cd demo/templates
Kubernetes Configmap with Helm Chart 17

open the configMap.yaml

nano  configMap.yaml
Kubernetes Configmap with Helm Chart 18

change the index.html

apiVersion: v1
kind: ConfigMap
metadata: 
  name: nginx-configmap
data:
  index.html: {{ .Values.pageContent }}
Kubernetes Configmap with Helm Chart 19

Now this time, Helm will render the message from the values.yaml, so let’s add the parameter in values.yaml file.

first exit the templates directory.

cd ..
Kubernetes Configmap with Helm Chart 20

open the values.yaml file

nano values.yaml
Kubernetes Configmap with Helm Chart 21

add the following content into it.

pageContent: |
  <h1> Helm Template using Go syntax </h1>
Kubernetes Configmap with Helm Chart 22

exit the directory

cd

Upgrade it and check it out with browser

helm upgrade mychart demo
Kubernetes Configmap with Helm Chart 23

Use port-forward to forward the local port to service.

kubectl port-forward --address 0.0.0.0 svc/mychart-demo 8888:80
Kubernetes Configmap with Helm Chart 24

And check browser again,

Kubernetes Configmap with Helm Chart 25

Accessing file inside templates

Now again open the demo and templates directory

cd demo

Create a static directory and open it.

mkdir static
cd static
Kubernetes Configmap with Helm Chart 26

inside it create a index.html file and the following content into it

nano index.html
Kubernetes Configmap with Helm Chart 27
<h1> Helm is accessing files ! </h1>
Kubernetes Configmap with Helm Chart 28

exit the static directory

cd ..

open the templates directory

cd templates

Modify configMap.yaml.

nano configMap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-configmap
data:
  {{- (.Files.Glob "static/*").AsConfig | nindent 2 }}
Kubernetes Configmap with Helm Chart 29

exit the directory

cd

Upgrade it

helm upgrade mychart demo
Kubernetes Configmap with Helm Chart 30

use kubectl port forward

kubectl port-forward --address 0.0.0.0 svc/mychart-demo 8888:80

check it out with browser again.

Kubernetes Configmap with Helm Chart 31

You can view the revisions created of your helm chart

helm history mychart 
Kubernetes Configmap with Helm Chart 32

Let’s try to rollback the chart to the first revision.

helm rollback mychart 1
Kubernetes Configmap with Helm Chart 33

Then you’ll see Nginx welcome page come back and add another revision into the history as well.

Kubernetes Configmap with Helm Chart 34
helm history mychart 
Kubernetes Configmap with Helm Chart 35

Conclusion:

In summary, this comprehensive guide takes you through the steps of creating a Helm chart, integrating configMap for enhanced configuration management, utilizing Go template syntax, accessing files within templates, and managing chart revisions efficiently.

Reference:

Kubernetes configmap with helm chart official page

Prasad Hole

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap