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 install and enable Kubernetes ConfigMap

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

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to install and enable Kubernetes ConfigMap". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Let's take a look at the simple web page app that monitors profile changes.

The interesting part of this app is ConfigManager and WatchFile.

ConfigManager's job is to provide a path structure to access our Config {} so that when Kubernetes ConfigMap gives us a new version of the profile or when we update the Config {} object, contention conflicts do not exist.

WatchFile's job is to look at our configuration file for the changes made and run the callback function that reads the new version of the configuration file, setting the new Config {} using ConfigManager.

Let's take a look at ConfigManager installation and enablement.

Here is an example of how we use a simple Mutex to avoid competitive conflicts. Usually you want to avoid using Mutex, and then use golang built into channel. But since the administrator's job is to protect instances of the configuration object, it is acceptable to use Mutex.

Out of curiosity, I created a golang channel installation of this object to enable, and then ran some benchmark points. You can click (https://github.com/thrawn01/configmap-microservice-demo/blob/master/manager.go) to find the code and benchmark test.

The Mutex version has no risk of deadlock and is very efficient.

The implementation of FileWatcher will be a little more complicated. Its goal is to make any additional fsnotify events a separate updated event so that we only have to execute the callback function once. To see the complete code, please click here (https://github.com/thrawn01/configmap-microservice-demo/blob/master/watcher.go)

The interesting part is that the run () function executes in the thread and then runs the callback function.

You might think the code should look for fsnotify.Writeevents instead of fsnotify.Remove, however. The configuration file that ConfigMap presents to the application is actually a symbolic link to our configuration file, not a file. When ConfigMap is updated, Kubernetes AtomicWriter () can implement powerful ConfigMap updates.

To do this, AtomicWriter () creates a new directory; writes the updated ConfigMap content to the new directory. Once written, it moves the symbolic link to the original profile and replaces it with a new symbolic link to the newly created directory.

In theory, our code should be handled by monitoring symbolic links to our configuration files, rather than for real events files. However, fsnotify.v1 does not allow us to submit the IN_DONT_FOLLOW logo to inotify,inotify to allow us to monitor symbolic links for modification. But fsnotify dereferenced the symbolic link and then monitored the real file for events. This is unlikely to change because fsnotify is designed for cross-platform and not all platforms support symbolic links.

I continue to use the fsnotify library because it is convenient for me to develop on osx and deploy on containers. Linux-centric implementations should use the golang.org/x/exp/inotify database directly.

Now that we have our code, we can create a Docker image and update it to Docker hub to prepare for deployment in our Kubernetes cluster.

Suppose you have set up a Kubernetes cluster; let's create a ConfigMap configuration and use it with our container.

Create ConfigMap

First, we create a key manifest file

This defines a new ConfigMap called configmap-microservice-demo, which includes data: the configuration file is called configmap-microservice-demo.yaml

Its content is message: Hello World.

Use kubectl to create a ConfigMap.

You can detect the newly created ConfigMap

Next let's define a Replication Controller key manifest to run our application container.

What's interesting is that volumes: and volumeMounts:, both tell kubelet running on the node where to install our configuration file. When our container is running; the data volume plug-in installs a directory called / etc/config in our container and replaces our configuration file configmap-microservice-demo.yaml there.

From our container point of view, the complete path to our configuration file will be: / etc/config/configmap-microservice-demo.yaml

Now let's create Replication Controller.

We can now check the pods we are running to find the IP address of our new pod.

Now if you log in to a node in our cluster, we can access the application from anywhere in the cluster using the IP address of pod.

If this section puzzles you, you can click on this http://www.dasblinkenlichten.com/kubernetes-101-networking/, which has deep guidance on how the Kubernetes network works. This is the official document (http://kubernetes.io/docs/admin/networking/)

Update ConfigMap

Now for the interesting part, let's update our configuration and deploy changes to ConfigMap.

Let's open the original ConfigMap key manifest file and modify our message:Hello World to message:Hello Grandma.

Replace the current ConfigMap with our updated version

We can verify that the update is successful by executing get on the configmap resource.

Our application quickly got the updated configuration, which we can verify by looking at the log.

Now that we can curl our application in the cluster, we should see the updated configuration reflected in our application.

You can log in to the node where our container is running, and then check the configuration file directly. Kubernetes installs the directory in / var/lib/kubelet/pods//volumes/kubernetes.io~configmap/config-volume.

This is the end of "how to install and enable Kubernetes ConfigMap". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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