使用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

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值