Contents

Kubernetes Environment Configmap

Create ConfigMap

ConfigMap
If you want to know all of ConfigMap, see this document

Imperative

Values with command

1
2
3
4
5
6
7
# Structure
kubectl create configmap <config-name> --from-literal=<key>=<value> \
                                       --from-literal=<key>=<value> \
                                       --from-literal=<key>=<value>
# Example
kubectl create configmap app-config --from-literal=APP_COLOR=blue \
                                    --from-literal=APP_MODE=prod

Values from file

app_config.properties

1
2
APP_COLOR: blue
APP_MODE: prod
1
2
3
4
5
# Structure
kubectl create configmap <config-name> --from-file=<path-to-file>

# Example
kubectl create configmap app-config --from-file=app_config.properties

Declarative

config-map.yaml

1
2
3
4
5
6
7
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  APP_COLOR: blue
  APP_MODE: prod
1
kubectl create –f config-map.yaml

Apply ConfigMap to POD YAML

pod-definition.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
apiVersion: v1
kind:
  Pod
metadata:
  name: simple-webapp-color
spec:
  containers:
    - name: simple-webapp-color
      image: simple-webapp-color
      ports:
        - containerPort: 8080
      env:
        - name: APP_COLOR
          valueFrom:
            configMapKeyRef:
              name: app-config
              key: APP_COLOR
1
kubectl create -f pod-definition.yaml

View ConfigMaps

1
2
kubectl get cm
kubectl get configmaps

ConfigMap Detail

1
2
3
4
5
# All ConfigMaps
kubectl describe configmaps

# Specific ConfigMap
kubectl describe configmap app-config

ConfigMap in PODs

ENV

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
apiVersion: v1
kind:
  Pod
metadata:
  name: simple-webapp-color
spec:
  containers:
    - name: simple-webapp-color
      image: simple-webapp-color
      ports:
        - containerPort: 8080
      envFrom:
        - configMapRef:
            name: app-config
        - configMapRef:
            name: db-config

SINGLE ENV

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
apiVersion: v1
kind:
  Pod
metadata:
  name: simple-webapp-color
spec:
  containers:
    - name: simple-webapp-color
      image: simple-webapp-color
      ports:
        - containerPort: 8080
      env:
        - name: APP_COLOR
          valueFrom:
            configMapKeyRef:
              name: app-config
              key: APP_COLOR

VOLUME

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
apiVersion: v1
kind:
  Pod
metadata:
  name: simple-webapp-color
spec:
  volumes:
    - name: config-volume
      configMap:
        name: app-config
  containers:
    - name: simple-webapp-color
      image: simple-webapp-color
      ports:
        - containerPort: 8080
      volumeMounts:
        - name: config-volume
          mountPath: /etc/config