使用docker stack方式部署web集群
如何部署swarm集群,请参考:
https://blog.csdn.net/IndexMan/article/details/102713777
创建文件夹
mkdir -p /opt/dockerfile/godockerswarm/
cd /opt/dockerfile/godockerswarm/
编写main.go
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))
}
下拉golang镜像
官方的太慢,我直接找了个阿里公共镜像
docker pull registry.cn-hangzhou.aliyuncs.com/artifact/golang:1.12-alpine
docker tag registry.cn-hangzhou.aliyuncs.com/artifact/golang:1.12-alpine golang:1.12-alpine
编写dockerfile
FROM golang:1.12-alpine
# 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"]
构建镜像
docker build . -t goweb:1.0
测试运行
docker run -p 8100:8000 1b54d
curl 127.0.0.1:8100/hi; echo
将镜像推送到私有仓库
我这里推送到自己的阿里镜像仓库。
docker tag goweb:1.0 registry.cn-hangzhou.aliyuncs.com/laoxu/goweb:1.0
docker push registry.cn-hangzhou.aliyuncs.com/laoxu/goweb:1.0
编写docker-compose.yml
version: "3"
services:
web:
image: registry.cn-hangzhou.aliyuncs.com/laoxu/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:
启动测试
docker stack deploy -c docker-compose.yml mygoweb