How to configure the application

Today we are going to share ConfigMap resources. Before sharing, let’s take a look at how we run the application.

In the previous applications, no configuration is written, at most volumes are used to store data

So how to pass in the configuration in the application?

How to use ENTRYPOINT and CMD in Dockerfile

Write a simple small case to simulate

image-20220128162418555

The general idea is this:

  • 1 Simply write a script echo.sh , output a string of information on the page as a timing INTER second, you can pass in a parameter as a specific timing
#!/bin/bash

INTER=$1

while :
do
  echo "echo info test  -- "$INTER
  sleep $INTER

done
  • 2 Write Dockerfile, ADD echo.sh, and pass in the timing
FROM ubuntu:latest
ADD echo.sh /bin/echo.sh
ENTRYPOINT ["/bin/echo.sh"]
CMD ["2"]
  • 3 Build the Dockerfile into an image , for the specific docker hub warehouse, please use your own account
docker build -t xiaomotong888/echoinfotest
docker push xiaomotong888/echoinfotest
  • 4 Write yaml and generate pod
apiVersion: v1
kind: Pod
metadata:
  name: echoinfo
spec:
  containers:
  - image: xiaomotong888/echoinfotest
    name: echoinfo
  • 5 View the effect

Through the above case, we can see that we can add the parameters of the executable program in the way of CMD in the Dockerfile, which can achieve the desired parameter passing effect

Then let's see if we can use a similar method in the k8s yaml list?

Use the yaml file in k8s to pass parameters to the image

The practice and the above types, the related codes involved are displayed in the form of screenshots

  • Modify the above list file and pass in the parameters in the yaml file. This time, modify it to pass in a timing of 3 seconds

If there are many parameters in args here, we can also write one parameter per line:

args:
- xiaozhu
- canshu2
- "100"
  • View the effect

Use the yaml list to pass in the parameters we need, which can still meet our requirements

How to use environment variables

Now let's use the third method, instead of passing parameters, let's set the environment variable INTER in the yaml list, and let the script directly read the value of the environment variable

  • 1 Write an echo_env.sh script to read environment variables
#!/bin/bash

while :
do
  echo "echo info test  -- "$INTER
  sleep $INTER

done
  • 2 Write Dockerfile, ADD echo.sh, execute
FROM ubuntu:latest
ADD echo_env.sh /bin/echo_env.sh
ENTRYPOINT ["/bin/echo_env.sh"]
  • 3 Make a mirror image
docker build -t xiaomotong888/echoinfotest-env
docker push xiaomotong888/echoinfotest-env
  • 4 Write yaml list, set environment variables, create pod
apiVersion: v1
kind: Pod
metadata:
  name: echoinfo-k8s-env
spec:
  containers:
  - image: xiaomotong888/echoinfotest-env
    name: echoinfo-k8s-env
    env:
    - name: INTER
      value: "5"
  • View the effect

Sure enough, all three methods are ok. Friends, do you feel that your needs have been met?

But it should be noted that this is hard-coded, either hard-coded in the generated image, or hard-coded in the yaml list, so if I want to adjust my timing now, do I have to make another mirror image , or redo a pod?

How to use ConfigMap

Then in k8s, there must be a better way to solve such problems, so ConfigMap began to show its talents

The purpose of using ConfigMap is to decouple the configuration of the service. In which environment the service is deployed, we can directly modify the ConfigMap for adaptation. There is no need to modify the image and pod, let alone the source code of our service program

Think about it, is it more efficient to use it this way?

What exactly is ConfigMap?

ConfigMap is an object that stores key-value pair mapping, and is also a resource in k8s. This mapping relationship can be a key mapping a value, or a file mapping

There is one thing to note:

At first glance, do you think that the service in the container directly reads the data of the ConfigMap ? nonono ,

In fact, it is like this, draw a picture to express it visually:

For the ConfigMap resource, the container is not aware of it . The container actually obtains the configuration by reading the volume or environment variable in the environment. In fact, the container does not need to know the existence of the ConfigMap. It is simple and good.

How to create ConfigMaps?

To create a ConfigMap resource, we can directly write a yaml list in the way we are familiar with, and then use the kubectl create -f xxx.yamlway to create a cm resource

The cm here is the abbreviation of ConfigMap

We can also use the command line to create a ConfigMap for our use. For example, we continue the above example, we add a key-value mapping relationship INTER=10 to the ConfigMap

kubectl create configmap echo-config --from-literal=INTER=10

Here you can see that kubectl create configmap is used to create ConfigMap. When we create resources, if we need to add multiple key-value pairs, we can also add:

--from-literal=xxx=xxx --from-literal=xx=xx

Then you can view the details of cm through the following command

kubectl get cm

kubectl describe cm cm-name

From the above information, we can see that the information in the echo-config resource contains our key-value pair INTER=10

You can check what the yaml resource corresponding to the cm is like

kubectl describe cm echo-config -o yaml

If there are many key-values ​​that need to be added, then it is not a solution for us to write them one by one, it is too poking

Then we can have a key corresponding to a file, for example, we can do this

We can use the parameter --from-file to specify the key and file, for example, use the following command

kubectl create configmap my-config --from-file=key1=config.json

Of course, we can also specify a directory after –from-file , then k8s will read the file in the specified directory, read the problem according to the command specification of k8s, if no key is specified, then the file name is the key

draw a picture to illustrate

For a ConfigMap we can set multiple files, multiple direct keys and values

So how to read the data of ConfigMap?

We can read the corresponding key value from the specified ConfigMap when creating the pod's yaml list, and the method of use can be used in the same way as reading environment variables

  • Write the yaml list, specify the ConfigMap and specify the corresponding key value

echoinfotest_configmap.yaml

apiVersion: v1
kind: Pod
metadata:
  name: echoinfo-k8s-configmap
spec:
  containers:
  - image: xiaomotong888/echoinfotest-env
    name: echoinfo-k8s-env
    env:
    - name: INTER
      valueFrom:
        configMapKeyRef:
          name: echo-config
          key: INTER

Do you still remember the value of INTER we wrote in echo-config ?

After we create the pod, check the log of the corresponding pod, we can see that what is printed is

echo info test  -- 10
echo info test  -- 10
echo info test  -- 10

Obviously, what is printed is 10 , the data is the data read from our ConfigMap, nothing wrong, old iron

When we are running the pod now, let's modify the cm

kubectl edit cm echo-config

If we need to allow our pod to read the 8-second timing, we can delete the pod and then create a pod. We can also use RC/RS to make the pod restart by modifying the number of replicas. , you can share other ways to restart the pod later

That’s all for today, what I have learned, if there is any deviation, please correct me

Welcome to like, follow and collect

Friends, your support and encouragement is my motivation to keep sharing and improve quality

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-q0RK65w1-1689479652770)(https://gitee.com/common_dev/mypic/raw/master/%E6% B5%B7%E6%8A%A5.png)]

Ok, that's it for this time

Technology is open, and our mentality should be even more open. Embrace change, live in the sun, and strive to move forward.

I am A Bing Yunyuan , welcome to like, follow and collect, see you next time~
For more, you can check the live broadcast of Zero Sound at 8 o'clock every night: h ttps://ke.qq.com/course/417774

Guess you like

Origin blog.csdn.net/m0_37322399/article/details/131748830