1. ConfigMap type
-
property-like
key
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
api-url: "https://api.example.com"
log-level: "debug"
-
file-like
key
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
config.yaml: |
apiVersion: v1
data:
app-name: "MyApp"
app-version: "1.0"
2. ConfigMap consume type
- Args
- Inside a container command and args
- samilar with the env var comsuming method
- Env var
- Use it as environment variables for a container
- normally used
property-like
key ConfigMap.
- Vol Mount
- Add a file in read-only volume, for the application to read
- can be used both
property-like
key andfile-like
ConfigMap
- Write code to run inside the Pod that uses the Kubernetes API to read a ConfigMap (not discuss it here)
3.1 Args
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
my-param: "Hello, Kubernetes!"
---
apiVersion: v1
kind: Pod
metadata:
name: app-pod
spec:
containers:
- name: app-container
image: busybox
command: ["/bin/sh"]
args: ["-c", "sleep 3600", "echo $(MY_PARAM)> log.txt"]
env: # use it here same as env
- name: MY_PARAM
valueFrom:
configMapKeyRef:
name: app-config
key: my-param
2.2 Env var
2.2.1 Use whole data
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
api-url: "https://api.example.com"
log-level: "debug"
---
apiVersion: v1
kind: Pod
metadata:
name: app-pod
spec:
containers:
- name: app-container
image: busybox
command: ["bin/sh", "-c", "sleep 3600"]
envFrom: # use whole data value
- configMapRef:
name: app-config
2.2.2 Use some data
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
api-url: "https://api.example.com"
log-level: "debug"
---
apiVersion: v1
kind: Pod
metadata:
name: app-pod
spec:
containers:
- name: app-container
image: busybox
command: ["bin/sh", "-c", "sleep 3600"]
env:
- name: API_URL
valueFrom:
configMapKeyRef:
name: app-config
key: api-url
- name: LOG_LEVEL
valueFrom: # use some data value
configMapKeyRef:
name: app-config
key: log-level
2.3 Vol Mount
2.3.1 use whole file
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
config.yaml: |
apiVersion: v1
data:
app-name: "MyApp"
app-version: "1.0"
---
apiVersion: v1
kind: Pod
metadata:
name: app-pod
spec:
containers:
- name: app-container
image: busybox
command: ["/bin/sh", "-c", "sleep 3600"]
volumeMounts:
- name: config-volume
mountPath: /etc/config
readOnly: true
volumes:
- name: config-volume
configMap:
name: app-config
2.3.2 Use part data items from file
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
app-name: "MyApp"
app-version-file: |
appVersion "1.0"
---
apiVersion: v1
kind: Pod
metadata:
name: app-pod
spec:
containers:
- name: app-container
image: busybox
command: ["/bin/sh", "-c", "sleep 3600"]
volumeMounts:
- name: config-volume
mountPath: /etc/config
readOnly: true
volumes:
- name: config-volume
configMap:
name: app-config
items:
- key: app-name
path: app-name
- key: app-version-file
path: app-version-file
we can see the files inside the container:
/ # ls /etc/config/
app-name app-version-file
/ # cat /etc/config/app-name
/ # cat /etc/config/app-version-file
appVersion "1.0"
/ # cat /etc/config/app-name
MyApp
/ # tree /etc/config/
/etc/config/
├── app-name -> ..data/app-name
└── app-version-file -> ..data/app-version-file
0 directories, 2 files
Above we can see it according to property-like
key and file-like
create different files.
Top comments (0)