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 manage the Docker stack

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

Share

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

This article will explain in detail how to manage the Docker stack. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

The details are as follows:

Understand the top level of the distributed application hierarchy: the stack. A stack is a set of interrelated services that can share dependencies and can be coordinated and scaled together. A single stack can define and coordinate the functionality of the entire application.

Add a new service and redeploy

Adding services to the docker-compose.yml file is easy, first adding a free visualizer service so we can see how the container is scheduled. Open docker-compose.yml in the editor and replace its contents with the following code:

Version: "3" services: web: # replace username/repo:tag image with your name and image details: username/repo:tag deploy: replicas: 5 restart_policy: condition: on-failure resources: limits: cpus: "0.1" memory: 50m ports:-"80:80" networks:-webnet visualizer: image: dockersamples/visualizer:stable ports:-"8080 memory 8080" volumes:-"/ var/run/docker.sock: / var/run/docker.sock "deploy: placement: constraints: [node.role = = manager] networks:-webnetnetworks: webnet:

The only addition here is a web peer service called visualizer, where you can see two new things: the volumes key, giving visualizer access to Docker's host socket files, and a placement key to ensure that the service can only run on the cluster manager. The container built by the open source project created by Docker can display the Docker services running on the cluster.

Make sure your terminal is configured to communicate with myvm1, run docker-machine ls to list the machine, and make sure you are connected to myvm1, rerun docker-machine env myvm1 if necessary, and then run the given command to configure the terminal. Rerun the docker stack deploy command on the manager, and any services that need to be updated will be updated:

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

You can see in the docker-compose.yml file that visualizer is running on port 8080. Get the IP address of one of the nodes by running docker-machine ls, go to the IP address of port 8080, and you will see that visualizer is running:

A single copy of visualizer runs on the manager as expected, and five instances of the network are distributed throughout the cluster. You can verify this visualization by running docker stack ps:

$docker stack ps getstartedlab

Visualizer is a stand-alone service that can run in any application that contains it, and it doesn't depend on anything else. Now create a dependent service, and the Redis service will provide visitor counters.

Data persistence

Once again, use the same workflow to add a Redis database for storing application data, save the new docker-compose.yml file, and finally add a Redis service:

Version: "3" services: web: # replace username/repo:tag image with your name and image details: username/repo:tag deploy: replicas: 5 restart_policy: condition: on-failure resources: limits: cpus: "0.1" memory: 50m ports:-"80:80" networks:-webnet visualizer: image: dockersamples/visualizer:stable ports:-"8080 memory 8080" volumes:-"/ var/run/docker.sock: / var/run/docker.sock "deploy: placement: constraints: [node.role = = manager] networks:-webnet redis: image: redis ports: -" 6379 volumes: / / home/docker/data:/data deploy: placement: constraints: [node.role = = manager] command: redis-server-- appendonly yes networks:-webnetnetworks: webnet:

Redis has an official image in the Docker library and has been given a short image name for redis, so there is no username/repo here. Redis port 6379 is preconfigured by Redis, exposed from the container to the host, and exposed from the host to the outside in the docker-compose.yml file, so that you can go to the Redis Desktop Manager for any node and manage the Redis instance.

Most importantly, there are several things in the redis specification that keep data persistent between deployments on this stack: redis always runs on the manager, so it always uses the same file system; redis accesses an arbitrary directory in the host's file system as / data inside the container, which is where Redis stores data. In short, this is to create a "real source" in the host physical file system of the Redis data. Without this, Redis will store its data in / data in the container file system, and if the container is redeployed, the data will be cleared.

This real source has two components: placement's placement constraint on the Redis service to ensure that it always uses the same host; the created volumes allows the container to access / data (on the host) as / data (within the Redis container), and the / data file stored on the specified host will persist as the container comes and goes, thus maintaining continuity.

Create a / data directory on the manager:

$docker-machine ssh myvm1 "mkdir. / data"

Make sure the terminal is configured to communicate with myvm1, run docker-machine ls to list the machines, and make sure you are connected to the myvm1, rerun docker-machine env myvm1 if necessary, and then run the given command to configure the terminal. Rerun the docker stack deploy command on the manager, and any services that need to be updated will be updated:

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

Run docker service ls to verify that the three services are working as expected:

$docker service ls

Look at the web page of one of your nodes, and you will see the result of the guest counter, which now exists and stores the information on Redis:

In addition, access the visualizer using the IP address port 8080 of either node, and you will see the redis services running with the web and visualizer services:

This is the end of this article on "how to manage the Docker stack". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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