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

Use Rancher-Gen to dynamically update configuration files

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

Docker and Rancher make it easier to deploy and manage microservice-based applications. However, if some services depend on other dynamic services, how do you manage their configuration? Do you often think that it would be nice if there was a way to automatically detect changes in back-end services and dynamically update configuration files? This article gives you the answer.

Preface

Docker and Rancher make it easier to deploy and manage microservice-based applications. However, a key challenge is how to manage the configuration of some services if they are dependent on other dynamic services.

Imagine this scenario: you have multiple back-end containers running Web applications, and some nginx containers that proxy all requests to these back-end containers. Now, you have to deploy a new version of the Web application, which means you need to build and deploy a new version of the back-end container. After these deployments are complete, the configuration of nginx needs to be changed to point to the new back-end container. So, what do you do with nginx? Change its configuration, build a new container and deploy it? At the moment, do you think it would be nice if there was a way to automatically detect changes in back-end services and dynamically update nginx?

This is the moment when Rancher-Gen will make his debut!

Rancher-Gen is a Python tool that listens for changes to Rancher services and renders a user-specified Jinja2 template. This allows users to generate profiles for existing services based on these changes. In addition, Rancher-Gen provides a mechanism to run notification commands after the template is rendered. The following tutorial shows how to automatically generate a nginx configuration file that runs the back-end service of the ghost blog platform.

Tutorials

All the configuration files described below can be found in the demo directory in the Rancher-Gen library.

Step 1-deploy the Ghost service

For simplicity, we intend to use the official ghost image in Docker hub. Therefore, create a docker-compose.yml file and add the following ghost service:

Now, deploy the ghost service with Rancher Compose:

Step 2-create a nginx image with Rancher-Gen

The following is the Dockerfile used to build nginx images:

Let's disassemble Dockerfile step by step. Steps 1 and 2 are self-evident: just install nginx, Python, and Rancher-Gen.

Step 3 is to set up the service that runs when the image is started. The first service is nginx, which runs with the / etc/servce/nginx file. The contents of the file are:

Notice how we passed the paths named / nginxconf/default.j2 and / etc/nginx/sites-available/default after the notification step. The former is the Jinjia2 template, and the latter is the output location of the rendering template. The following is the contents of the default.j2 file:

Steps 4 and 5 of Dockerfile set the run command "/ sbin/my_init" in the mirror and expose ports 80 and 443.

Now is the time to build the image:

Step 3-create and deploy nginx services

Now that we have the nginx image, we can add the nginx service to the docker-compose.yml file we created in step 1.

The above mutable RANCHER_GEN_OPTIONS environment is used to pass additional command line options to Rancher-Gen. You can see the description of these options in the Rancher-Gen documentation.

Now run rancher-compose to start the nginx service:

At this point, both the ghost and nginx services are up and running:

Also, point your browser to the IP address of the host in the running nginx container, and you can access ghost:

If you use shell to check the nginx container and open the rendered file / etc/nginx/sites-enabled/default, you will see the following output:

As expected, this is the rendered output based on the specified template when running the rancher-gen command. At this point, if you want to upgrade the ghost service and look at the rendering file again, you will find that the IP address under the upstream section has changed.

Conclusion

To sum up, Rancher-Gen is an automated tool that can be used to generate files and run notification commands. With the template expressiveness of Jinja2 and its clean command line interface, Rancher-Gen can be used to generate most configuration files and automatically solve tedious and repetitive tasks for most system administrators and software engineers.

Original source: Rancher Labs

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

Network Security

Wechat

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

12
Report