쿠버네티스

[클라우드] 10. 쿠버네티스 Configmap 사용하기.

트리스탄1234 2023. 8. 20. 16:19
728x90
반응형

이번 포스팅에서는 쿠버네티스의 Configmap 객체에 대해서 알아 보도록 하겠습니다. 모든 어플리케이션은 어플리케이션에서 사용하는 환경 변수나 설정값과 데이터들을 사용을 하는데. 예를 들면 API Key나 토큰 비밀번호 등등의 데이터를 어플리케이션에서는 참조를 합니다.

그림출처:Kubernetes in action

위의 그림을 보면 개발(development)시스템과 운영(production)시스템 두개가 있는데 각각 환경 설정 값이나 어플리케이션에서 사용하는 data가 다를경우. 그리고 대상 container가 수백대일 경우 운영자가 일일이 관리를 하는건 엄청난 시간낭비와 오류로 인한 장애 발생등 많은 문제를 발생시킬 수 있습니다. 물론 관리 하는 컨테이너가 몇개 되지 않는다면 운영자가 hardcondig으로 관리를 할 수 있겠지만. 규모가 커질수록 관리가 힘들어 집니다.

이를 위해 사용하는것이 Configmap이고 모든 환경 설정값이나 어플리케이션에서 사용할 데이터들을 configmap에 정의를 하고 중앙 집중 관리를 하게 하면 오류도 방지하고 운영자의 작업도 많이 덜어낼수 있습니다.

그럼 단계별로 환경변수를 설정하는 방법에 대해서 하나씩 알아 봅시다 우선 여기 아래와 같이 hardcode.js라는 소스코드 파일이 있다고 가정을 하고 각 단계 하드코딩 ==> OS환경 변수 ==> Dockerfile ==> Kubernetes Configmap의 순으로 각각 살펴 보도록 하겠 습니다.

우리가 하고자 하는 것은 위의 소스코드 중 Language와 API key를 단계별로 변경하고자 하는 것입니다.

  • Hardcoding: 우선 아래와 같이 hardcode.js 파일을 생성하고 아래의 내용을 입력 후 저장을 합니다
root@master-VirtualBox:~/test# mkdir config
root@master-VirtualBox:~/test# cd config
root@master-VirtualBox:~/test/config# vi hardcode.js
// Copyright 2017, Google, Inc.
// Licensed under the Apache License, Version 2.0 (the "License")
var http = require('http');
var server = http.createServer(function (request, response) {
const language = 'English';
const API_KEY = '123456789';
response.write(`Language: ${language}\n`);
response.write(`API Key: ${API_KEY}\n`);
response.end(`\n`);
});
server.listen(3000);

그럼 위에서 생성한 파일을 실행시키기 위해 아래와 같이 nodejs를 설치를 합니다.

# apt-get update
# curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -
# apt-get install -y nodejs
# apt-get install -y build-essential
# apt-get install -y npm

그럼 이제 생성한 파일을 아래와 같이 실행을 시키고 웹브라우저로 아래와 같이 서비스 요청을 보내 보겠습니다.

root@master-VirtualBox:~/test# node hardcode.js​

그럼 웹브라우저에서 http://localhost:3000으로 요청을 보내보면 아래와 같이 Language와 API Key값을 리턴을 해줍니다.

Language: English
API Key: 123456789
  • OS environment: 이번에는 OS의 환경 변수를 이용하는 방법에 대해서 살펴 보겠 습니다. envvar.js파일을 생성한 후에 아래의 내용을 입력 후 저장을 합니다.
root@master-VirtualBox:~/test# mkdir config
root@master-VirtualBox:~/test# cd config
root@master-VirtualBox:~/test/config# vi envvar.js
// Copyright 2017, Google, Inc.
// Licensed under the Apache License, Version 2.0 (the "License")
var http = require('http');
var server = http.createServer(function (request, response) {
const language = process.env.LANGUAGE;
const API_KEY = process.env.API_KEY;
response.write(`Language: ${language}\n`);
response.write(`API Key: ${API_KEY}\n`);
response.end(`\n`);
});
server.listen(3000);

다음으로 OS의 환경 변수에 값을 넣어 주고 생성한 envvar.js를 실행을 시키고 웹드라우저로 https://localhost:3000 으로 요청메세지를 보냅니다.

root@master-VirtualBox:~/test/config# export LANGUAGE=English
root@master-VirtualBox:~/test/config#export API_KEY=123456789
root@master-VirtualBox:~/test/config#node envvars.js
엡브라우저에서 http://localhost:3000 실행
Language: English
API Key: 123456789
  • DockerFile로 환경 변수 지정하기: 이번에는 Dockerfile을 이용해서 설정값들을 정의하는 방법에 대해서 진행을 해보겠습니다. 아래와 같이 Dockerfile을 생성을 하고 아래의 내용을 파일에 저장을 합니다.
root@master-VirtualBox:~/test/config# vi /test/config1/Dockerfile
# Copyright 2017, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")
FROM node:8
EXPOSE 3000
ENV LANGUAGE English
ENV API_KEY 123456789
RUN mkdir -p /lab/config1/
COPY . /lab/config1/
WORKDIR /lab/config1/
CMD ["node", "envvars.js"]

이제 생성한 Dockerfile을 build한 후 실행을 시켜 보겠 습니다.

root@master-VirtualBox:~/test/config# docker build -t localhost:5000/envtest:v1
root@master-VirtualBox:~/test/config# docker run -d -p 3000:3000 localhost:5000/envtest:v1

브라우저에서 http://localhost:3000을 호출을 해보면 아래와 같이 리턴값을 볼수 있습니다.

Language: English
API Key: 123456789
  • 쿠버 네티스 YAML파일의 파라메터 이용하기

그럼 이번에는 쿠버네티스의 YAML파일을 이용해서 데이터값을 저장하는 방법을 알아보겠습니다.

아래와 같이 yaml파일을 생성 후 아래의 내용을 파일에 입력 합니다.

root@master-VirtualBox:~/test/config#vi /lab/config1/embededenv.yaml
apiVersion: v1
kind: Service
metadata:
name: envtest
labels:
name: envtest
spec:
type: NodePort
ports:
- port: 3000
targetPort: 3000
protocol: TCP
selector:
name: envtest
---
# Copyright 2017, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: envtest
spec:
replicas: 1
template:
metadata:
labels:
name: envtest
spec:
containers:
- name: envtest
image: localhost:5000/envtest:v1
imagePullPolicy: Never
ports:
- containerPort: 3000
env:
- name: LANGUAGE
value: "English"
- name: API_KEY
value: "123456789"

이제 작성한 yaml파일을 배포하고 svc의 정보를 조회한 후에 http:localhost:3000 으로 요청 메세지를 보내 보면 그 결과가 아래와 같이 나옵니다.

root@master-VirtualBox:~/test/config# kubectl apply -f /lab/config1/embededenv.yaml
root@master-VirtualBox:~/test/config#kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
envtest NodePort 172.168.1.191 <none> 3000:31256/TCP 14s
결과값
Language: English
API Key: 123456789
  • 쿠버네티스 secret과 configmap을 사용해 데이터 저장하기

위에서본 Dockerfile과 Yaml파일을 이용해 데이터를 저장을 하면 문제가 값이 변경이 될때 Dockerfile로 다시 Build를 하고 베포를 해야 한다는 문제가 있습니다. yaml도 마찬가지 입니다 . 이를 해결 하기 위해서 secret와 Configmap을 사용해 데이터를 저장해 보도록 하겠습니다 .

우선 아래 명령어로 secret와 configmap 객체를 생성을 하고 그 정보를 조회해 보겠 습니다.

root@master-VirtualBox:~/test/config# kubectl create secret generic apikey --from-literal=API_KEY=123456789
root@master-VirtualBox:~/test/config#kubectl create configmap language --from-literal=LANGUAGE=English
root@master-VirtualBox:~/test/config#kubectl get secret
NAME TYPE DATA AGE
apikey Opaque 1 7s
root@master-VirtualBox:~/test/config## kubectl get configmap
NAME DATA AGE
language 1 6s

그럼 이제 yaml 파일을 생성한 후 아래와 같은 내용으로 파일의 내용을 입력 합니다.

root@master-VirtualBox:~/test/config# vi /lab/config1/final.yaml
apiVersion: v1
kind: Service
metadata:
name: envtest
labels:
name: envtest
spec:
type: NodePort
ports:
- port: 3000
targetPort: 3000
protocol: TCP
selector:
name: envtest
---
# Copyright 2017, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: envtest
spec:
replicas: 1
template:
metadata:
labels:
name: envtest
spec:
containers:
- name: envtest
image: localhost:5000/envtest:v1
imagePullPolicy: Never
ports:
- containerPort: 3000
env:
- name: LANGUAGE
valueFrom:
configMapKeyRef:
name: language
key: LANGUAGE ==> kubectl create로 생성한 Configmap의 객체 이름
- name: API_KEY
valueFrom:
secretKeyRef:
name: apikey
key: API_KEY ==> kubectl create로 생성한 secret의 객체 이름

이제 생성된 yaml을 배포하고 Pod가 생성된 후 secret 객체와 config map의 데이터를 변경을 해보겠습니다. Dockerfile이나 YAML파일을 이용한 데이터 저장과 다르게 이번에는 생성된 secret객체와 Configmap객체의 값을 변경한 후 Pod만 restart를 해주면 변경된 데이터가 반영이 됩니다. 즉 다시 배포 생성할 필요가 없습니다.

root@master-VirtualBox:~/test/config## kubectl create configmap language --from-literal=LANGUAGE=Spanish \
-o yaml --dry-run | kubectl replace -f -
root@master-VirtualBox:~/test/config## kubectl create secret generic apikey --from-literal=API_KEY=098765 \
-o yaml --dry-run | kubectl replace -f -

값을 변경 후 웹브라우저로 http://localhost:3000으로 요청을 보내면 아래와 같이 변경된 값을 볼수 있습니다.

Language: Spanish
API Key: 098765

이번에는 어플리케이션이 구동될때 특정 파일로 부터 값들을 읽어 들여서 어플리케이션이 구동되는 절차중에 Dockerfile와 Kubernetes를 이용해서 파일을 읽어 들이고 환경 변수를 설정하는 절차를 진행해 보겠 습니다.

아래와 같이 echo명령을 통해서 LNAGUAGUE와 API_KIY값을 각각의 파일에 저장을 합니다

root@master-VirtualBox:~/test/config# echo '{"LANGUAGE":"English"}' > /lab/config2/config.json
root@master-VirtualBox:~/test/config# echo '{"API_KEY":"123456789"}' > /lab/config2/secret.json

그리고 아래와 같이 file.js 파일을 생성하여 아래 내용을 입력 후 저장을 합니다.

// Copyright 2017, Google, Inc.
// Licensed under the Apache License, Version 2.0 (the "License")
var http = require('http');
var fs = require('fs');
var server = http.createServer(function (request, response) {
fs.readFile('/lab/config2/config.json', function (err, config) {
if (err) return console.log(err);
const language = JSON.parse(config).LANGUAGE;
fs.readFile('/lab/config2/secret.json', function (err, secret) {
if (err) return console.log(err);
const API_KEY = JSON.parse(secret).API_KEY;
response.write(`Language: ${language}\n`);
response.write(`API Key: ${API_KEY}\n`);
response.end(`\n`);
});
});
});
server.listen(3000);

정상적으로 작성이 되었는지 file.js파일을 실행을 하고 웹브라우저로 http://localhost:3000으로 요청을 보냅니다.

root@master-VirtualBox:~/test/config# node file.js
Language: English
API Key: 123456789
  • Dockerfile을 이용해 파일로 부터 실정값 읽어 들이기.

아래와 같이 Dokerfile을 작성을 한후에 배포하고 컨테이너를 실행해 보겠습니다.

root@master-VirtualBox:~/test/config# vi /lab/config2/Dockerfile
# Copyright 2017, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")
FROM node:8
EXPOSE 3000
VOLUME /lab/config2/
COPY . /lab/config2/
WORKDIR /lab/config2/
CMD ["node", "file.js"]
root@master-VirtualBox:~/test/config# docker build -t localhost:5000/envtest:v2 .
root@master-VirtualBox:~/test/config# docker run -d -p 3000:3000 -v /lab/config2/:/lab/config2/ localhost:5000/envtest:v2

이제 브라우저에서 http://localhost:3000으로 서비스 요청을 보내보면 아래와 같은 결과값이 나옵니다.

Language: English
API Key: 123456789

여기서 설정파일 아래로 바꾸고. http:localhost:3000으로 다시 요청을 보내면 변경된 값으로 결과값을 보여 줍니다. 즉 새로 배포할 필요도 없고. Pod를 restart할 필요도 없습니다.

root@master-VirtualBox:~/test/config# echo '{"LANGUAGE":"Spanish"}' > /lab/config2/config.json
Language: Spanish
API Key: 123456789
  • 설정 파일로 부터 Configmap과 Secret객체 생성하기

이번에는 설정파일로 부터 Configmap과 Secret객체를 생성하는 과정을 살펴 보겠 습니다.

root@master-VirtualBox:~/test/config## kubectl create secret generic my-secret --from-file=/lab/config2/secret.json
root@master-VirtualBox:~/test/config##kubectl create configmap my-config --from-file=/lab/config2/config.json

생성된 객체를 조회를해 보겠 습니다.

root@master-VirtualBox:~/test/config## kubectl get secret
NAME TYPE DATA AGE
apikey Opaque 1 7s
root@master-VirtualBox:~/test/config## kubectl get configmap
NAME DATA AGE
language 1 6s

그럼 이제 위에서 만든 Configmap과 secret의 객체를 Pod의 Spce으로 마운트를해 보겠습니다.

아래와 같은 yaml파일 만들고 파일안에 아래의 내용을 입력 후에 배포를 해보겠 습니다.

root@master-VirtualBox:~/test/config## vi /lab/config2/file.yaml
apiVersion: v1
kind: Service
metadata:
name: envtest
labels:
name: envtest
spec:
type: NodePort
ports:
- port: 3000
targetPort: 3000
protocol: TCP
selector:
name: envtest
---
# Copyright 2017, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: envtest
spec:
replicas: 1
template:
metadata:
labels:
name: envtest
spec:
containers:
- name: envtest
image: localhost:5000/envtest:v2
ports:
- containerPort: 3000
volumeMounts:
- name: my-config
mountPath: /lab/config2
- name: my-secret
mountPath: /lab/config2
volumes:
- name: my-config
configMap:
name: my-config
- name: my-secret
secret:
secretName: my-secret
root@master-VirtualBox:~/test/config##kubectl apply -f fiole.yaml

이상태에서 설정파일을 바꾸면 일정 시간이 경과후에 자동으로 Pod에 적용이 될것을 볼수 있습니다. 즉 이 방식은 재 build와 Pod의 restart가 불필요 하다는 점을 기억하면 될것 같습니다.

그럼 아래 명령으로 변경을 하고 http:localhost:3000으로 조회를 해보면 변경된 값으로 결과값을 볼수가 있습니다.

root@master-VirtualBox:~/test/config## echo '{"LANGUAGE":"Klingon"}' > /lab/config2/config.json
root@master-VirtualBox:~/test/config## kubectl create configmap my-config \
--from-file=/lab/config2/config.json \
-o yaml --dry-run | kubectl replace -f -

결과값을 보면 값이 변경된 것을 볼수 있습니다.

Language: Klingon
API Key: 123456789
728x90
반응형