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 set up Docker Swarm

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

Share

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

This article mainly explains "how to set up Docker Swarm". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to set up Docker Swarm.

Environment

I used two centos7 virtual machines to do this tutorial, and their ip is

Master server: 192.168.0.105 / / is also a private warehouse server server 2: 192.168.0.49

Demo code in this post github address: demo code

Set up Docker Swarm

I use 192.168.0.105 as the main server and open swarm on it.

Docker swarm init

After executing the command, the command to join the swarm will be given.

Execute the command to join swarm on 192.168.0.49

Docker swarm join-- token SWMTKN-1-425vswwmb8o34uhnmo58w0k4rfzs5okjtye7mokpqps1vl9ymq-0p6pr2gua7l8a6udb67tfndoo 192.168.0.105pur2377

In this way, we have built the swarm, and the two hosts have now established a relationship.

Web service

The web service writes a simple interface in go language and returns the name of the host: this makes it easy for us to see if there is a load.

Package mainimport ("fmt"log"net/http"os") func main () {http.HandleFunc ("/ hi", func (w http.ResponseWriter, r * http.Request) {hostName, _: = os.Hostname () fmt.Fprintf (w, "HostName:% s", hostName)}) log.Fatal (http.ListenAndServe (": 8000", nil))} Docker file

Take a look at the dockerfile file:

Execution means to copy the code to the appropriate folder, expose the port, and run the program based on the golang situation. It's easy.

FROM golang# Copy the current directory contents into the containerCOPY. / go/src/github.com/lpxxn/godockerswarm/WORKDIR / go/src/github.com/lpxxn/godockerswarm/RUN go buildEXPOSE 8000CMD [". / godockerswarm"]

Take a look at the folder where the dockerfile file is located

You can run the newly generated image.

Docker run-p 8100 docker run 8000 7a7e3

Submit the image to the private warehouse

I have more to say here about how to build a private warehouse server. You can take a look at my previous post.

Address 1: http://www.cnblogs.com/li-peng/p/6511331.html

Address 2: https://yq.aliyun.com/articles/303216?spm=5176.8091938.0.0.2ce387dadknIQu

You can also use harbor to build it yourself. I haven't done a tutorial yet. I'll write it again when I have time.

Because the machine on the cluster automatically takes the image from the repository and then runs the program, we need to push the image generated above to our private repository. I built it myself.

Rename using tag

Docker tag goweb:1.0 lpxxn.com:5000/goweb:1.0

Docker-compose file

Next, create the docker-compose.yml file

Image is the image we created above. Running five applications, docker will do its own load, port mapping 8111, automatically restart the service if it fails, and create its own network, which is very useful when there are multiple server services.

For the specific parameters, you can see the official tutorial: https://docs.docker.com/compose/compose-file/#dockerfile

Version: "3" services: web: image: lpxxn.com:5000/goweb:1.0 deploy: replicas: 5 resources: limits: cpus: "0.1" memory: 50m restart_policy: condition: on-failure ports:-"8111 gowebnetnetworks 8000" networks:-gowebnetnetworks: gowebnet:

Deploy the application

When it comes to the final stage, the subordinate is as simple as it is. Execute the deploy command, create a mirror on the two IP, and then run the following instructions on the manager

Docker stack deploy-c docker-compose.yml mygowebdocker service ls view servicedocker service rm service_id delete servicedocker service ps myflask_web view servicedocker node ls view node

View started services

Docker service ps mygoweb_web

Test service

Look at these returned hostnames: different. Docker did the load for us.

=

1. Create dockerfile

FROM python:3.6-alpineCOPY. / codeWORKDIR / codeRUN pip install redis flask

App.py

From flask import Flaskfrom redis import Redisapp = Flask (_ _ name__) I = 1@app.route ('/') def hello (): global i count = I + = 1 return 'Hello World! this page has been accessed {} times.\ n'.format (count) if _ _ name__ = "_ _ main__": app.run (host= "0.0.0.0", debug=True,port=5000)

Establish a good image

Docker build-t app:v1.

Create a cluster

Manager IP: 192.168.0.105 main server manager: docker swarm init-- advertise-addr 192.168.0.105worker: docker swarm join-- token SWMTKN-1-425vswwmb8o34uhnmo58w0k4rfzs5okjtye7mokpqps1vl9ymq-0p6pr2gua7l8a6udb67tfndoo 192.168.0.105 docker swarm join 2377 so that we have built the swarm, and the two hosts have now established a relationship

Docker-compose.yml

Version: '3'services: web: image: app:v1 ports:-"5555 app:v1 ports 5000" deploy: replicas: 2 command: python / code/app.pydocker stack deploy-c docker-compose.yml myflask if you connect two machines with swarm, then run this statement on manager, and will automatically create distributed deployment and execution. At this point, I believe you have a deeper understanding of "how to set up Docker Swarm", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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