Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Kubernetes's configmap to inject into pod through environment variables

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Kubernetes's configmap to inject into pod through environment variables". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Kubernetes's configmap to inject into pod through environment variables".

On the Kubernetes website, there is an article that mentions that one of the best practices in Kubernetes is to separate application code from configuration information, one way is to use the concept of configmap introduced in Kubernetes 1.2.

Https://kubernetes.io/blog/2016/04/configuration-management-with-containers/

Configmap is actually a series of key-value pairs, stored in etcd. There is a sentence on etcd's official website:

Etcd is a distributed key-value store designed to reliably and quickly preserve and provide access to critical data.

Https://github.com/etcd-io/etcd/blob/master/Documentation/docs.md

Etcd is a high-performance distributed key-value pair repository for storing and accessing critical data.

Create a Kubernetes config map using the following command line:

Kubectl create configmap test-config-from-literal=test.type=unit-from-literal=test.exec=always

Create a key-value pair named test-config, with key as test.type, unit,key as test.exec, and always as value.

Next I'm going to create a pod and consume this configmap called test-config.

Create a yaml file with the following contents:

ApiVersion: v1kind: Podmetadata:name: test-configmapspec:containers:- name: test-containerimage: alpine:3.8command: ["/ bin/sh", "- c", "env"] env:- name: TEST_TYPEvalueFrom:configMapKeyRef:name: test-configkey: test.type- name: TEST_EXECvalueFrom:configMapKeyRef:name: test-configkey: test.execrestartPolicy: Never

The pod defined by this yaml file is based on the docker image alpine and executes the shell command / bin/sh-c env to view the environment variables.

In the env area, I inject an environment variable named TEST_TYPE into the pod, which is taken from the value of the key name of the configMap key-value pair called test.type.

Kubectl create-f creates this pod:

Using the command kubectl logs test-configmap to view the log generated by the pod run, you can see that TEST_TYPE=unit appears in the output list of environment variables. This TEST_TYPE is the name of the environment variable I injected into the yaml file, and unit comes from the value unit of test-config in configmap.

Thank you for your reading, the above is the content of "how to use Kubernetes's configmap to inject environment variables into pod". After the study of this article, I believe you have a deeper understanding of how to use Kubernetes's configmap to inject environment variables into pod, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report