Open In App

How To Update ConfigMap Or Secret Without Deleting One?

Last Updated : 05 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Kubernetes or K8s is an open-sourced container orchestration technology that is used for automating the manual processes of deploying, managing and scaling applications by the help of containers. Kubernetes was originally developed by engineers at Google and In 2015, it was donated to CNCF (Cloud Native Computing Foundation). In this tutorial we will see how we can update a ConfigMap or a Secret in a Kubernetes Cluster, without deleting the ConfigMap or the Secret.

Complete Guide To Update ConfigMap Or Secret Without Deleting One

Step 1: Creating a Kubernetes Cluster.

You can skip this step if you already have a Kubernetes Cluster running in your machine. But in case you don’t have a Cluster running enter the following command to create Kubernetes locally using Minikube:

minikube start

minikube

Minikube is a one-node Kubernetes cluster where master processes and work processes both run on one node.

Step 2. Create a Configmap file

Let’s Start with creating a configmap, in this tutorial we are putting ourselves into the shoes of a game developer org that is deploying there game in a local Kubernetes Cluster for development purposes. Create a file gfg-cm.yaml by entering the following command:

touch gfg-cm.yaml

and add the following YAML code to the file:

apiVersion: v1
kind: ConfigMap
metadata:
name: my-game
data:
# property-like keys; each key maps to a simple value
player_initial_lives: "3"
ui_properties_file_name: "user-interface.properties"

# file-like keys
game.properties: |
enemy.types=aliens,monsters
player.maximum-lives=5
user-interface.properties: |
color.good=purple
color.bad=yellow
allow.textmode=true

Step 3. Creating the Pod file

Now we will create a Pod named configmap-demo-pod that defines the basics of the game. Note that here this Pod will be getting values from the my-game configmap that we created in the previous step. Create a new file named gfg-depl.yaml by entering the following command:

touch gfg-depl.yaml

and now add the following YAML code to the file:

apiVersion: v1
kind: Pod
metadata:
name: configmap-demo-pod
spec:
containers:
- name: demo
image: alpine
command: ["sleep", "3600"]
env:
# Define the environment variable
- name: PLAYER_INITIAL_LIVES # Notice that the case is different here
# from the key name in the ConfigMap.
valueFrom:
configMapKeyRef:
name: game-demo # The ConfigMap this value comes from.
key: player_initial_lives # The key to fetch.
- name: UI_PROPERTIES_FILE_NAME
valueFrom:
configMapKeyRef:
name: game-demo
key: ui_properties_file_name
volumeMounts:
- name: config
mountPath: "/config"
readOnly: true
volumes:
# You set volumes at the Pod level, then mount them into containers inside that Pod
- name: config
configMap:
# Provide the name of the ConfigMap you want to mount.
name: game-demo
# An array of keys from the ConfigMap to create as files
items:
- key: "game.properties"
path: "game.properties"
- key: "user-interface.properties"
path: "user-interface.properties"


Step 4. Creating Pod and ConfigMap

Now we need to create the ConfigMap and Pod from the configuration files we just created in above steps. In order to create the ConfigMap, enter the following command:

kubectl apply -f gfg-cm.yaml


Similarly, to create the Pod, enter the following command:

kubectl apply -f gfg-depl.yaml


Step 5. Checking if the Pod and ConfigMap are running

Now let’s check if our ConfigMap and the Pod are running in the cluster. enter the following command to check the list of ConfigMaps in the cluster:

kubectl get cm


or

kubectl get configmaps


Get Config Maps

The ConfigMap is present. Similarly we can check the list of Pods running in the cluster using the following command:

Kubectl get pods

Step 6. Editing the ConfigMap

Now let’s imagine a scenario where our users gave us a feedback that the maximum lives per player should be 10 and the player should only get 1 initial player life. Other than this they also suggested changing the enemies from aliens and monsters to dragons and dinosaurs. So now lets change our ConfigMap but making sure that the previous one is not deleted and the Pod doesn’t crashes since our user will not be able to play the game then. To Edit a Configmap enter the following command:

kubectl edit cm/my-game


Kubectl yaml

Now we can edit the configmap as we desire for me, I am changing the maximum-lives=10 and the player should only get 1 initial player life, so I am changing the value of player_initial_lives to 1. Other than this I am also changing the enemies from aliens and monsters to dragons and dinosaurs i.e. enemy.types=dragons,dinosaurs.

The final configmap would have the following configuration:{“apiVersion”:”v1″,”data”:{“game.properties”:”enemy.types=dragons,dinosaurs\nplayer.maximum-lives=10 \n”,”player_initial_lives”:”1″,”ui_properties_file_name”:”user-interface.properties”,”user-interface.properties”:”color.good=purple\ncolor.bad=yellow\nallow.textmode=true \n”},”kind”:”ConfigMap”,”metadata”:{“annotations”:{},”name”:”my-game”,”namespace”:”default”}}

This is how it looks on terminal:

YAML

Step 7. Checking the ConfigMap

And now if we check that our ConfigMap was deleted or not:

kubectl get cm


we will find the following results:

kubectl get cm

This means that we the CongifMap was never deleted in order to change the configurations inside it. We can also check if the configuration was updated on not by following command:

kubectl get configmap my-game -o yaml


Kubectl get configs

This does represent the changes we made. Hence finally we have learned how to update a ConfigMap without deleting it. You can use the exact process in order to update your Secret without deleting it.

Conclusion

Kubernetes or K8s is an open-sourced container orchestration technology that is used for automating the manual processes of deploying, managing and scaling applications by the help of containers. Kubernetes was originally developed by engineers at Google and In 2015, it was donated to CNCF (Cloud Native Computing Foundation). Updating a Configmap or a Secret is quite straight forward in Kubernetes, you can simply used the command:

kubectl edit [RESOURCE_TYPE]/[RESOURCE_NAME]

in order to update the resource without deleteing it. Make sure to go through the tutorial we discussed in this article in order to understand how to update a Configmap or a Secret without deleting it.

ConfigMap Or Secret Without Deleting One FAQ’s

What is ConfigMap used for?

ConfigMap is a Kubernetes API Object that is used to store non-confidential data in the form of key value pairs.

What is the difference between Secret and Config Map?

Kubernetes Secret is used to store secret data credentials that should not be available to everyone, in base64 encoded format. While Config Maps usually contains configuration data like URLs of database or URLs of some other services.

What are Kubernetes Pods?

Pod is the smallest or fundamental component of a Kubernetes architecture.

What are Kubernetes Deployment?

Deployment are the Kubernetes component that manages the replication and lifecycle of the Pods in the Kubernetes Cluster.

What are the formats supported by Kubernetes Configuration file?

The formats that are supported by Kubernetes Configuration file are:

  1. YAML (YAML Ain’t Markup Language)
  2. JSON (JavaScript Object Notation)

What is the full form of config?

The full form of config is configuration.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads