Kubernetes

[Cloud] 10. Using Kubernetes Configmap

트리스탄1234 2023. 2. 10. 13:24
728x90
반응형

In this post, we will learn about the Configmap object in Kubernetes. All applications use environment variables, settings, and data used by the application. For example, the application refers to data such as API key or token password.

sourceof picture:Kubernetes in action

If you look at the picture above, there are two development systems and two production systems, and the environment setting values ​​or data used in the application are different. And if there are hundreds of target containers, managing them one by one can cause a lot of problems, such as a huge waste of time and failures due to errors. Of course, if there are only a few containers to manage, the operator can manage it with hardcondig. The bigger it gets, the more difficult it is to manage. Configmap is used for this purpose, and if all environment settings and data to be used in applications are defined in configmap and centrally managed, errors can be prevented and the operator's work can be reduced.

So, let's learn how to set environment variables step by step. First, assuming that there is a source code file called hardcode.js as shown below, hardcoding each step ==> OS environment variable ==> Dockerfile ==> Kubernetes Configmap Let's take a look at each in turn. What we want to do is to change the language and API key in the above source code step by step.

반응형

Hardcoding: First, create a hardcode.js file as shown below and save it after entering the following contents

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);

Then, install nodejs as shown below to run the file created above.

# 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

Then, execute the created file as shown below and send a service request to the web browser as shown below.

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

Then, when a web browser sends a request to http://localhost:3000, the language and API key values ​​are returned as shown below.

Language: English
API Key: 123456789

OS environment: This time, we will look at how to use the environment variables of the OS. After creating the envvar.js file, enter the following and save it.

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);

Next, put a value in the environment variable of the OS, run the created envvar.js, and send a request message to https://localhost:3000 through the web browser.

 

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

Specifying Environment Variables with DockerFile: This time, we will proceed with how to define configuration values ​​using Dockerfile. Create a Dockerfile as shown below and save the following contents to the file.

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"]

Now, let's build the created Dockerfile and run it.

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

If you call http://localhost:3000 in your browser, you can see the return value as shown below..

Language: English
API Key: 123456789

Using parameters in a Kubernetes YAML file Now, let's see how to store data values ​​using the YAML file of Kubernetes. After creating a yaml file as shown below, enter the following contents into the file.

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"

Now, if you distribute the created yaml file and send a request message to http:localhost:3000 after inquiring the information of svc, the result is shown below.

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
RESULT
Language: English
API Key: 123456789

Storing data using kubernetes secrets and configmaps If you save data using the Dockerfile and Yaml file seen above, there is a problem that you have to build and distribute the Dockerfile again when the value changes. The same goes for yaml. To solve this, let's try to store data using secret and Configmap. ​

First, let's create a secret and configmap object with the command below and look up their information.

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

Now, after creating a yaml file, enter the contents of the file with the following contents.

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의 객체 이름

Now, let's deploy the generated yaml and change the data of the secret object and config map after the pod is created. Unlike data storage using Dockerfile or YAML file, this time, after changing the values ​​of the created secret and Configmap objects, restart only the Pod to reflect the changed data. This means you don't have to create a distribution again.

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 -

After changing the value, if you send a request to http://localhost:3000 with a web browser, you can see the changed value as shown below.

Language: Spanish
API Key: 098765

This time, we will read the values ​​from a specific file when the application is running and read the file and set the environment variable using Dockerfile and Kubernetes while the application is running.

Save the LNAGUAGUE and API_KIY values ​​in each file through the echo command as shown below.

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

Then, create a file.js file as shown below, enter the following contents and save it

// 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);

Execute the file.js file to see if it is written properly and send a request to http://localhost:3000 with a web browser.

root@master-VirtualBox:~/test/config# node file.js
Language: English
API Key: 123456789

Reading real values ​​from a file using a Dockerfile. After writing the Dockerfile as shown below, let's deploy and run the container.

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

Now, if you send a service request to http://localhost:3000 from your browser, you will get the following result.

Language: English
API Key: 123456789

Change the config file here. If you send a request to http:localhost:3000 again, it shows the result with the changed value. That is, there is no need to re-deploy. You don't even need to restart the Pods.

root@master-VirtualBox:~/test/config# echo '{"LANGUAGE":"Spanish"}' > /lab/config2/config.json
Language: Spanish
API Key: 123456789

Creating Configmap and Secret objects from configuration files This time, we will look at the process of creating a Configmap and Secret object from a configuration file..

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

Let's check the created object.

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

Now, let's mount the Configmap and secret object created above as the Pod's Spce. Let's create a yaml file like the one below and put the following in the file and then distribute it.

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

If you change the configuration file in this state, you can see that it will be automatically applied to the Pod after a certain amount of time has elapsed. In other words, it is worth remembering that this method does not require re-build and restart of the Pod. ​ Then, change it with the command below and search with http:localhost:3000 to see the result with the changed value.

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 -

If you look at the result, you can see that the value has changed.

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