# pod-definition.yaml
apiVersion: v1
kind:
metadata:
name: myapp-pod
spec:
containers:
- name: myapp-container
image: nginx
env:
- name: myenv
value: 1.9.1
env:
- name: APP_COLOR
value: pink
env:
- name: APP_COLOR
valueFrom:
configMapKeyRef:
env:
- name: APP_COLOR
valueFrom:
secretKeyRef:
# Imerative
$ kubectl create configmap <config-name> --from-literal<key>=<value>
$ kubectl create configmap \
app-config --from-literal=APP_COLOR=blue
# --from-literal 옵션은 자체에서 키 값 쌍을 지정하는데 사용된다.
# 만약 키 값 쌍을 추가하고싶다면 단순히 --from-literal 옵션을 추가하면 된다.
# 하지만 구성 항목이 너무 많다면 복잡해질 수 있다.
$ kubectl create configmap <config-name> --from-file=<path-to-file>
$ kubectl create configmap app-config --from-file=app_config_properties
# --from-literal이 아닌 --from-file 옵션으로 파일을 통해서 구성 데이터를 입력 할 수있다.
# Declarative (선언적 접근)
$ kubectl create -f
# config-map.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
# spec 대신 data 가 들어간다
# data 아래 키 값 포맷을 입력한다
data:
APP_COLOR: blue
APP_MODE: prod
$ kubectl create -f config-map.yaml
# app-config
APP_COLOR: blue
APP_MODE: prod
# mysql-config
port: 3306
max_allowed_packet: 128M
# redis-config
port: 6379
rdb-compression: yes
$ kubectl get configmaps
$ kubectl describe configmaps
# pod-definition.yaml
apiVersion: v1
kind: pod
metadata:
name: simple-webapp-color
labels:
name: simple-webapp-color
spec:
containers:
- name: simple-webapp-color
image: simple-webapp-color
ports:
- containerPort: 8080
envFrom: // 이 부분 구문 참조
- configMapRef:
name: app-config
# app-config는 위에 정의되어 있고 이 때문에 blue 변수를 전달한다.
$ kubectl create -f pod-definition.yaml
💡configMap에서 하나의 키, 값 쌍만 사용하는 방법
apiVersion: v1
kind: Pod
metadata:
name: single-env-var-pod
spec:
containers:
- name: my-container
image: nginx
env:
- name: DATABASE_HOST
valueFrom:
configMapKeyRef:
name: my-config
key: DB_HOST
