From e16d0ac3defa9295529c18242204a43b879e004c Mon Sep 17 00:00:00 2001 From: zhuyasen Date: Mon, 26 Sep 2022 23:51:21 +0800 Subject: [PATCH] add the build docker image and depoyments --- build/Dockerfile | 37 +++++++++++++++++++ build/Dockerfile_sponge | 23 ++++++++++++ .../docker-compose/sponge/docker-compose.yml | 22 +++++++++++ internal/ecode/grpc_systemCode_test.go | 11 ------ scripts/grpc-health-probe.sh | 9 +++++ 5 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 build/Dockerfile create mode 100644 build/Dockerfile_sponge create mode 100644 deployments/docker-compose/sponge/docker-compose.yml delete mode 100644 internal/ecode/grpc_systemCode_test.go create mode 100644 scripts/grpc-health-probe.sh diff --git a/build/Dockerfile b/build/Dockerfile new file mode 100644 index 0000000..3dea2d6 --- /dev/null +++ b/build/Dockerfile @@ -0,0 +1,37 @@ +# 需要先打包代码`tar zcf sponge.tar.gz $(ls)`,并移动到和Dokerfile在同一个目录下 +# 编译go代码 +FROM golang:1.18-alpine as build +COPY . /go/src/sponge +WORKDIR /go/src/sponge +RUN tar zxf sponge.tar.gz +RUN go env -w GOPROXY=https://goproxy.cn,direct +RUN go mod download +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -gcflags "all=-N -l" -o /sponge cmd/sponge/main.go + +# 构建grpc_health_probe +RUN go install github.com/grpc-ecosystem/grpc-health-probe@v0.4.12 +RUN cd $GOPATH/pkg/mod/github.com/grpc-ecosystem/grpc-health-probe@v0.4.12 \ + && go mod download \ + && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /grpc_health_probe + +# 通过二进制文件构建镜像 +FROM alpine:latest +MAINTAINER zhufuyi "g.zhufuyi@gmail.com" + +# 设置时区为上海 +RUN apk add tzdata \ + && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ + && echo "Asia/Shanghai" > /etc/timezone \ + && apk del tzdata + +# 添加curl,用在http服务的检查 +RUN apk add curl + +# 添加grpc_health_probe,用在grpc服务的健康检查 +COPY --from=build /grpc_health_probe /bin/grpc_health_probe + +COPY --from=build /sponge /app/sponge +COPY --from=build /go/src/sponge/config/conf.yml /app/config/conf.yml + +WORKDIR /app +CMD ["./sponge", "-c", "config/conf.yml"] diff --git a/build/Dockerfile_sponge b/build/Dockerfile_sponge new file mode 100644 index 0000000..5283f4f --- /dev/null +++ b/build/Dockerfile_sponge @@ -0,0 +1,23 @@ +# 需要先在本地编译好二进制文件和配置文件复制到和Dokerfile在同一个目录下 +FROM alpine:latest + +MAINTAINER zhufuyi "g.zhufuyi@gmail.com" + +# 设置时区为上海 +RUN apk add tzdata \ + && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ + && echo "Asia/Shanghai" > /etc/timezone \ + && apk del tzdata + +# 添加curl,用在http服务的检查 +RUN apk add curl + +# 添加grpc_health_probe,用在grpc服务的健康检查 +COPY grpc_health_probe /bin/grpc_health_probe +RUN chmod +x /bin/grpc_health_probe + +COPY config/ /app/config/ +COPY sponge /app/sponge +RUN chmod +x /app/sponge +WORKDIR /app +CMD ["./sponge", "-c", "config/conf.yml"] diff --git a/deployments/docker-compose/sponge/docker-compose.yml b/deployments/docker-compose/sponge/docker-compose.yml new file mode 100644 index 0000000..00fd970 --- /dev/null +++ b/deployments/docker-compose/sponge/docker-compose.yml @@ -0,0 +1,22 @@ +version: "3.7" + +services: + sponge: + + image: sponge/sponge:latest + container_name: sponge-app + restart: always + command: ["./sponge", "-c", "/app/config/conf.yml"] + ports: + - "28080:8080" # http端口 + - "29090:9090" # grpc服务端口 + - "29082:9082" # grpc metrics端口 + volumes: + - $PWD/config:/app/config + healthcheck: + #test: ["CMD", "curl", "-f", "http://localhost:8080/health"] # http健康检查,注:镜像里必须又curl命令 + test: ["CMD", "grpc_health_probe", "-addr=localhost:9090"] # grpc健康检查,注:镜像必须有grpc_health_probe命令 + interval: 30s # 间隔时间 + timeout: 5s # 超时时间 + retries: 3 # 重试次数 + start_period: 10s # 启动多久后开始检查 diff --git a/internal/ecode/grpc_systemCode_test.go b/internal/ecode/grpc_systemCode_test.go deleted file mode 100644 index 8bdd67b..0000000 --- a/internal/ecode/grpc_systemCode_test.go +++ /dev/null @@ -1,11 +0,0 @@ -package ecode - -import ( - "github.com/stretchr/testify/assert" - "testing" -) - -func TestAny(t *testing.T) { - detail := Any("foo", "bar") - assert.Equal(t, "foo: {bar}", detail.String()) -} diff --git a/scripts/grpc-health-probe.sh b/scripts/grpc-health-probe.sh new file mode 100644 index 0000000..7c6f8f3 --- /dev/null +++ b/scripts/grpc-health-probe.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +go env -w GOPROXY=https://goproxy.cn,direct + +go install github.com/grpc-ecosystem/grpc-health-probe@v0.4.12 + +cd $GOPATH/pkg/mod/github.com/grpc-ecosystem/grpc-health-probe@v0.4.12 \ + && go mod download \ + && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /tmp/grpc_health_probe