49

I am using the following command to create a configMap.

kubectl create configmap test --from-file=./application.properties --from-file=./mongo.properties --from-file=./logback.xml 

Now, I have modified a value for a key from mongo.properties which i need to update in kubernetes.

Option1 :-

kubectl edit test

Here, it opens the entire file. But, I want to just update mongo.properties and hence want to see only the mongo.properties. Is there any other way?

Note :- I dont want to have mongo.properties in a separate configMap.

Thanks

user1578872
  • 7,808
  • 29
  • 108
  • 206
  • 1
    Its answered here https://stackoverflow.com/questions/38216278/update-k8s-configmap-or-secret-without-deleting-the-existing-one – bits Apr 23 '18 at 23:10

9 Answers9

68

Now you can. Just throw: kubectl edit configmap <name of the configmap> on your command line. Then you can edit your configuration.

Arivaldo
  • 791
  • 1
  • 5
  • 4
  • 5
    Hi, is the changes auto applied after editing and saving the Configmap? – jialeee17 Nov 23 '21 at 03:37
  • 2
    @jialeee17 if `reload` is defined, then should be applied automatically. Regarding to docs: > reload: Allows automatic reload of a changed Corefile. After you edit the ConfigMap configuration, allow two minutes for your changes to take effect. – georgeos Jul 29 '22 at 13:50
38

Another option is actually you can use this command:

kubectl create configmap some-config \
  --from-file=some-key=some-config.yaml \
  -n some-namespace \
  -o yaml \
  --dry-run | kubectl apply -f - 

Refer to Github issue: Support updating config map and secret with --from-file

Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
irvifa
  • 1,865
  • 2
  • 16
  • 20
  • 2
    +1 This works for small configmaps but not for large ones... It fails with `The ConfigMap "xx" is invalid: metadata.annotations: Too long: must have at most 262144 characters`. Do you know a way around this? – MoRe Sep 09 '20 at 15:09
  • @srinivas I ended up just deleting and recreating the configmap from scratch. That works :) – MoRe Oct 01 '20 at 08:48
  • @More, I tried with more than 262144 characters, it worked with the above command. Command used - kubectl create cm ${applicationconfigmap} --namespace {{ .Release.Namespace }} --from-file=key1.yml=/tmp/key1-backup.yaml -o yaml --dry-run | kubectl apply -f - – srinivas Oct 05 '20 at 14:26
  • Use server side argument for apply – trallnag May 31 '21 at 07:42
29

kubectl edit configmap -n <namespace> <configMapName> -o yaml

This opens up a vim editor with the configmap in yaml format. Now simply edit it and save it.

Sathish
  • 1,245
  • 2
  • 15
  • 22
  • yes you are right.. it was a typo from my end.. it is `kubectl edit` that opens editor thanks for pointing out @3h4x – Sathish Mar 31 '20 at 08:22
  • 5
    also it might not open vim, it depends on environment variable `editor` that you have set – 3h4x Apr 02 '20 at 13:21
  • 3
    I don't understand why this isn't the accepted answer? You can edit the config map and It works for me. Only one edit to the answer @Sathish would be that after the edit you need to bring the re-scale the pods for the edit to be deployed. – Anand Sonawane Sep 07 '21 at 12:51
  • @AnandSonawane, well we use annotations https://github.com/stakater/Reloader to do that automatically for us – Sathish Sep 07 '21 at 13:00
  • @Sathish Got it Thanks. – Anand Sonawane Sep 07 '21 at 13:02
  • 1
    Is the changes auto-applied or we have to use other kubectl commands to apply the changes? – jialeee17 Nov 23 '21 at 03:17
  • yeah it edits the configMaps in the cluster and applies the changes to the configMaps where as if you want your pods to pick the recent changes, you will have to restart the pods separately or if you use https://github.com/stakater/Reloader than the pods are automatically restarted! – Sathish Nov 23 '21 at 11:53
22

Here's a neat way to do an in-place update from a script.

The idea is;

  1. export the configmap to YAML (kubectl get cm -o yaml)
  2. use sed to do a command-line replace of an old value with a new value (sed "s|from|to")
  3. push it back to the cluster using kubectl apply

In this worked example, I'm updating a log level variable from 'info' level logging to 'warn' level logging.

So, step 1, read the current config;

$ kubectl get cm common-config -o yaml

apiVersion: v1
data:
  CR_COMMON_LOG_LEVEL: info
kind: ConfigMap

Step 2, you modify it locally with a regular expression search-and-replace, using sed:

$ kubectl get cm common-config -o yaml | \
  sed -e 's|CR_COMMON_LOG_LEVEL: info|CR_COMMON_LOG_LEVEL: warn|'

apiVersion: v1
data:
  CR_COMMON_LOG_LEVEL: warn
kind: ConfigMap

You can see the value has changed. Let's push it back up to the cluster;

Step 3; use kubectl apply -f -, which tells kubectl to read from stdin and apply it to the cluster;

$ kubectl get cm common-config -o yaml | \
  sed -e 's|CR_COMMON_LOG_LEVEL: info|CR_COMMON_LOG_LEVEL: warn|' | \
  kubectl apply -f -

configmap/common-config configured
Steve Cooper
  • 20,542
  • 15
  • 71
  • 88
  • 1
    A perfect one line command that works like a charm! thank you. Can be extended with "kubectl rollout restart deployment (name of the deployment)" if you want to automatically apply your new configmap if affects a deployment for example – Gonzalo Cao Jun 12 '20 at 18:40
  • Should be the first answer! – Matthias Aug 02 '21 at 15:57
  • instead of `sed -e 's|CR_COMMON_LOG_LEVEL: info|CR_COMMON_LOG_LEVEL: warn|'` we can use regex `sed -e 's|CR_COMMON_LOG_LEVEL:.*|CR_COMMON_LOG_LEVEL: warn|'` – caxefaizan Apr 04 '22 at 05:02
  • This is the best answer because you can use it to automate configuration changes to default files (using bash scripts etc.), which you can'do if you have to make the change manually using `kubectl edit` and vim or a dashboard like `k9s`... – James Lombard Jun 01 '22 at 19:48
  • How to add new entries ? – Sanjeev Oct 25 '22 at 15:01
13

No, you can't.

Replace in kubernetes will simply replace everything in that configmap. You can't just update one file or one single property in it.

However, if you check with the client Api, you will find if you create a configmap with lots of files. Then, those files will be stored as a HashMap, where key is file name by default, value is the file content encoded as a string. So you can write your own function based on existing key-value pair in HashMap.

This is what I found so far, if you find there is already existing method to deal with this issue, please let me know :)

FYI, if you want to update just one or few properties, it is possible if you use patch. However, it is a little bit hard to implement.

this and this may help

Wytrzymały Wiktor
  • 11,492
  • 5
  • 29
  • 37
Conan Bryant
  • 164
  • 1
  • 4
6

Here is how you can add/modify/remove files in a configmap with some help from jq:

export configmap to a JSON file:

CM_FILE=$(mktemp -d)/config-map.json
oc get cm <configmap name> -o json > $CM_FILE

DATA_FILES_DIR=$(mktemp -d)
files=$(cat $CM_FILE | jq '.data' | jq -r 'keys[]')
for k in $files; do
    name=".data[\"$k\"]"
    cat $CM_FILE | jq -r $name > $DATA_FILES_DIR/$k;
done

add/modify a file:

echo '<paste file contents here>' > $DATA_FILES_DIR/<file name>.conf

remove a file:

rm <file name>.conf

when done, update the configmap:

kubectl create configmap <configmap name> --from-file $DATA_FILES_DIR -o yaml --dry-run | kubectl apply -f -

delete temporary files and folders:

rm -rf CM_FILE
rm -rf DATA_FILES_DIR
Bruce S.
  • 106
  • 1
  • 3
5

Suggestion

I would highly consider using a CLI editor like k9s (which is more like a K8S CLI managment tool).

As you can see below (ignore all white placeholders), when your cluster's context is set on terminal you just type k9s and you will reach a nice terminal where you can inspect all cluster resources.

Just type ":" and enter the resource name (configmaps in our case) which will appear in the middle of screen (green rectangle).
Then you can choose the relevant configmap with the up and down arrows and type e to edit it (see green arrow).

For all Configmaps in all namespaces you choose 0, for a specific namespace you choose the number from the upper left menu - for example 1 for kube-system:

enter image description here

Rot-man
  • 18,045
  • 12
  • 118
  • 124
2

Here is a complete shell script to add new file to configmap (or replace existing one) based on @Bruce S. answer https://stackoverflow.com/a/54876249/2862663

#!/bin/bash
# Requires jq to be installed
if [ -z "$1" ]
  then
    echo "usage: update-config-map.sh <config map name> <config file to add>"
    return
fi 
if [ -z "$2" ]
  then
    echo "usage: update-config-map.sh <config map name> <config file to add>"
    return
fi 

CM_FILE=$(mktemp -d)/config-map.json
kubectl get cm $1 -o json > $CM_FILE

DATA_FILES_DIR=$(mktemp -d)
files=$(cat $CM_FILE | jq '.data' | jq -r 'keys[]')
for k in $files; do
    name=".data[\"$k\"]"
    cat $CM_FILE | jq -r $name > $DATA_FILES_DIR/$k;
done

echo cunfigmap: $CM_FILE tempdir: $DATA_FILES_DIR

echo will add file $2 to config

cp $2 $DATA_FILES_DIR

kubectl create configmap $1 --from-file $DATA_FILES_DIR -o yaml --dry-run |  kubectl apply -f -
echo Done
echo removing temp dirs
rm -rf $CM_FILE
rm -rf $DATA_FILES_DIR
Dmitry Andrievsky
  • 1,803
  • 17
  • 20
  • The `dry-run | kubectl apply..` line is what I needed, thanks for that e.g. `kubectl create secret generic secrets --from-env-file=secrets.env -o yaml --dry-run=client | kubectl apply -f -` – Paul Watson Feb 10 '21 at 16:09
1

I managed to update a setting ("large-client-header-buffers") in the nginx pod's /etc/nginx/nginx.conf via configmap. Here are the steps I have followed..

  1. Find the configmap name in the nginx ingress controller pod describition
kubectl -n utility describe pods/test-nginx-ingress-controller-584dd58494-d8fqr |grep configmap
      --configmap=test-namespace/test-nginx-ingress-controller

Note: In my case, the namespace is "test-namespace" and the configmap name is "test-nginx-ingress-controller"

  1. Create a configmap yaml
cat  << EOF > test-nginx-ingress-controller-configmap.yaml

kind: ConfigMap
apiVersion: v1
metadata:
  name: test-nginx-ingress-controller
  namespace: test-namespace
data:
  large-client-header-buffers: "4 16k"
EOF

Note: Please replace the namespace and configmap name as per finding in the step 1

  1. Deploy the configmap yaml
kubectl apply -f test-nginx-ingress-controller-configmap.yaml

Then you will see the change is updated to nginx controller pod after mins

i.g.
kubectl -n test-namespace exec -it test-nginx-ingress-controller-584dd58494-d8fqr -- cat /etc/nginx/nginx.conf|grep large
    large_client_header_buffers     4 16k;

Thanks to the sharing by NeverEndingQueue in How to use ConfigMap configuration with Helm NginX Ingress controller - Kubernetes

Chance
  • 405
  • 4
  • 8