Docker Stack deployment method steps for web cluster

Docker Stack deployment method steps for web cluster

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:
  • Detailed explanation of the construction and use of Docker private warehouse
  • Example of how to quickly build a LEMP environment with Docker
  • Detailed explanation of log processing of Docker containers
  • Weird and interesting Docker commands you may not know
  • Docker creates MySQL explanation
  • Solution to the error when installing Docker on CentOS version
  • Using Docker to create static website applications (multiple ways)
  • How to deploy microservices using Spring Boot and Docker
  • How to install Docker on Raspberry Pi
  • Docker containers communicate directly through routing to achieve network communication

<<:  How to use the concat function in mysql

>>:  Solve the problem of MYSQL connection port being occupied and introducing file path errors

Recommend

Detailed explanation of VUE Token's invalidation process

Table of contents Target Thought Analysis Code la...

Analysis of the cause of docker error Exited (1) 4 minutes ago

Docker error 1. Check the cause docker logs nexus...

Mysql online recovery of undo table space actual combat record

1 Mysql5.6 1.1 Related parameters MySQL 5.6 adds ...

MySQL 5.6.27 Installation Tutorial under Linux

This article shares the installation tutorial of ...

JavaScript Basics: Immediate Execution Function

Table of contents Immediately execute function fo...

A brief discussion on several ways to implement front-end JS sandbox

Table of contents Preface iframe implements sandb...

CSS3 border effects

What is CSS# CSS (abbreviation of Cascading Style...

CentOS 7.6 installation of MySQL 5.7 GA version tutorial diagram

Table of contents Environment Preparation Environ...

How to use Docker plugin to remotely deploy projects to cloud servers in IDEA

1. Open port 2375 Edit docker.service vim /lib/sy...

How to add vim implementation code examples in power shell

1. Go to Vim's official website to download t...

The grid is your layout plan for the page

<br /> English original: http://desktoppub.a...

How to ensure the overall user experience

Related Articles: Website Design for User Experien...

A brief analysis of the four import methods and priorities in CSS

First: 4 ways to introduce CSS There are four way...