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 by Docker

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

Share

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

In this article, the editor introduces in detail "how to achieve distributed application functions in Docker". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to achieve distributed application functions in Docker" can help you solve your doubts.

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 has read this article "how to implement distributed application functions in Docker". If you want to master the knowledge points of this article, you still need to practice and use it. If you want to know more about related articles, 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.

Share To

Internet Technology

Wechat

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

12
Report