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 realize distributed Application function with Docker

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

How to use Docker to realize distributed application function? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Create a docker-compose.yml file

Create a docker-compose.yml file and place it in the same directory as Dockerfile. The docker-compose.yml file is a YAML file that defines how the Docker container behaves in production.

Version: "3" services: web: # replace the username/repo:tag command image: username/repo:tag deploy: replicas: 5 resources: limits: cpus: "0.1" memory: 50m restart_policy: condition: on-failure ports:-"80:80" networks:-webnetnetworks: webnet with your user name and image details:

This docker-compose.yml file tells Docker to do the following:

-remove the image we uploaded in the "introduction to Docker installation and Application tutorial" from the registry.

-run 5 instances of the image as a service called web, limiting each server to use a maximum of 10% of CPU (across all cores) and RAM of 50MB.

-if one fails, restart the container immediately.

-Map port 80 on the host to port 80 on Web.

-instructs the Web container to share port 80 over a load-balanced network called webnet (internally, the container itself will be published to port 80 of Web on a temporary port).

-define the webnet network using the default setting, which is a load-balanced overlay network.

Run a new load balancing application

Run the following command before using the docker stack deploy command.

$docker swarm init

Open a command line terminal, make sure it is still at the top of the new directory, and now to run it, you must give the application a name, where it is set to getstartedlab.

$docker stack deploy-c docker-compose.yml getstartedlab

A single service stack runs five container instances of deployment images on a single host to get the service ID of a service in the application.

$docker service ls

You will see the output of the Web service prefixed with your application name. If you name it the same as shown in this example, the name will be getstartedlab_web. It also lists the service ID as well as the number of copies, mirror name, and exposed port.

A single container running in a service is called a task. The task will be assigned a unique number of increments of ID that can run up to the number of replicas defined in docker-compose.yml. Use the following command to list the tasks in the service.

$docker service ps getstartedlab_web

If only all containers on the system are listed, tasks will also be displayed, but will not be filtered by the service.

$docker container ls-Q

You can run curl-4 http://localhost several times in a row, or go to the URL in a browser and click Refresh several times.

Either way, you can see changes to the container ID, demonstrating load balancing. In each request, select one of the five tasks to respond in a loop, and the container ID will match the output of your previous docker container ls-Q command.

Scale the application

You can extend the application by changing the replicas value in docker-compose.yml, saving the changes, and rerunning the docker stack deploy command.

$docker stack deploy-c docker-compose.yml getstartedlab

Docker does an update operation without first deleting the stack or killing any containers. Now, rerun docker container ls-Q to see the reconfigured deployed instance. Obviously because of the expansion of the replicas value, that is, replicas, there are more tasks and containers.

Next, end the application and cluster, use docker stack rm to end the application, and use docker swarm leave to end the cluster.

Docker stack rm getstartedlab$ docker swarm leave-force after reading the above, have you mastered how to use Docker to implement distributed application functions? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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