backend/kubernetes in action

[kubernetes in action] 7. Configmaps and Secrets - 1

seul chan 2020. 6. 16. 23:40

7. ConfigMaps and Secrets configuring applications

  • Changing the main process of a container
  • Passing command-line options to the app
  • Setting environment variables exposed to the app
  • Configuring apps through ConfigMaps
  • Passing sensitive information through Secrets

거의 모든 app이 configuration을 필요로 하고 이를 app 자체에 담을 수 없으므로 kubernetes에서 어떻게 configuration option을 넘겨줄 수 있음

7.1. Configuring containerized application

configuration 부분을 건너 뛰면 명령줄의 인자로 이를 넘길 수 있음. configuration이 커질경우 그 때 config file로 만들어도 됨

또 다른 방법은 환경 변수를 사용하는 방법

왜 container 안에서 환경 변수를 사용하는 방법이 널리 사용될까? Docker container 안에서 configuration file을 사용하는게 까다롭기 때문
container image 안에 config file을 함께 넣을 수 없기 때문이다.

이런 문제를 해결하기 위해 Kubernetes의 ConfigMap 리소스를 사용 가능

ConfigMa을 사용하든 그렇지 않든 다음 방법으로 앱의 config를 수정할 수 있다.

  • Passing command-line arguments to containers
  • Setting custom environment variables for each container
  • Mounting configuration files into containers through a special type of volume

다음 장들에서 위의 내용을 다루면서 각각의 특징을 살펴봄

7.2. Passing command-line arguments to containers

Defining the command and arguments in Docker

컨테이너에서 실행되는 모든 명령어는 두 파트로 나뉜다: commandarguments

Dockerfile에서는 이 두 가지로 정의된다

  • ENTRYPOINT: container가 시작되면 실행
  • CMD: ENTRYPOINT에 전달되는 arguemtns

CMD를 사용하여 이미지가 실행될 때 execute 시킬 수 있지만 올바른 방법은 ENTRYPOINT를 사용하고 기본 인자를 수정할 필요가 있을 때에만 CMD를 사용하는 것이다.

이전 장에서 사용한 fortune image를 config를 변경할 수 있게 수정해보자.

fortune-args/fortuneloop.sh

#!/bin/bash
trap "exit" SIGINT
INTERVAL=$1
echo Configured to generate new fortune every $INTERVAL seconds
mkdir -p /var/htdocs
while :
do
  echo $(date) Writing fortune to /var/htdocs/index.html
  /usr/games/fortune > /var/htdocs/index.html
  sleep $INTERVAL
done

마찬가지로 fortune-args/Dockerfile도 만들어주자.

FROM ubuntu:latest
RUN apt-get update ; apt-get -y install fortune
ADD fortuneloop.sh /bin/fortuneloop.sh
ENTRYPOINT ["/bin/fortuneloop.sh"]
CMD ["10"]

이제 해당 이미지를 run 해보자

$ docker build -t docker.io/<username>/fortune:args .
$ docker push docker.io/<username>/fortune:args

$ docker run -it docker.io/<username>/fortune:args
Configured to generate new fortune every 10 seconds
Mon Jun 8 12:04:37 UTC 2020 Writing fortune to /var/htdocs/index.html

기본 10으로 설정된 sleep interval을 변경할 수 있다.

$ docker run -it docker.io/<username>/fortune:args 15
Configured to generate new fortune every 15 seconds

Overriding the command and arguments in Kubernetes

Kubernetes에서는 ENTRYPOINTCMD를 선택적으로 override 할 수 있다. (commandargs 사용)

은근 헷갈릴수 있을듯..

  • ENTRYPOINT -> command
  • CMD -> args
kind: Pod
spec:
  containers:
  - image: some/image
    command: ["/bin/command"]
    args: ["arg1", "arg2", "arg3"]

fortune pod도 argument를 받을 수 있게 수정해보자. fortune-pod-args.yaml

apiVersion: v1
kind: Pod
metadata:
  name: fortune2s                    1
spec:
  containers:
  - image: luksa/fortune:args        2
    args: ["2"]                      3
    name: html-generator
    volumeMounts:
    - name: html
      mountPath: /var/htdocs
...