Abstract: Docker is getting more and more mature and more powerful. It is also very convenient to use Dokcer Stack as a service cluster. Docker itself provides the load function, which is very convenient. I want to share it with you and make a simple tutorial. For the environment, I used two centos7 virtual machines for this tutorial. Their ip is the main server: 192.
Docker is becoming more and more mature and its functions are becoming more and more powerful. It is also very convenient to use Dokcer Stack as a service cluster. Docker itself provides the 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 IPs are the
main server: 192.168.0.105 // also the private warehouse server
Server 2: 192.168.0.49
All the codes in this post github address: https: //github.com/lpxxn/godockerswarm
Set up Docker Swarm
I use 192.168.0.105 as the main server, open the swarm on him and
docker swarm init
will give the command to join the swarm after executing the command 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
So we have built the swarm, and the two hosts are now in a relationship.
web service
web service is a simple interface written in go language and returns the name of the host: this is convenient for us to check whether there is a load
copy code
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))
}
Copy the code
Docker file Take a
look at the dockerfile file:
the execution means that based on the golang image, copy the code to the corresponding folder, expose the port, run program. It's easy.
Copy code
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" ]
Copy code 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 The
image is submitted to the private repository
About How to build a private warehouse server I will say more here, you can go to my previous post to see
Address 1: http://www.cnblogs.com/li-peng/p/6511331.htmlAddress
2: https:// yq.aliyun.com/articles/303216?spm=5176.8091938.0.0.2ce387dadknIQu
can also be built with harbor. I haven't done a tutorial yet, and I will write it when I have time.
Because the machine on the cluster automatically fetches the image from the warehouse and then runs the program, we need to push the image we generated above to our private warehouse. I built it myself
Use tag to rename
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 that we created above mirror. Run 5 applications, docker will do the load by itself, 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 inside, you can read the official tutorial:
https://docs.docker.com/compose/compose-file/#dockerfilecopy
codeversion :
"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:
Copy code
Deploy application
At the final stage, the deployment is as simple as executing the deploy command
docker stack deploy -c docker-compose.yml mygoweb to
view the started service
docker service ps mygoweb
test service
Look at these returned hostnames: different. docker does the load for us.
All code github address in this post: https://github.com/lpxxn/godockerswarm
Copyright Notice: The content of this article is contributed by Internet users, and the copyright belongs to the author. This community does not own the ownership and does not assume relevant legal responsibility. If you find any content suspected of plagiarism in this community, please send an email to: [email protected] to report and provide relevant evidence. Once verified, this community will immediately delete the allegedly infringing content.
Docker Stack deploys web cluster
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326056922&siteId=291194637
Recommended
Ranking