您好,登录后才能下订单哦!
如何用docker swarm 实现集群,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
(1) go 编写 web 程序
主要实现 监听 ip:9090/Source 浏览,并记录 服务器主机与访问次数。
代码
package main import ( "fmt" "log" "net/http" "runtime" "strconv" "os" ) var i int = 0 func sayHello(w http.ResponseWriter, r *http.Request){ //func sayHello(){ i++ fmt.Print(i) fmt.Print("\n") fmt.Print("visit!"+"\n") hostname,er :=os.Hostname() if er == nil { dates := "jenkins:"+"Login \n"+"visit Count:"+strconv.Itoa(i)+"\nHostname:"+hostname w.Write([]byte(dates)) defer r.Body.Close() }else { return } } func main(){ fmt.Print("<<<<< "+runtime.GOOS+"\n") fmt.Print("<<<<< "+runtime.GOARCH+"\n") http.HandleFunc("/Source",sayHello) fmt.Print("<<< debug start"+"\n") er := http.ListenAndServe(":9090",nil) fmt.Print("<<< debug end") fmt.Print(er) if er != nil { log.Fatal("ListenAndServe",er) } }
(2) 生成arch 程序
go build arch.go
(3) arch + Dockerfile 生成微服务镜像
Dockerfile
FROM ubuntu:16.04 MAINTAINER ZHB COPY ./arch / RUN chmod 777 /arch RUN touch /log.txt EXPOSE 9090 ENTRYPOINT ["sh","-c","./arch >> /log.txt"] #可在 容器里 tail -f log.txt 监控
docker build -t ip:5000/swarm:0.0 ./
ubuntu@local:~/SWARM$ docker build -t 192.168.175.139:5000/swarm:0.0 ./ Sending build context to Docker daemon 6.629MB #Sending build context 可以用.dockerignore 阻止 Step 1/7 : FROM ubuntu:16.04 ---> a51debf7e1eb Step 2/7 : MAINTAINER ZHB ---> Using cache ---> 26b542a8286b Step 3/7 : COPY ./arch / ---> Using cache ---> aff0b358480c Step 4/7 : RUN chmod 777 /arch ---> Using cache ---> 6e07a223b571 Step 5/7 : RUN touch /log.txt ---> Using cache ---> 03be86c93b8a Step 6/7 : EXPOSE 9090 ---> Using cache ---> fe9c6a7ab542 Step 7/7 : ENTRYPOINT ["sh","-c","./arch >> /log.txt"] ---> Using cache ---> 6287178e0005 Successfully built 6287178e0005 Successfully tagged 192.168.175.139:5000/swarm:0.0
push 镜像
docker push ip:5000/swarm:0.0
(4)主节点 swarm init
docker swarm init
其他服务器加入集群
docker swarm join --token **** ip:2377
(5)创建service
docker service create --replicas 6 --name=swarm-service --publish 9090:9090 IP:5000/swarm:0.0
overall progress: 0 out of 6 tasks 1/6: preparing [=================================> ] 2/6: preparing [=================================> ] 3/6: preparing [=================================> ] 4/6: preparing [=================================> ] 5/6: preparing [=================================> ] 6/6: preparing [=================================> ]
(6) web 访问 ip:9090/Source 并实现负载均衡
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。