ConfigMap & Secret
ConfigMap
컨테이너 구성 정보를 한곳에 모아서 관리
특정 Pod들에게 환경변수(일부, 전체)를 전달 할 수 있음
key value 형태이나, key에 file을 혹은 value에 file 혹은 directory를 넣을 수 있다.
(파일용량은 1M 이하)
ConfigMap 생성
kubectl create configmap NAME [--from-file=source] [--from-literal=key1=value1]--from-file: 파일 내용을 value로 저장한다. key는 파일명이며,key=파일형식으로 key 이름을 바꿀 수 있다. 디렉터리를 지정하면 안의 파일마다 key가 생긴다.--from-literal:key=value를 명령어에 직접 입력한다.
--from-file=text.file파일 text.file → 파일명이 key
text.file
--from-file=mydata=text.file파일 text.file → key 이름을 직접 지정
mydata
--from-file=/configmap.dir/디렉터리 configmap.dir/ → 안의 파일마다 key 생성
intervalnginx-config.conf
--from-literal=id=b2021002 --from-literal=class=bigdata명령어에 값을 직접 입력
idclass
→
ConfigMap : test_Configmap
text.fileThis is a file.
mydataThis is a file.
idb2021002
classbigdata
interval2
nginx-config.confserver { ... }
ConfigMap : test_Configmap
| Key | Value | 생성 옵션 |
|---|---|---|
text.file | This is a file. | --from-file=text.file |
mydata | This is a file. | --from-file=mydata=text.file |
id | b2021002 | --from-literal=id=b2021002 |
class | bigdata | --from-literal=class=bigdata |
interval | 2 | --from-file=/configmap.dir/ |
nginx-config.conf | | --from-file=/configmap.dir/ |
kubectl create configmap CONFIG_NAME --from-literal=id=b2021002 \n --from-literal=class=bigdata
kubectl create configmap CONFIG_NAME --from-file=text.file
kubectl create configmap CONFIG_NAME --from-file=mydata=text.file
kubectl create configmap CONFIG_NAME --from-file=/configmap.dir/출처 : 유튜브 따배런 따배쿠 https://www.youtube.com/@ttabae-learn
#생성예제
#config.dir 디렉토리를 가지고 있고, 이 안에는 nginx-confg.conf 파일이 있다고 가정
kubectl create configmap ttabae-config \
--from-literal=INTERVAL=2 --from-literal=OPTION=boy --from-file=config.dir/
#configmap 조회
kubectl get configmaps
NAME DATA AGE
kube-root-ca.crt 1 11d
ttabae-config 3 26s #<--- 값이 3개 있다고 보임
#실행 후 ttabae-config에는 다음의 값이 들어가 있을 것이다
#INNTERVAL:2
#OPTION:boy
#nginx-config.conf:파일내용 어쩌고저쩌고#configmap 상세조회
kubectl describe configmaps ttabae-config
Name: ttabae-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
INTERVAL:
----
2
OPTION:
----
boy
nginx-config.conf:
----
server {
listen 80;
server_name www.example.com;
gzip on;
gzip_types text/plain application/xml;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
BinaryData
====
Events: <none>
# configMap 수정
kubectl edit configmaps ttabae-config#위에서 만든 configMap의 환경변수 값을 이용하여 pod yaml 파일을 생성
cat genid.yaml
apiVersion: v1
kind: Pod
metadata:
name: genid-stone
spec:
containers:
- image: bluedove97/genid:env
env:
- name: INTERVAL
valueFrom:
configMapKeyRef:
name: ttabae-config #<-- ttabae-config에 있는 INTERVAL 키값를, 환경변수 INTERVAL 에 넣어라.
key: INTERVAL
name: fakeid
volumeMounts:
- name: html
mountPath: /webdata
- image: nginx:1.14
name: web-server
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
readOnly: true
ports:
- containerPort: 80
volumes:
- name: html
emptyDir: {}#configMap을 통째로 환경변수로 세팅할 때
cat genid-whole.yaml
apiVersion: v1
kind: Pod
metadata:
name: genid-boy
spec:
containers:
- image: bluedove97/genid:env
envFrom: #<--- envFrom : configMap 정의된 전체환경변수를 불러온다.
- configMapRef:
name: ttabae-config
name: fakeid
volumeMounts:
- name: html
mountPath: /webdata
- image: nginx:1.14
name: web-server
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
readOnly: true
ports:
- containerPort: 80
volumes:
- name: html
emptyDir: {}#ConfigMap의 key를 pod의 컨테이너에 볼륨마운트 하기. (파일로부터 읽어온 환경변수 넘기기)
cat genid-volume.yaml
apiVersion: v1
kind: Pod
metadata:
name: genid-volume
spec:
containers:
- image: bluedove97/genid:env
env:
- name: INTERVAL
valueFrom:
configMapKeyRef:
name: ttabae-config
key: INTERVAL
name: fakeid-generator
volumeMounts:
- name: html
mountPath: /webdata
- image: nginx:1.14
name: web-server
ports:
- containerPort: 80
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
readOnly: true
- name: config
mountPath: /etc/nginx/conf.d
readOnly: true
volumes:
- name: html
emptyDir: {}
- name: config
configMap:
name: ttabae-config
items:
- key: nginx-config.conf
path: nginx-config.confSecret
시크릿은 컨피그맵과 비슷한 맥락이지만, base64로 인코딩해서 모아둔다.
시크릿 용량제한 1M
- docker-registry 도커관련된 변수(이름, 패스워드, 이메일)
- generic 일반적인 변수
- tls 인증과 관련된 변수
Secret 만들기
kubectl create secret <Available Commands> name [flags] [options]| Available Command | 설명 |
|---|---|
docker-registry | Docker registry에서 사용할 Secret 생성 (이름, 패스워드, 이메일) |
generic | 로컬 파일, 디렉터리, literal 값으로 Secret 생성 |
tls | TLS 인증서/키로 Secret 생성 |
# tls
kubectl create secret tls my-secret --cert=path/to/cert/file --key=path/to/key/file
# docker-registry
kubectl create secret docker-registry reg-secret --docker-username=tiger \
--docker-password=pass --docker-email=tiger@acme.com
# generic
kubectl create secret generic ttabae-secret \
--from-literal=INTERVAL=2 --from-file=./genid-web-config/--from-literal=INTERVAL=2명령어에 값을 직접 입력
INTERVAL
--from-file=./genid-web-config/디렉터리 genid-web-config/ → 안의 파일마다 key 생성
nginx-config.conf
→
Secret : ttabae-secret
INTERVAL2
nginx-config.confserver { ... }
Secret : ttabae-secret
| Key | Value |
|---|---|
INTERVAL | 2 |
nginx-config.conf | |
- Secret의 value는 base64로 인코딩되어 저장된다. (암호화는 아님)
출처 : 유튜브 따배런 따배쿠 https://www.youtube.com/@ttabae-learn
#secret 생성
kubectl create secret generic ttabae-secret \
--from-literal=INTERVAL=2 --from-file=./genid-web-config/
#조회
kubectl get secrets
NAME TYPE DATA AGE
ttabae-secret Opaque 2 46s #<--- TYPE Opaque 는 사용자 정의 타입이다
#yaml로 조회 --> base64 인코딩데이터가 보인다.
kubectl get secrets ttabae-secret -o yaml
#secret을 포함한 pod 생성
cat genid-env-secret.yaml
apiVersion: v1
kind: Pod
metadata:
name: genid-env-secret
spec:
containers:
- image: bluedove97/genid:env
env:
- name: INTERVAL
valueFrom:
secretKeyRef:
name: ttabae-secret
key: INTERVAL
name: fakeid-generator
volumeMounts:
- name: html
mountPath: /webdata
- image: nginx:1.14
name: web-server
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
readOnly: true
ports:
- containerPort: 80
volumes:
- name: html
emptyDir: {}