java swarm集群_用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 并实现负载均衡

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值