kimhope 2022. 6. 29. 17:59
728x90

ConfigMap

 


컨피그맵이란?

컨테이너에 필요한 환경 설정을 컨테이너와 분리하여 제공하는 기능

  • 별도의 데이터 저장을 위한 오브젝트
  • 같은 이미지를 이용해서 설정이 다른 컨테이너를 실행하기 위해 사용
  • 컨피그맵을 설정하고 파드(컨테이너)에서 그 값을 사용함 => 전체 또는 일부 값을 호출하여 사용
  • 환경 변수로 호출: 변수의 값으로 설정
  • volume 형태로 마운트: 파일

 


 

실습 - 1

env 변수로 어플리케이션에 변수 값 전달

  • env 변수가 정의된 파드 생성
$ cat myapp-pod-env.yml
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod-env
spec:
  containers:
  - name: myapp
    image: ghcr.io/c1t1d0s7/go-myweb:alpine
    env:
    - name: MESSAGE
      value: "Customized Hello World!"
    ports:
    - containerPort: 8080
      protocol: TCP

 

  • 파드 생성
$ kubectl create -f myapp-pod-env.yml
pod/myapp-pod-env created

 

  • 8080 포트 사용 확인
$ netstat -antp | grep 8080
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 10.0.2.15:55440         10.233.89.39:8080       TIME_WAIT   -
tcp        0      0 10.0.2.15:47172         10.233.89.37:8080       TIME_WAIT   -
tcp        0      0 10.0.2.15:55024         10.233.89.39:8080       TIME_WAIT   -
tcp        0      0 10.0.2.15:55190         10.233.89.39:8080       TIME_WAIT   -
tcp        0      0 10.0.2.15:47088         10.233.89.37:8080       TIME_WAIT   -

 

  • myapp-pod-env 파드를 8080포트로 포트 포워딩
$ kubectl port-forward myapp-pod-env 8080:8080
Forwarding from 127.0.0.1:8080 -> 8080
Forwarding from [::1]:8080 -> 8080
Handling connection for 8080

 

실행 결과

  • 새 터미널을 열고 실행
  • env 변수로 전달되어 출력되는 것을 확인할 수 있음
vagrant@kube-master1:~
$ curl http://localhost:8080
Customized Hello World!
myapp-pod-env

 


 

실습 - 2

configMapRef: 컨피그맵 참조

  • 컨피그맵 설정 전체를 불러오는 파드 생성
$ cat myapp-pod-cm.yml
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod-cm
spec:
  containers:
  - name: myapp
    image: ghcr.io/c1t1d0s7/go-myweb:alpine
    env:
    - name: MESSAGE
      valueFrom:
        configMapKeyRef:
          name: myapp-message
          key: message
    args:
    - $(MESSAGE)
    ports:
    - containerPort: 8080
      protocol: TCP

 

  • 참조할 message 파일 생성
$ cd ~
$ mkdir configmap
$ echo  "Customized Hello World!" > configmap/message

vagrant@kube-master1:~/configmap
$ ls
message  myapp-pod-cm.yml

 

  • myapp-message 컨피그맵 생성
vagrant@kube-master1:~/configmap
$ kubectl create configmap myapp-message --from-file=.
configmap/myapp-message created

vagrant@kube-master1:~/configmap
$ kubectl get configmap
NAME            DATA   AGE
myapp-message   3      23s

 

  • 파드 생성
$ kubectl create -f myapp-pod-cm.yml
pod/myapp-pod-cm created

vagrant@kube-master1:~/configmap
$ kubectl get pod
NAME           READY   STATUS    RESTARTS   AGE
myapp-pod-cm   1/1     Running   0          5s

 

  • 파드 포트포워딩
vagrant@kube-master1:~/configmap
$ kubectl port-forward myapp-pod-cm 8080:8080
Forwarding from 127.0.0.1:8080 -> 8080
Forwarding from [::1]:8080 -> 8080

 

 

실행 결과

  • 새 터미널에서 실행
vagrant@kube-master1:~
$ curl http://localhost:8080
Customized Hello World!

myapp-pod-cm

 


 

실습 - 3

컨테이너의 볼륨 형식으로 컨피그맵을 설정해 파일로 컨테이너에 제공

  • 마운트되면서 마운트 하기 전부터 존재했던 기존 파일에 접근이 안됨(사용할 수 없음) => 리눅스 특징: 파일 개별로 마운트 가능
  • 호스트에서 conf 디렉토리 생성
vagrant@kube-master1:~
$ mkdir conf/

vagrant@kube-master1:~
$ cd conf

 

  • config 파일 생성
$ cat conf/nginx-gzip.conf
server {
    listen                         80;
    server_name             myapp.example.com;
    gzip on;
    gzip_types text/plain application/xml;
    location / {
        root /usr/share/nginx/html;
        index index.html;
    }
}

 

  • 컨피그맵 생성
$ kubectl create configmap nginx-gzip-config --from-file=nginx-gzip.conf
configmap/nginx-gzip-config created

 

  • 생성된 컨피그맵 정보 확인
$ kubectl get configmap
NAME                DATA   AGE
nginx-gzip-config   1      4s

$ kubectl describe configmap nginx-gzip-config
Name:         nginx-gzip-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
nginx-gzip.conf:
----
server {
    listen                         80;
    server_name             myapp.example.com;
    gzip on;
    gzip_types text/plain application/xml;
    location / {
        root /usr/share/nginx/html;
        index index.html;
    }
}

Events:  <none>

 

  • 파드 생성 파일
  • 볼륨이 .spec.containers.volumeMount.mountPath 필드로 마운트함
$ cat nginx-pod-compress.yml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod-compress
spec:
  containers:
  - name: nginx-compress
    image: nginx
    volumeMounts:
    - name: nginx-compress-config
      mountPath: /etc/nginx/conf.d
    ports:
    - containerPort: 80
      protocol: TCP
  volumes:
  - name: nginx-compress-config
    configMap:
      name: nginx-gzip-config

 

  • 파드 생성
$ kubectl create -f nginx-pod-compress.yml
pod/nginx-pod-compress created

 

  • 호스트에서 8080 포트를 사용하고 있는지 확인 => 10.0.2.15:8080이 있는지 확인
  • 현재 사용하고 있지 않으므로 8080으로 포트 포워딩 가능
$ netstat -natp | grep :8080
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 10.0.2.15:48024         10.233.89.40:8080       TIME_WAIT   -   
tcp        0      0 10.0.2.15:50058         10.233.89.42:8080       TIME_WAIT   -

 

  • 파드에 접속
$ kubectl port-forward nginx-pod-compress 8080:80
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80

 

 

실행 결과

  • -v 옵션: 헤더 정보 확인
$ curl http://localhost:8080 -v
* Rebuilt URL to: http://localhost:8080/
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.23.0
< Date: Thu, 30 Jun 2022 02:41:04 GMT
< Content-Type: text/html
< Content-Length: 615
< Last-Modified: Tue, 21 Jun 2022 14:25:37 GMT
< Connection: keep-alive
< ETag: "62b1d4e1-267"
< Accept-Ranges: bytes
<
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

 

  • -H 옵션: 헤더 추가
$ curl -H "Accept-Encoding: gzip" http://localhost:8080 -v
* Rebuilt URL to: http://localhost:8080/
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.58.0
> Accept: */*
> Accept-Encoding: gzip
>
< HTTP/1.1 200 OK
< Server: nginx/1.23.0
< Date: Thu, 30 Jun 2022 02:45:28 GMT
< Content-Type: text/html
< Last-Modified: Tue, 21 Jun 2022 14:25:37 GMT
< Transfer-Encoding: chunked
< Connection: keep-alive
< ETag: W/"62b1d4e1-267"
< Content-Encoding: gzip
<
Warning: Binary output can mess up your terminal. Use "--output -" to tell
Warning: curl to output it to your terminal anyway, or consider "--output
Warning: <FILE>" to save to a file.
* Failed writing body (0 != 397)
* Failed writing data
* stopped the pause stream!
* Closing connection 0
728x90