From 903c33447e42b1db00bb8f787a5bac4e70f1adf1 Mon Sep 17 00:00:00 2001 From: zhuyasen Date: Tue, 27 Sep 2022 22:30:59 +0800 Subject: [PATCH] add deployments scripts --- .golangci.yml | 1 + Makefile | 57 ++++++----- README.md | 2 +- build/Dockerfile | 5 +- build/Dockerfile_sponge | 2 +- .../docker-compose/sponge/docker-compose.yml | 10 +- deployments/kubernetes/namespace.yaml | 4 + deployments/kubernetes/sponge-configmap.yml | 96 +++++++++++++++++++ deployments/kubernetes/sponge-deployment.yml | 70 ++++++++++++++ deployments/kubernetes/sponge-svc.yml | 19 ++++ internal/model/init_test.go | 3 +- 11 files changed, 237 insertions(+), 32 deletions(-) create mode 100644 deployments/kubernetes/namespace.yaml create mode 100644 deployments/kubernetes/sponge-configmap.yml create mode 100644 deployments/kubernetes/sponge-deployment.yml create mode 100644 deployments/kubernetes/sponge-svc.yml diff --git a/.golangci.yml b/.golangci.yml index 5c42bdb..c764773 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -14,6 +14,7 @@ run: skip-dirs: - pkg - docs + - api # which files to skip: they will be analyzed, but issues from them # won't be reported. Default value is empty list, but there is # no need to include all autogenerated files, we confidently recognize diff --git a/Makefile b/Makefile index 1c9804b..710d6e7 100644 --- a/Makefile +++ b/Makefile @@ -4,10 +4,11 @@ PROJECT_NAME := "github.com/zhufuyi/sponge" PKG := "$(PROJECT_NAME)" PKG_LIST := $(shell go list ${PKG}/... | grep -v /vendor/) GO_FILES := $(shell find . -name '*.go' | grep -v /vendor/ | grep -v _test.go) +PROJECT_FILES := $(shell ls) .PHONY: init -# init env +# installation of dependent tools init: go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28.0 go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2.0 @@ -24,81 +25,93 @@ init: .PHONY: ci-lint -# make ci-lint +# check the code specification against the rules in the .golangci.yml file ci-lint: golangci-lint run ./... .PHONY: build -# make build, Build the binary file +# go build the linux amd64 binary file build: - @cd cmd/sponge && go build + @cd cmd/sponge && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -gcflags "all=-N -l" @echo "build finished, binary file in path 'cmd/sponge'" .PHONY: run -# make run, run app +# run server run: @bash scripts/run.sh .PHONY: dep -# make dep Get the dependencies +# download dependencies to the directory vendor dep: @go mod download .PHONY: fmt -# make fmt +# go format *.go files fmt: @gofmt -s -w . .PHONY: test -# make test +# go test *_test.go files test: go test -short ${PKG_LIST} .PHONY: cover -# make cover +# generate test coverage cover: - go test -short -coverprofile coverage.out -covermode=atomic ${PKG_LIST} - go tool cover -html=coverage.out + go test -short -coverprofile cover.out -covermode=atomic ${PKG_LIST} + go tool cover -html=cover.out .PHONY: docker -# generate docker image +# build docker image docker: - docker build -t sponge:latest -f build/Dockerfile + @tar zcf sponge.tar.gz ${PROJECT_FILES} + @mv -f sponge.tar.gz build/ + docker build -t sponge/sponge:latest build/ + @rm -rf build/sponge.tar.gz + + +.PHONY: docker-image +# copy the binary file to build the docker image, skip the compile to binary in docker +docker-image: build + @bash scripts/grpc_health_probe.sh + @mv -f cmd/sponge/sponge build/ && cp -f /tmp/grpc_health_probe build/ + @mkdir -p build/config && cp -f config/conf.yml build/config/ + docker build -f build/Dockerfile_sponge -t sponge/sponge:latest build/ + @rm -rf build/sponge build/config/ build/grpc_health_probe .PHONY: clean -# make clean +# clean binary file, cover.out, redundant dependency packages clean: - @-rm -vrf sponge - @-rm -vrf cover.out - @-rm -vrf coverage.txt + @rm -vrf cmd/sponge/sponge + @rm -vrf cover.out @go mod tidy @echo "clean finished" .PHONY: docs -# gen swagger doc +# generate swagger doc docs: @swag init -g cmd/sponge/main.go @echo "see docs by: http://localhost:8080/swagger/index.html" .PHONY: graph -# make graph 生成交互式的可视化Go程序调用图,生成完毕后会在浏览器自动打开 +# generate interactive visual function dependency graphs graph: @echo "generating graph ......" @go-callvis github.com/zhufuyi/sponge .PHONY: mockgen -# make mockgen gen mock file +# mockgen gen mock file mockgen: cd ./internal && for file in `egrep -rnl "type.*?interface" ./repository | grep -v "_test" `; do \ echo $$file ; \ @@ -107,13 +120,13 @@ mockgen: .PHONY: proto -# generate proto struct only +# generate *.pb.go codes from *.proto files proto: @bash scripts/protoc.sh .PHONY: proto-doc -# generate proto doc +# generate doc from *.proto files proto-doc: @bash scripts/proto-doc.sh diff --git a/README.md b/README.md index abe436c..8b96f56 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ sponge 是一个微服务框架,支持http和grpc及服务治理,结合[goct │ ├── handler # http的业务功能实现 │ ├── model # 数据库 model │ ├── routers # http 路由 -│ ├── server # 服务入口,包括http和grpc服务入口 +│ ├── server # 服务入口,包括http和grpc服务 │ └── service # grpc的业务功能实现 ├── pkg # 外部应用程序可以使用的库代码 ├── scripts # 存放用于执行各种构建,安装,分析等操作的脚本 diff --git a/build/Dockerfile b/build/Dockerfile index 3dea2d6..7d64c43 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -14,7 +14,7 @@ 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" @@ -24,7 +24,7 @@ RUN apk add tzdata \ && echo "Asia/Shanghai" > /etc/timezone \ && apk del tzdata -# 添加curl,用在http服务的检查 +# 添加curl,用在http服务的检查,如果用部署在k8s,可以不用安装 RUN apk add curl # 添加grpc_health_probe,用在grpc服务的健康检查 @@ -35,3 +35,4 @@ 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 index 5283f4f..3235319 100644 --- a/build/Dockerfile_sponge +++ b/build/Dockerfile_sponge @@ -1,6 +1,5 @@ # 需要先在本地编译好二进制文件和配置文件复制到和Dokerfile在同一个目录下 FROM alpine:latest - MAINTAINER zhufuyi "g.zhufuyi@gmail.com" # 设置时区为上海 @@ -19,5 +18,6 @@ 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 index 00fd970..8f29402 100644 --- a/deployments/docker-compose/sponge/docker-compose.yml +++ b/deployments/docker-compose/sponge/docker-compose.yml @@ -14,9 +14,9 @@ services: 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 # 重试次数 + #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/deployments/kubernetes/namespace.yaml b/deployments/kubernetes/namespace.yaml new file mode 100644 index 0000000..6df4e4a --- /dev/null +++ b/deployments/kubernetes/namespace.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: sponge diff --git a/deployments/kubernetes/sponge-configmap.yml b/deployments/kubernetes/sponge-configmap.yml new file mode 100644 index 0000000..dd7c7cc --- /dev/null +++ b/deployments/kubernetes/sponge-configmap.yml @@ -0,0 +1,96 @@ +kind: ConfigMap +apiVersion: v1 +metadata: + name: sponge-config + namespace: sponge +data: + conf.yml: |- + # app 设置 + app: + name: "userExample" # 服务名称 + env: "dev" # 运行环境,dev:开发环境,prod:生产环境,pre:预生产环境 + version: "v0.0.0" # 版本 + host: "127.0.0.1" # 主机名称或ip + enableProfile: false # 是否开启性能分析功能,true:开启,false:关闭 + enableMetrics: true # 是否开启指标采集,true:开启,false:关闭 + enableLimit: false # 是否开启限流,true:开启,false:关闭 + enableTracing: false # 是否开启链路跟踪,true:开启,false:关闭 + enableRegistryDiscovery: false # 是否开启注册与发现,true:开启,false:关闭 + + + # http 设置 + http: + port: 8080 # 监听端口 + readTimeout: 3 # 读超时,单位(秒) + writeTimeout: 90 # 写超时,单位(秒),如果enableProfile为true,需要大于60s,pprof做profiling的默认值是60s + + + # grpc 服务设置 + grpc: + port: 9090 # 监听端口 + readTimeout: 3 # 读超时,单位(秒) + writeTimeout: 3 # 写超时,单位(秒) + + + # logger 设置 + logger: + level: "info" # 输出日志级别 debug, info, warn, error,默认是debug + format: "console" # 输出格式,console或json,默认是console + isSave: false # false:输出到终端,true:输出到文件,默认是false + logFileConfig: # isSave=true时有效 + filename: "out.log" # 文件名称,默认值out.log + maxSize: 20 # 最大文件大小(MB),默认值10MB + maxBackups: 50 # 保留旧文件的最大个数,默认值100个 + maxAge: 15 # 保留旧文件的最大天数,默认值30天 + isCompression: true # 是否压缩/归档旧文件,默认值false + + + # mysql 设置 + mysql: + # dsn格式::@(127.0.0.1:3306)/?[k=v& ......] + dsn: "root:123456@(192.168.3.37:3306)/account?parseTime=true&loc=Local&charset=utf8,utf8mb4" + enableLog: true # 是否开启打印所有日志 + slowThreshold: 0 # 如果大于0,只打印时间大于阈值的日志,优先级比enableLog高,单位(毫秒) + maxIdleConns: 3 #设置空闲连接池中连接的最大数量 + maxOpenConns: 50 # 设置打开数据库连接的最大数量 + connMaxLifetime: 30 # 设置了连接可复用的最大时间,单位(分钟) + + + # redis 设置 + redis: + # dsn只适合redis6版本以上,默认用户为default,url格式 [user]:@]127.0.0.1:6379/[db] + dsn: "default:123456@192.168.3.37:6379" + # 适合各个版本redis + addr: 127.0.0.1:6379 + password: "123456" + dB: 0 + minIdleConn: 20 + dialTimeout: 30 # 链接超时,单位(秒) + readTimeout: 500 # 读超时,单位(毫秒) + writeTimeout: 500 # 写超时,单位(毫秒) + poolSize: 100 + poolTimeout: 200 # 连接池超时,单位(秒) + + + # jaeger配置 + jaeger: + agentHost: "192.168.3.37" + agentPort: "6831" + samplingRate: 1.0 # 采样率,0~1之间,0表示禁止采样,大于等于1表示采样所有链路 + + + # etcd配置 + etcd: + addrs: ["192.168.3.37:2379"] + + + # limit配置 + rateLimiter: + dimension: "path" # 限流维度,支持path和ip两种,默认是path + qpsLimit: 1000 # 持续每秒允许成功请求数,默认是500 + maxLimit: 2000 # 瞬时最大允许峰值,默认是1000,通常大于qpsLimit + + + # metrics配置 + metrics: + port: 9082 diff --git a/deployments/kubernetes/sponge-deployment.yml b/deployments/kubernetes/sponge-deployment.yml new file mode 100644 index 0000000..e3ff29a --- /dev/null +++ b/deployments/kubernetes/sponge-deployment.yml @@ -0,0 +1,70 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: sponge-dm + namespace: sponge +spec: + replicas: 1 + selector: + matchLabels: + app: sponge + template: + metadata: + name: sponge-pod + labels: + app: sponge + spec: + containers: + - name: sponge + image: sponge/sponge:latest + # If using a local image, use Never, the default is Always + imagePullPolicy: Never + command: ["./sponge", "-c", "/app/config/conf.yml"] + ports: + - name: http-port + containerPort: 8080 + - name: grpc-port + containerPort: 9090 + - name: metrics-port + containerPort: 9082 + resources: + requests: + cpu: 10m + memory: 10Mi + limits: + cpu: 1000m + memory: 1000Mi + volumeMounts: + - name: sponge-vl + mountPath: /app/config/ + readOnly: true + # 就绪探测 + readinessProbe: + #httpGet: + #port: http-port + #path: /health + exec: + command: ["/bin/grpc_health_probe", "-addr=:9090"] + initialDelaySeconds: 10 + timeoutSeconds: 2 + periodSeconds: 30 + successThreshold: 1 + failureThreshold: 3 + + # 容器存活探测 + livenessProbe: + #httpGet: + #port: http-port + #path: /health + exec: + command: ["/bin/grpc_health_probe", "-addr=:9090"] + initialDelaySeconds: 10 + timeoutSeconds: 2 + periodSeconds: 30 + successThreshold: 1 + failureThreshold: 3 + + volumes: + - name: sponge-vl + configMap: + name: sponge-config diff --git a/deployments/kubernetes/sponge-svc.yml b/deployments/kubernetes/sponge-svc.yml new file mode 100644 index 0000000..9d62d1b --- /dev/null +++ b/deployments/kubernetes/sponge-svc.yml @@ -0,0 +1,19 @@ +apiVersion: v1 +kind: Service +metadata: + name: sponge-svc + namespace: sponge +spec: + selector: + app: sponge + type: ClusterIP + ports: + - name: sponge-svc-http-port + port: 8080 + targetPort: 8080 + - name: sponge-svc-grpc-port + port: 9090 + targetPort: 9090 + - name: sponge-svc-grpc-metrics-port + port: 9082 + targetPort: 9082 diff --git a/internal/model/init_test.go b/internal/model/init_test.go index 1c6e411..2179e07 100644 --- a/internal/model/init_test.go +++ b/internal/model/init_test.go @@ -4,8 +4,9 @@ import ( "testing" "time" - "github.com/stretchr/testify/assert" "github.com/zhufuyi/sponge/config" + + "github.com/stretchr/testify/assert" ) // 测试时需要连接真实数据