In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to understand the kubernetes configuration center configmap, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
In an enterprise, there are generally 2-4 environments, development, testing, delivery, and production. The configuration of these environments has also changed, and we usually cannot deploy four environments with one configuration file. It's not universal. Especially the containerized environment.
In containerized applications, each environment has to play an independent image and then give the image a unique tag, which is very troublesome. The native configuration center configMap of K8s is used to solve this problem. Let's look at the example.
Deploy the application using configMap.
Here we use nginx as an example, which is simple and rough.
The default nginx data directory is in: / usr/share/nginx/html directory. I'll write a configuration specified port and data directory below.
1. Load the configuration file into configMap first
Write nginx configuration
[root@server yaml] # vim nginx.conf server {listen 8081; # # Port 8081 server_name _; root / html; # # change the data directory to / html location / {}}
Load the nginx.conf written above into configMap
There are three ways to create a configMap
1) single key
Command format:
Example: kubectl create configmap special-config-- from-literal=special.how=very-- from-literal=special.type=charm
View: kubectl get configmaps special-config-o yaml
2) load all files under the directory into configMap based on the directory
Command format:
Example: kubectl create configmap tomcat-conf-from-file=configmap/conf
View: kubectl get configmaps tomcat-conf-o yaml
3) based on a single file
Command format:
Example: kubectl create configmap game-conf-from-file=configmap/conf/game.properties
View: kubectl get configmaps tomcat-conf-o yaml
Note:
Pod using configmap must be configured in the same namespaces as configmap, otherwise it will not be recognized. Add-n namespaces name to the end when creating the configmap
Example: kubectl create configmap tomcat-conf-- from-file=configmap/conf-n tomcat
The third method is used here: a method based on a single file, and other methods are also simple.
Format:
File loaded by custom name of command action module
Kubectl create configmap nginx-config-from-file=./nginx.conf
[root@server yaml] # kubectl create configmap nginx-config-- from-file=./nginx.conf configmap/nginx-config created # # prompt that [root@server yaml] # [root@server yaml] # kubectl get configmaps # # check whether the created nginx-config configuration is successful NAME DATA AGEnginx-config 15s
Verify that the nginx-config configuration in configMap is the same as the previous nginx.conf in vi
[root@server yaml] # kubectl get configmaps/nginx-config-o yamlapiVersion: v1data: nginx.conf: | + # # this paragraph is the content, and nginx.conf is the key server {listen 8081; server_name _; root / html of the file Location / {}} kind: ConfigMapmetadata: creationTimestamp: "2019-01-31T09:20:08Z" name: nginx-config namespace: default resourceVersion: "416047" selfLink: / api/v1/namespaces/default/configmaps/nginx-config uid: 67199b66-2539-11e9-821c-000c2963b9a7 [root@server yaml] #
You can see the data: the contents of the following nginx.conf are the same as those of the vi above
Then use this configuration when deploying the application
Write the yaml file for deploying the application
[root@server yaml] # vim nginx.yamlapiVersion: extensions/v1beta1kind: Deploymentmetadata: name: my-nginxspec: replicas: 1 template: metadata: labels: app: my-nginxspec: containers:-name: my-nginx image: nginx:1.9 ports:-containerPort: 8081 volumeMounts:-- this section is configured using configMap -mountPath: / etc/nginx/conf.d-- where to mount the configuration file name: config-mountPath: / html-- specify the data directory name: data volumes:-name: data-- specify the data directory creation emptyDir: {} -name: config-- specify config to use configMap configMap: name: nginx-config-- specify to use nginx-config in configMap to configure items:-- Note: items may not be specified By default, all values in nginx-config are mounted-key: nginx.conf-- uses the contents of the nginx.confi key configured by nginx-config: path: nginx.conf---apiVersion: v1kind: Servicemetadata: name: nginx-svcspec: type: NodePort selector: app: my-nginx ports:-protocol: TCP port: 8081 targetPort: 8081 nodePort: 28081
Deploy the application
[root@server yaml] # kubectl create-f nginx.yaml deployment.extensions/my-nginx createdservice/nginx-svc created [root@server yaml] # [root@server yaml] # [root@server yaml] # kubectl get pod NAME READY STATUS RESTARTS AGEmy-nginx-55fd7587b7-7fscq 1 Running 09s [root@server yaml] # [root@server yaml] # kubectl get svc | grep nginxNAME TYPE CLUSTER-IP EXTERNAL-IP PORT (S) AGEnginx-svc NodePort 10.68.230.130 8081:28081/TCP 15s
Deployment is successful, pod has been run successfully, and service discovery has completed 28081 external access portals.
2. Verify whether the nginx application uses nginx-config in configMap.
Log in to the deployed application pod to check it out.
[root@server yaml] # kubectl exec-it my-nginx-55fd7587b7-7fscq bash-- login container root@my-nginx-55fd7587b7-7fscq:/# root@my-nginx-55fd7587b7-7fscq:/# cat / etc/nginx/conf.d/nginx.conf-- check that the configuration is indeed the content of the above vi server {listen 8081; server_name _; root / html Location / {}} root@my-nginx-55fd7587b7-7fscq:/#root@my-nginx-55fd7587b7-7fscq:/# ls-d / html/-- the data directory has also been created / html/root@my-nginx-55fd7587b7-7fscq:/#root@my-nginx-55fd7587b7-7fscq:/# ls-l / etc/nginx/conf.d/total 0lrwxrwxrwx 1 root root 17 Jan 31 09:31 nginx.conf->.. data/nginx .conf-you can see that this configuration file is a relative path, not an entity file
It has been verified that the application does use the configuration of configMap
In this way, we can create a copy of different environment configuration files in k8s and specify the configuration file of the corresponding environment at the time of deployment.
This solves the problem of repackaging images in different environments when deploying applications.
The above is how to understand the configuration center configmap of kubernetes. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.