In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to achieve dynamic management and monitoring of docker containers based on spring-boot and docker-java, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Introduction to docker
Docker is an open source application container engine. Compared with traditional virtual machine technology, Docker container has very low performance overhead, so it is also widely loved by developers. With more and more developers based on docker, more and more docker images are available. In the future, all kinds of enterprise-level complete solutions can be used directly by downloading images. So docker is becoming more and more important.
The purpose of this article
This paper introduces how to manage and monitor the docker container through docker external interface through a project example.
Application scenarios:
The server resource pool is managed uniformly through docker, and resources are allocated and containers are created as needed to maximize the utilization of resources. At the same time, it ensures the isolation of each business (container). And can support the online deployment of the project.
This project simulates the above scenarios, the code is not completely prohibited, the code is only for learning.
Bar this article
1. Create a tomcat container for docker through the interface, and configure the quota.
2. Upload the war package and deploy to the container.
3. Dynamically monitor the resource usage of all docker containers.
The code is based on docker-java open source components and completes functional development based on spring-boot. The interface is shown below:
Basic environment
1. Eclipse 2019-03
2 、 docker for windows . For learning and simplicity purposes, the linux environment is not used, but the principles and basic commands are the same.
Related technologies and knowledge points:
Maven,spring-boot;spring mvc;spring upload and download, jquery ajax and upload.
Realization principle
Docker provides rich interfaces (such as restfull APi) through which docker can be managed. Docker-java open source components are encapsulated based on this interface, making development easier. Of course, there are other packaged components on the market, which you can compare and learn by yourself. I would like to thank the open source author here.
Preparation for the realization process
1. Upgrade windows
Because we are using the windows environment, the author's own computer is pre-installed with win10 Home Edition, and docker for windows needs to run on a higher-level operating system in order to use virtualization technology, so it needs to be upgraded to a professional version.
If you need to upgrade, the upgrade process is relatively simple, briefly introduced as follows (already a professional version can be skipped):
Click to change the product key, enter the professional version of the key, and wait for the upgrade slowly. The key is found on the Internet, anyway, there is.
2. Install docker for windows
After downloading from the official website, the next step along the way will be fine. After the installation, it will start by itself, and the docker logo will appear on the toolbar after startup.
After the installation is successful, you can test the success by typing some commands. Such as: view the basic information of docker.
3. Modify the docker image address.
Since the default image address is slow, a tomcat image is more than 500m long. If the network is not easy to download, it will be very slow. You can configure the image address as NetEase's image address, as follows:
4. Start remote management
Check the last item, where the tcp//xxx is the remote connection address.
After startup, click the address in the browser to see if you can return the result:
Http://localhost:2375/info (this address is also entered as docker indo in cmd) returns the content in json format:
Code development for implementing the process
1. Create maven project.
Because the process is simple, it is omitted. The code structure of the entire project is as follows:
2. Configure the maven dependency of docker-java and spring boot. The final pom file is as follows
By looking at the jar package, the underlying docker-java should be using netty dependencies.
3. Add spring configuration file application.properties
Configure the location of jsp and the management address of docker.
4. Introduction of main categories:
1.spring boot starts the main class.
Of course, you can also configure other containers or start it with maven's jetty plug-in.
You can refer to another article: the principle of Spring-boot (with an example of spring-boot-starter implementation) with source code download
Note where this class is placed, preferably in the root directory, so that all subdirectories under the root directory can be scanned by spring.
2.DockerClientOperaterServer (classes that interact with docker), as follows:
Include the following methods:
InitClient: initializes the link.
GetDockerInfo: get basic information about docker
Stat: real-time statistics
RefreshContainers: get the basic information of all containers and put them in the cache
CreateAndStartrContainerAnddeployApp: create and start the container according to the parameters passed by the interface, and deploy the project at the same time
StopContainer: stop the container
RmContainer: deleting containers
CloseClient: closing links
PushAppToCotainer: deploy war packages to tomcat
Because the code snippet is too long, here are a few main methods:
CreateContainer (create container):
This method uses docker-java to create containers, assign quotas, configure port mappings, and so on.
PushAppToCotainer: deploy the project to tomcat
Stat, monitoring method:
Monitoring this area does not use the docker-java method, in fact, it is supported, but because the return result requires a variety of processing, it uses an opportunistic method. Use java to call cmd to docker stats to get the monitoring results.
The code for docker-java to obtain monitoring information is as follows:
3.DockerClientController (controller that interacts with the interface).
The main methods are as follows:
AddContainer: add containers and upload war packages
This is the explanation of the main code.
For details of the code, please download the source code and follow the procedure after starting it.
The main interface after implementation:
Because it is a sample project, the code does not use any vue and bootstrap frameworks. I wrote some css and js myself. The specific effects are as follows:
1. Monitoring and management home page
2. Add containers
These are all the contents of the article "how to dynamically manage and monitor docker containers based on spring-boot and docker-java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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.