Docker is becoming more and more mature and its functions are becoming more and more powerful. It is also very convenient to use Docker Stack to build a service cluster. Docker itself provides load function, which is very convenient. I want to share it with you and make a simple tutorial. environment I used two centos7 virtual machines to do this tutorial. Their IP addresses are Main server: 192.168.0.105 // also a private warehouse server Server 2: 192.168.0.49 All the code in this post can be found on GitHub: https://github.com/lpxxn/godockerswarm Setting up Docker Swarm I use 192.168.0.105 as the main server and start swarm on it docker swarm init After executing the command, the command to join the swarm will be given Execute the command on 192.168.0.49 to join the swarm docker swarm join --token SWMTKN-1-425vswwmb8o34uhnmo58w0k4rfzs5okjtye7mokpqps1vl9ymq-0p6pr2gua7l8a6udb67tfndoo 192.168.0.105:2377 In this way, we have built the swarm, and the two hosts now have a relationship. Web Services The web service is a simple interface written in go language, which returns the name of the host: This makes it easy for us to check whether there is load package main import ( "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: Execution means copying the code to the corresponding folder, exposing the port, and running the program based on the golang context. Simple, right? FROM golang # Copy the current directory contents into the container COPY ./go/src/github.com/lpxxn/godockerswarm/ WORKDIR /go/src/github.com/lpxxn/godockerswarm/ RUN go build EXPOSE 8000 CMD ["./godockerswarm"] Take a look at the folder where the dockerfile file is located Execute the docker build command in this directory: docker build . -t goweb:1.0 You can run the newly generated image docker run -p 8100:8000 7a7e3 Submit the image to a private repository I will say more about how to build a private warehouse server here. You can go to my previous post to check it out. Address: https://www.jb51.net/article/156168.htm Because the machines in the cluster automatically retrieve images from the warehouse and then run the program, we need to push the images we generated above to our private warehouse. I built it myself Rename using tag docker tag goweb:1.0 lpxxn.com:5000/goweb:1.0 Push docker push lpxxn.com:5000/goweb:1.0 docker-compose file Next, create the docker-compose.yml file Image is the image we created above. Run 5 applications, Docker will do its own load, port mapping 8111, automatically restart the service when it fails, and create its own network, which is very useful when there are multiple server services. For the specific parameters, you can refer to the official tutorial: https://docs.docker.com/compose/compose-file/ 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:8000" networks: - gowebnet networks: gowebnet: Deploy the application Now it’s the final stage. Deployment is just as simple. Execute the deploy command. docker stack deploy -c docker-compose.yml mygoweb View started services docker service ps mygoweb Testing Services Look at these returned host names: they are different. Docker does the load for us. All the code in this post can be found on GitHub: https://github.com/lpxxn/godockerswarm The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: How to use the concat function in mysql
>>: Solve the problem of MYSQL connection port being occupied and introducing file path errors
1. Source of the problem A friend @水米田 asked me a...
Add monitoring host Host 192.168.179.104 is added...
In this article, I will explain the relevant cont...
Table of contents 1. Anti-shake 2. Throttling 3. ...
<br />When inserting music into a web page, ...
A simple example of how to use the three methods ...
The system environment is server2012 1. Download ...
If you cannot download it by clicking downloadlao...
Since I have parsed HTML before, I want to use Vu...
Preface The basic principle of MySQL master-slave...
background Temporary tablespaces are used to mana...
The file name of the dynamic library file under L...
It is mainly a CSS style control and a META tag; C...
Error occurs: When exporting the database from My...
1. Media query method /*iPhone X adaptation*/ @me...