Kubernetes

쿠버네티스 (ConfigMap)

law and security 2024. 10. 4. 16:38

ConfigMap

 

 

< file을 통한 ConfigMap 생성>

[root@k8smaster config-files]# kubectl create configmap cowsay-config-fromfile \
> --from-file=foo.json \
> --from-file=bar=foobar.conf \
> --from-file=config-opts/ \
> --from-literal=sleep-interval=2
configmap/cowsay-config-fromfile created

[root@k8smaster config-files]# k get configmap
NAME                     DATA   AGE
cowsay-config-fromfile   5      39s
kube-root-ca.crt         1      283d

#생성된ConfigMap 확인

[root@k8smaster config-files]# kubectl get configmap cowsay-config-fromfile -o yaml
apiVersion: v1
data:
  bar: abc
  debug: "true"
  foo.json: "{\r\n  foo: bar,\r\n  bar: 5\r\n}\r\n"
  repeat: "100"
  sleep-interval: "2"
kind: ConfigMap
metadata:
  creationTimestamp: "2024-10-04T07:34:40Z"
  name: cowsay-config-fromfile
  namespace: default
  resourceVersion: "269876"
  uid: 4303d957-b888-4937-b71c-57f5c9b36ef5

 

 

 

 

< yaml 을 통한 ConfigMap 생성>


[root@k8smaster config-files]# cd ~/lab/07/config-yaml/

# yaml file을 통한 ConfigMap 생성

[root@k8smaster config-yaml]# k create -f cowsay-config.yaml
configmap/cowsay-config created

#생성된ConfigMap 확인

[root@k8smaster config-yaml]# k get cm
NAME                     DATA   AGE
cowsay-config            5      7s
cowsay-config-fromfile   5      6m16s
kube-root-ca.crt         1      283d
[root@k8smaster config-yaml]# k get cm cowsay-config -o yaml
apiVersion: v1
data:
  bar: abc
  debug: "true"
  foo.json: |
    {
      foo: bar,
      bar: 5
    }
  repeat: "100"
  sleep-interval: "2"
kind: ConfigMap
metadata:
  creationTimestamp: "2024-10-04T07:40:49Z"
  name: cowsay-config
  namespace: default
  resourceVersion: "270447"
  uid: 4aa58285-34ac-4b88-9310-a6afd741ec9d
[root@k8smaster config-yaml]# cat cowsay-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: cowsay-config
data:
  bar: abc
  debug: "true"
  foo.json: |
    {
      foo: bar,
      bar: 5
    }
  repeat: "100"
  sleep-interval: "2"

 

 

* 생성 확인 

 

[root@k8smaster ~]# k describe cm cowsay-config
Name:         cowsay-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
bar:
----
abc
debug:
----
true
foo.json:
----
{
  foo: bar,
  bar: 5
}

repeat:
----
100
sleep-interval:
----
2

BinaryData
====

Events:  <none>

 

 


* ConfigMap으로 설정한 엔트리중에서 일부만 가져와서 사용하기

 

 

1. 컨테이너의 환경변수로 ConfigMap 엔트리전달

 

[root@k8smaster ~]# cd ~/lab/07
[root@k8smaster 07]# ls
config-files     cowsay-env                        cowsay-pod-env-configmap-allentry.yaml
configmap-files  cowsay-pod-args-configmap.yaml    cowsay-pod-env-configmap.yaml
config-yaml      cowsay-pod-args.yaml              cowsay-pod-env.yaml
cowsay-args      cowsay-pod-configmap-volume.yaml  guestbook
[root@k8smaster 07]# kubectl create -f cowsay-pod-env-configmap.yaml
pod/cowsay-env-from-configmap created
[root@k8smaster 07]# k get pods
NAME                        READY   STATUS    RESTARTS      AGE
calculator-qd85p            1/1     Running   2 (33m ago)   4d23h
cowsay-env                  2/2     Running   2 (33m ago)   2d19h
cowsay-env-from-configmap   2/2     Running   0             9s
guestbook-7pfq2             1/1     Running   2 (33m ago)   4d23h
guestbook-959db856c-bnbct   1/1     Running   1 (33m ago)   2d19h
guestbook-959db856c-hqdsg   1/1     Running   1 (33m ago)   2d19h
guestbook-959db856c-nvt8s   1/1     Running   1 (33m ago)   2d19h

[root@k8smaster 07]# k get pods cowsay-env-from-configmap -o wide
NAME                        READY   STATUS    RESTARTS   AGE   IP              NODE       NOMINATED NODE   READINESS GATES
cowsay-env-from-configmap   2/2     Running   0          79s   10.102.249.48   k8snode1   <none>           <none>

 

 

[root@k8smaster 07]# kubectl port-forward cowsay-env-from-configmap 80:80
Forwarding from 127.0.0.1:80 -> 80
Forwarding from [::1]:80 -> 80


<새로운창 >

[root@k8smaster ~]# while true;do curl http://localhost ;sleep 2; done

 

 

-> 2초씩 바뀌는 것을 확인 

[root@k8smaster 07]# k exec cowsay-env-from-configmap -c html-generator -- env | sort
CALCULATOR_PORT_80_TCP_ADDR=10.101.244.101
CALCULATOR_PORT_80_TCP_PORT=80
CALCULATOR_PORT_80_TCP_PROTO=tcp
CALCULATOR_PORT_80_TCP=tcp://10.101.244.101:80
CALCULATOR_PORT=tcp://10.101.244.101:80
CALCULATOR_SERVICE_HOST=10.101.244.101
CALCULATOR_SERVICE_PORT=80
GUESTBOOK_HTTP_PORT_80_TCP_ADDR=10.111.55.145
GUESTBOOK_HTTP_PORT_80_TCP_PORT=80
GUESTBOOK_HTTP_PORT_80_TCP_PROTO=tcp
GUESTBOOK_HTTP_PORT_80_TCP=tcp://10.111.55.145:80
GUESTBOOK_HTTP_PORT=tcp://10.111.55.145:80
GUESTBOOK_HTTP_SERVICE_HOST=10.111.55.145
GUESTBOOK_HTTP_SERVICE_PORT=80
GUESTBOOK_PORT_80_TCP_ADDR=10.105.210.38
GUESTBOOK_PORT_80_TCP_PORT=80
GUESTBOOK_PORT_80_TCP_PROTO=tcp
GUESTBOOK_PORT_80_TCP=tcp://10.105.210.38:80
GUESTBOOK_PORT=tcp://10.105.210.38:80
GUESTBOOK_SERVICE_HOST=10.105.210.38
GUESTBOOK_SERVICE_PORT=80
HOME=/root
HOSTNAME=cowsay-env-from-configmap
INTERVAL=2
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games



[root@k8smaster 07]# cat cowsay-pod-args-configmap.yaml
apiVersion: v1
kind: Pod
metadata:
  name: cowsay-args-from-configmap
spec:
  containers:
  - image: yu3papa/cowsay:args
    env:
    - name: INTERVAL
      valueFrom:
        configMapKeyRef:
          name: cowsay-config
          key: sleep-interval
    args: ["$(INTERVAL)"]
    name: html-generator
    volumeMounts:
    - name: html
      mountPath: /var/htdocs
  - image: nginx:alpine
    name: web-server
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
      readOnly: true
    ports:
    - containerPort: 80
      protocol: TCP
  volumes:
  - name: html
    emptyDir: {}

 

 

-> 환경 변수 INTERVAL은 ConfigMap cowsay-config에서 sleep-interval 키의 값을 가져와 설정. 이 값은 컨테이너 실행 시 인자 args로 전달

 

 

 

 

2. ConfigMap 항목을 명령행 인자로 전달하기

 

[root@k8smaster 07]# k create -f cowsay-pod-args-configmap.yaml
pod/cowsay-args-from-configmap created
[root@k8smaster 07]# k get pods
NAME                         READY   STATUS              RESTARTS      AGE
calculator-qd85p             1/1     Running             2 (64m ago)   4d23h
cowsay-args-from-configmap   0/2     ContainerCreating   0             4s
cowsay-env                   2/2     Running             2 (64m ago)   2d20h
cowsay-env-from-configmap    2/2     Running             0             32m
guestbook-7pfq2              1/1     Running             2 (64m ago)   4d23h
guestbook-959db856c-bnbct    1/1     Running             1 (64m ago)   2d20h
guestbook-959db856c-hqdsg    1/1     Running             1 (65m ago)   2d20h
guestbook-959db856c-nvt8s    1/1     Running             1 (64m ago)   2d20h

 

 

[root@k8smaster 07]# kubectl port-forward cowsay-args-from-configmap 80:80
Forwarding from 127.0.0.1:80 -> 80
Forwarding from [::1]:80 -> 80


<새로운창 >

[root@k8smaster ~]# while true;do curl http://localhost ;sleep 2; done

 

#  html-generator 컨테이너의 환경변수를 출력하고 INTERVAL 환경변수가 존재하는지 확인


[root@k8smaster 07]# kubectl exec cowsay-args-from-configmap -c html-generator -- env | sort
CALCULATOR_PORT_80_TCP_ADDR=10.101.244.101
CALCULATOR_PORT_80_TCP_PORT=80
CALCULATOR_PORT_80_TCP_PROTO=tcp
CALCULATOR_PORT_80_TCP=tcp://10.101.244.101:80
CALCULATOR_PORT=tcp://10.101.244.101:80
CALCULATOR_SERVICE_HOST=10.101.244.101
CALCULATOR_SERVICE_PORT=80
GUESTBOOK_HTTP_PORT_80_TCP_ADDR=10.111.55.145
GUESTBOOK_HTTP_PORT_80_TCP_PORT=80
GUESTBOOK_HTTP_PORT_80_TCP_PROTO=tcp
GUESTBOOK_HTTP_PORT_80_TCP=tcp://10.111.55.145:80
GUESTBOOK_HTTP_PORT=tcp://10.111.55.145:80
GUESTBOOK_HTTP_SERVICE_HOST=10.111.55.145
GUESTBOOK_HTTP_SERVICE_PORT=80
GUESTBOOK_PORT_80_TCP_ADDR=10.105.210.38
GUESTBOOK_PORT_80_TCP_PORT=80
GUESTBOOK_PORT_80_TCP_PROTO=tcp
GUESTBOOK_PORT_80_TCP=tcp://10.105.210.38:80
GUESTBOOK_PORT=tcp://10.105.210.38:80
GUESTBOOK_SERVICE_HOST=10.105.210.38
GUESTBOOK_SERVICE_PORT=80
HOME=/root
HOSTNAME=cowsay-args-from-configmap
INTERVAL=2
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

 

 

전체 ConfigMap 데이터를 한꺼번에 가져와서 사용하기

 

[root@k8smaster 07]# kubectl create -f cowsay-pod-env-configmap-allentry.yaml
pod/cowsay-env-from-configmap-allentry created
[root@k8smaster 07]# k get pods
NAME                                 READY   STATUS    RESTARTS      AGE
calculator-qd85p                     1/1     Running   2 (71m ago)   4d23h
cowsay-args-from-configmap           2/2     Running   0             6m49s
cowsay-env                           2/2     Running   2 (71m ago)   2d20h
cowsay-env-from-configmap            2/2     Running   0             38m
cowsay-env-from-configmap-allentry   1/1     Running   0             22s
guestbook-7pfq2                      1/1     Running   2 (71m ago)   5d
guestbook-959db856c-bnbct            1/1     Running   1 (71m ago)   2d20h
guestbook-959db856c-hqdsg            1/1     Running   1 (71m ago)   2d20h
guestbook-959db856c-nvt8s            1/1     Running   1 (71m ago)   2d20h

# html-generator 컨테이너의 환경변수를 출력하고 ConfigMap 환경변수가 모두 존재하는지 확인

[root@k8smaster 07]# kubectl exec cowsay-env-from-configmap-allentry -c web-server -- env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=cowsay-env-from-configmap-allentry
NGINX_VERSION=1.27.2
PKG_RELEASE=1
DYNPKG_RELEASE=1
NJS_VERSION=0.8.6
NJS_RELEASE=1
foo.json={
  foo: bar,
  bar: 5
}

repeat=100
sleep-interval=2
bar=abc
debug=true
KUBERNETES_PORT_443_TCP_PORT=443
GUESTBOOK_PORT_80_TCP_PORT=80
CALCULATOR_SERVICE_HOST=10.101.244.101
KUBERNETES_PORT=tcp://10.96.0.1:443
GUESTBOOK_SERVICE_PORT=80
GUESTBOOK_PORT=tcp://10.105.210.38:80
GUESTBOOK_HTTP_SERVICE_HOST=10.111.55.145
CALCULATOR_SERVICE_PORT=80
CALCULATOR_PORT_80_TCP_PROTO=tcp
GUESTBOOK_SERVICE_HOST=10.105.210.38
GUESTBOOK_PORT_80_TCP_ADDR=10.105.210.38
GUESTBOOK_HTTP_PORT_80_TCP_PORT=80
CALCULATOR_PORT_80_TCP=tcp://10.101.244.101:80
KUBERNETES_SERVICE_PORT_HTTPS=443
GUESTBOOK_HTTP_SERVICE_PORT=80
GUESTBOOK_HTTP_PORT_80_TCP_ADDR=10.111.55.145
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP_PROTO=tcp
GUESTBOOK_PORT_80_TCP=tcp://10.105.210.38:80
GUESTBOOK_PORT_80_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
GUESTBOOK_HTTP_PORT_80_TCP=tcp://10.111.55.145:80
CALCULATOR_PORT=tcp://10.101.244.101:80
GUESTBOOK_HTTP_PORT=tcp://10.111.55.145:80
GUESTBOOK_HTTP_PORT_80_TCP_PROTO=tcp
CALCULATOR_PORT_80_TCP_PORT=80
CALCULATOR_PORT_80_TCP_ADDR=10.101.244.101
HOME=/root

 

[root@k8smaster 07]# k exec -it cowsay-env-from-configmap-allentry -c web-server -- sh
/ # cd /usr/share/nginx/html
/usr/share/nginx/html # ls
/usr/share/nginx/html # exit

 

-> 문서가 없어서 열리지 않는다.