Env - ConfigMaps
개념
Podyaml 파일 안에 환경 변수를env로 직접 넣으면 파일이 복잡해지고 관리가 어려워진다.ConfigMap을 사용하면, 환경 변수로 사용할 key-value 들을 따로 관리할 수 있다.Pod가 생성될 때ConfigMap객체를 주입하거나, key-value(.conf,.yaml,.properties등)의 파일로ConfigMap을 마운트해서 사용한다.
ConfigMap 생성
Imperative(명령형) 방법
from-literal
kubectl create configmap <name> \
--from-literal=APP_COLOR=blue \
--from-literal=APP_MODE=prod
from-file
kubectl create configmap <name> --from-file=app.properties
Declarative(선언적) 방법 - yaml
# my-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: <name>
data:
APP_COLOR: blue
APP_MODE: prod
data:<key>: <value>(여러 개 가능)
kubectl apply -f my-configmap.yaml
Pod에 삽입
ConfigMap 전체
apiVersion: v1
kind: Pod
metadata:
name: nginx-envfrom
spec:
containers:
- name: nginx
image: nginx
envFrom:
- configMapRef:
name: nginx-config
-
ConfigMap에 있는 key-value 전체를Pod안에 환경변수로 등록한다. -
speccontainersenvFromconfigMapRefname:ConfigMap객체의 이름
특정 변수만 삽입
apiVersion: v1
kind: Pod
metadata:
name: nginx-env-key
spec:
containers:
- name: nginx
image: nginx
env:
- name: APP_COLOR
valueFrom:
configMapKeyRef:
name: nginx-config
key: APP_COLOR
- name: APP_MODE
valueFrom:
configMapKeyRef:
name: nginx-config
key: APP_MODE
-
ConfigMap에 있는 특정 key-value만Pod의 환경변수로 등록한다. (여러 개 가능) -
speccontainersenvname: 환경변수 이름valueFromconfigMapKeyRefname:ConfigMap객체의 이름key:ConfigMap에서 가져올 value의 key
ConfigMap을 Pod 내부 특정 경로에 마운트
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
data:
nginx.conf: |
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
apiVersion: v1
kind: Pod
metadata:
name: nginx-volume
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: nginx-config-volume
mountPath: /etc/nginx/conf.d
volumes:
- name: nginx-config-volume
configMap:
name: nginx-config
items:
- key: nginx.conf
path: nginx.conf
-
ConfigMap을Pod안의 특정 경로에 주입하고 싶을 경우 사용 (환경변수 X) -
speccontainersvolumeMountsname: 아래에 설정할volumes의namemountPath: 마운트할Pod내부의 경로
volumesname:volume의 이름configMap:name: 주입할ConfigMap객체의 이름items:key: 주입할 value의 keypath:Pod내부에 만들어질 파일 명
레퍼런스
- https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/
- https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#define-container-environment-variables-using-configmap-data
- https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#create-configmaps-from-files
- Udemy - Certified Kubernetes Administrator (CKA) with Practice Tests (Mumshad)- Udemy - Certified Kubernetes Administrator (CKA) with Practice Tests (Mumshad)