file 로 컨피그맵 생성

테스트 파일 생성

vagrant@ubuntu:~$ echo Hello, world! >> index.html
vagrant@ubuntu:~$ cat index.html
Hello, world!

테스트 파일(index.html)을 이용해서 index-file이라는 컨피그맵을 생성

vagrant@ubuntu:~$ kubectl create configmap index-file --from-file ./index.html
configmap/index-file created

생성한 index-file 컨피그맵을 확인

vagrant@ubuntu:~$ kubectl describe configmap index-file
Name:         index-file
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
index.html:        ⇐ 파일명이 키(key)로 사용
----
Hello, world!        ⇐ 파일의 내용이 값(value)로 사용

Events:  <none>

키이름을 직접 지정해서 컨피그맵을 생성

vagrant@ubuntu:~$ kubectl create configmap index-file-customkey --from-file myindex=./index.html
configmap/index-file-customkey created

vagrant@ubuntu:~$ kubectl describe configmap index-file-customkey
Name:         index-file-customkey
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
myindex:        ⇐ 컨피그맵 생성 시 지정한 키 이름을 사용
----
Hello, world!

Events:  <none>

여러 개의 키-값 형태의 내용으로 구성된 설정 파일을 한번에 컨피그맵으로 설정

키-값 형태의 내용으로 구성된 설정 파일을 생성

vagrant@ubuntu:~$ vi ./multiple-keyvalue.env

mykey1=myvalue1
mykey2=myvalue2
mykey3=myvalue3

설정 파일에 정의된 키-값 형태를 컨피그맵의 키-값 항목으로 일괄 전환

kubectl create configmap abcd --from-literal mykey1=myvalue1 --from-literal mykey2=myvalue2 --from-literal mykey3=myvalue3 … 형식의 명령어를 파일을 이용해서 구현

vagrant@ubuntu:~$ kubectl create configmap from-envfile --from-env-file ./multiple-keyvalue.env
configmap/from-envfile created
vagrant@ubuntu:~$ kubectl describe configmap from-envfile
Name:         from-envfile
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
mykey1:
----
myvalue1
mykey2:
----
myvalue2
mykey3:
----
myvalue3
Events:  <none>

YAML 파일로 컨피그맵을 정의

컨피그맵을 실제로 생성하지 않고 YAML 형식으로 출력

vagrant@ubuntu:~$ kubectl create configmap my-configmap --from-literal mykey=myvalue --dry-run -o yaml
W0922 04:34:05.917495  358937 helpers.go:553] --dry-run is deprecated and can be replaced with --dry-run=client.
apiVersion: v1
data:
  mykey: myvalue
kind: ConfigMap
metadata:
  creationTimestamp: null
  name: my-configmap

vagrant@ubuntu:~$ kubectl get configmap
NAME                   DATA   AGE        ⇒ my-configmap 이름의 컨피그맵이 존재하지 않음
from-envfile           3      4m2s           → --dry-run 옵션: 실제로 컨피그맵 오브젝트를 생성하지는 않음
index-file             1      16m               
index-file-customkey   1      12m
log-level-configmap    1      3h21m
start-k8s              2      3h20m

YAML 형식의 출력을 YAML 파일로 저장

vagrant@ubuntu:~$ kubectl create configmap my-configmap --from-literal mykey=myvalue --dry-run -o yaml > my-config.yml
W0922 04:42:17.088587  360577 helpers.go:553] --dry-run is deprecated and can be replaced with --dry-run=client.
vagrant@ubuntu:~$

vagrant@ubuntu:~$ cat my-config.yml
apiVersion: v1
data:
  mykey: myvalue
kind: ConfigMap
metadata:
  creationTimestamp: null
  name: my-configmap

YAML 파일로 컨피그맵을 생성

vagrant@ubuntu:~$ kubectl apply -f my-config.yml
configmap/my-configmap created

vagrant@ubuntu:~$ kubectl get configmaps
NAME                   DATA   AGE
from-envfile           3      13m
index-file             1      26m
index-file-customkey   1      22m
log-level-configmap    1      3h31m
my-configmap           1      9s
start-k8s              2      3h30m

 

반응형

+ Recent posts