Kubernetes ConfigMap Operations: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(26 intermediate revisions by the same user not shown)
Line 4: Line 4:
* [[Kubernetes Operations#ConfigMap_Operations|Kubernetes Operations]]
* [[Kubernetes Operations#ConfigMap_Operations|Kubernetes Operations]]
* [[Kubernetes_Cluster_Configuration_Concepts#ConfigMap|Kubernetes Cluster Configuration Concepts]]
* [[Kubernetes_Cluster_Configuration_Concepts#ConfigMap|Kubernetes Cluster Configuration Concepts]]
=List ConfigMaps in a Namespace=
<syntaxhighlight lang='bash'>
kubectl -n <namespace-name> get configmaps
</syntaxhighlight>


=Inspect a ConfigMap=
=Inspect a ConfigMap=
 
<syntaxhighlight lang=bash>
kubectl get cm <''name''> -o yaml
kubectl get cm <cm-name> -o yaml
 
kubectl describe cm <cm-name>
</syntaxhighlight>
<syntaxhighlight lang=yaml>
<syntaxhighlight lang=yaml>
apiVersion: v1
apiVersion: v1
Line 18: Line 24:
   color: red
   color: red
   shape: square
   shape: square
</syntaxhighlight>
=Read the Content of a File Stored in ConfigMap=
<syntaxhighlight lang=bash>
kubectl get cm <cm-name> -o jsonpath='{.data.<''key''>}'
</syntaxhighlight>
</syntaxhighlight>


=Create a ConfigMap with CLI=
=Create a ConfigMap with CLI=
All three options presented below can be combined on one single command line:


==Specify Key/Value Pairs on Command Line==
==Specify Key/Value Pairs on Command Line==
 
<syntaxhighlight lang='bash'>
kubectl create configmap example --from-literal=color=red --from-literal=shape=square
kubectl create configmap example \
 
  --from-literal=COLOR=red \
  --from-literal=SHAPE=square \
  --from-literal=PHRASE="something and something else"
</syntaxhighlight>
Multiple <code>--from-literal=</code> entries are allowed.
Multiple <code>--from-literal=</code> entries are allowed.


==Use the Content of a File==
==Use the Content of a File==


The entire content of the file is keyed under the file name (by default) or under an arbitrary name, if specified.
The entire content of the file is keyed by default under the file name.
 
<syntaxhighlight lang='bash'>
kubectl create configmap example --from-file=some-config-file.txt
kubectl create configmap <cm-name> --from-file=some-config-file.txt [--from-file=some-config-file2.txt ...]
</syntaxhighlight>


<syntaxhighlight lang='yaml'>
<syntaxhighlight lang='yaml'>
Line 44: Line 61:
     color=blue
     color=blue
     shape=circle
     shape=circle
</syntaxhighlight>
An arbitrary key, different from the file name, can be specified:
<syntaxhighlight lang='bash'>
kubectl create configmap <cm-name> --from-file=mykey=some-config-file.txt
</syntaxhighlight>
Multiple <code>--from-file=</code> can be used.
==Import all Files from a Directory==
kubectl can be used to inspect a directory and create an individual ConfigMap entry for each file in the directory whose name is a valid [[Kubernetes_Cluster_Configuration_Concepts#ConfigMap_Keys|ConfigMap key]]. In the example below, /tmp/config-dir contains two files: A.txt and B.txt.
<syntaxhighlight lang='bash'>
kubectl create configmap example --from-file=/tmp/config-dir
</syntaxhighlight>
<syntaxhighlight lang='yaml'>
apiVersion: v1
kind: ConfigMap
metadata:
  name: example
  ...
data:
  A.txt: |
    color=red
  B.txt: |
    shape=square
</syntaxhighlight>
=Edit an Already Deployed ConfigMap=
==Interactively==
<syntaxhighlight lang='bash'>
kubectl edit configmap <cm-name>
</syntaxhighlight>
==Non-Interactively==
This sequence adds a key/value (shape=square) to an existing ConfigMap, leaving the other keys unchanged:
<syntaxhighlight lang='bash'>
kubectl create configmap <existing-configmap-name> --from-literal=shape=square --dry-run --save-config=true -o yaml | k apply -f -
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 17:18, 29 February 2024

External

Internal

List ConfigMaps in a Namespace

kubectl -n <namespace-name> get configmaps

Inspect a ConfigMap

kubectl get cm <cm-name> -o yaml
kubectl describe cm <cm-name>
apiVersion: v1
kind: ConfigMap
metadata:
  name: example
  ...
data:
  color: red
  shape: square

Read the Content of a File Stored in ConfigMap

kubectl get cm <cm-name> -o jsonpath='{.data.<''key''>}'

Create a ConfigMap with CLI

All three options presented below can be combined on one single command line:

Specify Key/Value Pairs on Command Line

kubectl create configmap example \
  --from-literal=COLOR=red \
  --from-literal=SHAPE=square \
  --from-literal=PHRASE="something and something else"

Multiple --from-literal= entries are allowed.

Use the Content of a File

The entire content of the file is keyed by default under the file name.

kubectl create configmap <cm-name> --from-file=some-config-file.txt [--from-file=some-config-file2.txt ...]
apiVersion: v1
kind: ConfigMap
metadata:
  name: example
  ...
data:
  some-config-file.txt: |
    color=blue
    shape=circle

An arbitrary key, different from the file name, can be specified:

kubectl create configmap <cm-name> --from-file=mykey=some-config-file.txt

Multiple --from-file= can be used.

Import all Files from a Directory

kubectl can be used to inspect a directory and create an individual ConfigMap entry for each file in the directory whose name is a valid ConfigMap key. In the example below, /tmp/config-dir contains two files: A.txt and B.txt.

kubectl create configmap example --from-file=/tmp/config-dir
apiVersion: v1
kind: ConfigMap
metadata:
  name: example
  ...
data:
  A.txt: |
    color=red
  B.txt: |
    shape=square

Edit an Already Deployed ConfigMap

Interactively

kubectl edit configmap <cm-name>

Non-Interactively

This sequence adds a key/value (shape=square) to an existing ConfigMap, leaving the other keys unchanged:

 kubectl create configmap <existing-configmap-name> --from-literal=shape=square --dry-run --save-config=true -o yaml | k apply -f -