如何用docker swarm 實現(xiàn)集群,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)公司是一家專注于成都網(wǎng)站建設(shè)、網(wǎng)站制作與策劃設(shè)計,黔江網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:黔江等地區(qū)。黔江做網(wǎng)站價格咨詢:028-86922220
(1) go 編寫 web 程序
主要實現(xiàn) 監(jiān)聽 ip:9090/Source 瀏覽,并記錄 服務(wù)器主機與訪問次數(shù)。
代碼
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 生成微服務(wù)鏡像
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 監(jiān)控
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)主節(jié)點 swarm init
docker swarm init
其他服務(wù)器加入集群
docker swarm join --token **** ip:2377
(5)創(chuàng)建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 并實現(xiàn)負(fù)載均衡
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。