Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Kubernetes: Storage - ConfigMap

Back


ConfigMap


ConfigMap object


Declarative Manifest

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  APP_MODE: "prod"
  application.yml: |
    server:
      port: 8080

Imperative Command

CMD DESC
kubectl get cm List ConfigMaps in the current namespace.
kubectl get cm app-config -o yaml Show a specific ConfigMap in YAML.
kubectl create cm app-config --from-literal=K=V --dry-run=client -o yaml > cm.yaml Generate YAML from an imperative command
kubectl describe cm app-config Detailed view: keys, events, annotations, etc.
kubectl edit cm app-config Edit a ConfigMap in editor.
kubectl delete cm app-config Delete a ConfigMap.
CMD DESC
kubectl create cm cm_name --from-literal=key=value Create a ConfigMap from one or more literal key/values.
kubectl create cm cm_name --from-literal=key=value Create a ConfigMap from one or more literal key/values.
kubectl create cm cm_name --from-file=cf_file Create from a file; filename becomes the key
kubectl create cm cm_name --from-file=mykey=config.yaml Create from a file; Specify the key
kubectl create cm cm_name --from-file=./templates/ Create from a directory; each file becomes a key.
kubectl create cm cm_name --from-env-file=.env Create from a dotenv file (KEY=VALUE lines).
Option Desc
--from-literal Inserts a key and a literal value.
--from-env-file Inserts each line of the specified file as a separate entry
--from-file Inserts the contents of a file

Lab: Create ConfigMap

Imperative Command from Literals

kubectl create cm config-literal --from-literal=APP_MODE="prod" --from-literal=LOG_LEVEL="info"
# configmap/config-literal created

kubectl get cm
# NAME               DATA   AGE
# config-literal     1      10s

kubectl describe cm config-literal
# Name:         config-literal
# Namespace:    default
# Labels:       <none>
# Annotations:  <none>

# Data
# ====
# APP_MODE:
# ----
# prod

# LOG_LEVEL:
# ----
# info


# BinaryData
# ====

# Events:  <none>

Imperative Command from file

mkdir conffile

# create file
cat > conffile/config-file.yaml <<'EOF'
APP_MODE: "prod"
LOG_LEVEL: "info"
application.yml: |
  server:
    port: 8080
EOF

kubectl create configmap config-file --from-file=./conffile/config-file.yaml
# configmap/config-file created

# confirm
kubectl get cm
# NAME               DATA   AGE
# config-file        1      15s

kubectl describe cm config-file
# Name:         config-file
# Namespace:    default
# Labels:       <none>
# Annotations:  <none>

# Data
# ====
# config-file.yaml:
# ----
# APP_MODE: "prod"\r
# LOG_LEVEL: "info"\r
# application.yml: |\r
#   server:\r
#     port: 8080


# BinaryData
# ====

# Events:  <none>

Imperative Command from file with custom key name

mkdir conffile

# create file
cat > conffile/config-file.yaml <<'EOF'
APP_MODE: "prod"
LOG_LEVEL: "info"
application.yml: |
  server:
    port: 8080
EOF

kubectl create configmap config-file-custom-key --from-file=application.yml=./conffile/config-file.yaml
# configmap/config-file-custom-key created

kubectl get cm
# NAME                     DATA   AGE
# config-file-custom-key   1      10s

kubectl describe cm config-file-custom-key
# Name:         config-file-custom-key
# Namespace:    default
# Labels:       <none>
# Annotations:  <none>

# Data
# ====
# application.yml:
# ----
# APP_MODE: "prod"\r
# LOG_LEVEL: "info"\r
# application.yml: |\r
#   server:\r
#     port: 8080


# BinaryData
# ====

# Events:  <none>

Imperative Command From multiple files in a directory

mkdir confdir
# create files
cat > confdir/dev.yaml <<'EOF'
APP_MODE: "dev"
LOG_LEVEL: "info"
application.yml: |
  server:
    port: 8080
EOF

cat > confdir/prod.yaml <<'EOF'
APP_MODE: "prod"
LOG_LEVEL: "info"
application.yml: |
  server:
    port: 80
EOF

kubectl create configmap config-dir --from-file=confdir/
# configmap/config-dir created

kubectl get cm
# NAME                     DATA   AGE
# config-dir               2      12s

kubectl describe cm config-dir
# Name:         config-dir
# Namespace:    default
# Labels:       <none>
# Annotations:  <none>

# Data
# ====
# dev.yaml:
# ----
# APP_MODE: "dev"\r
# LOG_LEVEL: "info"\r
# application.yml: |\r
#   server:\r
#     port: 8080\r


# prod.yaml:
# ----
# APP_MODE: "prod"\r
# LOG_LEVEL: "info"\r
# application.yml: |\r
#   server:\r
#     port: 80


# BinaryData
# ====

# Events:  <none>

Imperative Command From an env file

mkdir conf_env
cat > conf_env/dev.env <<EOF
APP_MODE="dev"
LOG_LEVEL="info"
PORT=8080
EOF

kubectl create cm config-env --from-env-file=conf_env/dev.env
# configmap/config-env created

kubectl get cm
# NAME                     DATA   AGE
# config-env               3      11s

kubectl describe cm config-env
# Name:         config-env
# Namespace:    default
# Labels:       <none>
# Annotations:  <none>

# Data
# ====
# APP_MODE:
# ----
# "dev"

# LOG_LEVEL:
# ----
# "info"

# PORT:
# ----
# 8080


# BinaryData
# ====

# Events:  <none>

Imperative Command From multple env files

mkdir conf_env

cat > conf_env/dev.env <<EOF
APP_MODE="dev"
LOG_LEVEL="info"
PORT=8080
EOF

cat > conf_env/prod.env <<EOF
APP_MODE="prod"
LOG_LEVEL="info"
PORT=80
EOF

kubectl create cm config-multipl-env --from-env-file=conf_env/dev.env --from-env-file=conf_env/prod.env
# error: cannot add key "APP_MODE", another key by that name already exists in Data for ConfigMap "config-multipl-env"

cat > conf_env/app.env <<EOF
LOG_LEVEL="info"
PORT=8080
EOF

cat > conf_env/more.env <<EOF
LOG_ERROR="error"
TIER="frontend"
EOF

kubectl create cm config-multipl-env --from-env-file=conf_env/app.env --from-env-file=conf_env/more.env
# configmap/config-multipl-env created

kubectl get cm config-multipl-env
# NAME                 DATA   AGE
# config-multipl-env   4      43

kubectl describe cm config-multipl-env
# Name:         config-multipl-env
# Namespace:    default
# Labels:       <none>
# Annotations:  <none>

# Data
# ====
# LOG_ERROR:
# ----
# "error"

# LOG_LEVEL:
# ----
# "info"

# PORT:
# ----
# 8080

# TIER:
# ----
# "frontend"


# BinaryData
# ====

# Events:  <none>

Declarative File

# conf_yaml/config-yaml.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: config-yaml
data:
  APP_MODE: "dev"
  LOG_LEVEL: "info"

  # file-like keys
  application.yml: |
    server:
      port: 8080
kubectl create -f conf_yaml/config-yaml.yaml
# configmap/config-yaml created

kubectl get cm
# NAME               DATA   AGE
# config-yaml        3      13s

kubectl describe cm config-yaml
# Name:         config-yaml
# Namespace:    default
# Labels:       <none>
# Annotations:  <none>

# Data
# ====
# APP_MODE:
# ----
# dev

# LOG_LEVEL:
# ----
# info

# application.yml:
# ----
# server:
#   port: 8080



# BinaryData
# ====

# Events:  <none>

Edit ConfigMap


# edit: add application.yml
kubectl edit cm config-literal
# configmap/config-literal edited

kubectl describe cm config-literal
# Name:         config-literal
# Namespace:    default
# Labels:       <none>
# Annotations:  <none>

# Data
# ====
# APP_MODE:
# ----
# prod

# application.yml:
# ----
# server:
#   port: 8080



# BinaryData
# ====

# Events:  <none>

Delete ConfigMap

kubectl delete cm config-literal config-dir config-env config-file config-file-custom-key config-multipl-env config-yaml
# configmap "config-literal" deleted from default namespace
# configmap "config-dir" deleted from default namespace
# configmap "config-env" deleted from default namespace
# configmap "config-file" deleted from default namespace
# configmap "config-file-custom-key" deleted from default namespace
# configmap "config-multipl-env" deleted from default namespace
# configmap "config-yaml" deleted from default namespace

# confirm
kubectl get cm