前提环境
#go环境:
[root@urbancabin go]# go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://mirrors.aliyun.com/goproxy/,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2087487172=/tmp/go-build -gno-record-gcc-switches"
[root@urbancabin urbancabin]# go env|grep -E "GOOS|GOARCH|GO111MODULE|GOPROXY"
GO111MODULE="on"
GOARCH="amd64"
GOOS="linux"
GOPROXY="https://mirrors.aliyun.com/goproxy/,direct" #或者https://goproxy.cn,direct
#修改环境配置命令
go env -w GOOS="linux"
go env -w GOHOSTARCH="amd64"
#其他参数修改类似
1、创建go项目
[root@urbancabin go]# mkdir urbancabin
[root@urbancabin go]# cd urbancabin/
2、初始化项目
[root@urbancabin urbancabin]# ls
[root@urbancabin urbancabin]# go mod init urbancabin
go: creating new go.mod: module urbancabin
3、创建一个gin项目
[root@urbancabin go]# cat gin.go
package main
import (
"github.com/gin-gonic/gin"
"time"
)
func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"code": 200,
"message": "Welcome UrbanCabin! 当前时间为:" + time.Now().Format("2020-08-15 10:10:10"),
})
})
err := r.Run(":1111")
if err != nil {
return
}
}
可以本地跑一边运行下程序是否正常。
4、Dockerfile
[root@urbancabin go]# cat Dockerfile
#对应自己的go版本, -alpine:指代镜像比较小
FROM golang:1.19-alpine as urbancabin
#为我们的镜像设置必要的环境变量
ENV GO111MODULE=on \
GOPROXY=https://goproxy.cn,direct \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
WORKDIR /project/docker_go/
#COPY go.mod , go.sum and download the dependencied
COPY go.* ./
RUN go mod download
#COPY All things inside the project and build
COPY . .
RUN go build -o /project/docker_go/build/urbancabin .
EXPOSE 1111
ENTRYPOINT ["/project/docker_go/build/urbancabin"]
FROM alpine:latest
RUN mkdir -p /usr/share/zoneinfo/Asia/
COPY Shanghai /etc/localtime
WORKDIR /
COPY --from=urbancabin /project/docker_go/build/urbancabin /
CMD "./urbancabin"
释义:
FROM golang:1.19-alpine as urbancabin :将FROM golang:1.19-alpine用作此Docker构建的基础镜像,和自己使用的go版本进行统一。不知道的,可以查看go.mod看看自己使用的版本。as urbancabin 给基础镜像一个别名
ENV :环境配置,配置代理,编译环境。
WORKDIR:工作目录路径。
COPY go.* ./ :从项目复制go.mod & go.sum文件到工作目录。
RUN go mod dowmload:从go modules 下载项目依赖。
COPY . .:将我们项目中的所有内容复制到工作目录中。
RUN go build -o /project/docker_go/build/urbancabin . : 在工作目录中构建我们的项目并将其/project/docker_go/build/urbancabin作为二进制文件输出。
EXPOSE 1111 : 告诉docker我们的代码将暴露端口1111
ENTRYPOINT ["/project/docker_first/build/myapp"] :当我们运行这个镜像的容器时,它将从我们的构建可执行文件开始执行
FROM alpine:latest 拉取基础镜像
RUN mkdir -p /usr/share/zoneinfo/Asia/ 创建时区目录
COPY Shanghai /etc/localtime copy本地时间到容器目录
WORKDIR / 工作目录路径
COPY --from=urbancabin /project/docker_go/build/urbancabin / 引用上面编译好的基础镜像
CMD "./urbancabin" 运行
5、准备好时区文件
[root@urbancabin urbancabin]# cp /usr/share/zoneinfo/Asia/Shanghai .
[root@urbancabin urbancabin]# ls
Dockerfile gin.go go.mod Shanghai
6、构建镜像
docker build -f Dockerfile -t demo:v1 .
报错:
[root@urbancabin urbancabin]# docker build -f Dockerfile -t demo:v1 .
Sending build context to Docker daemon 6.144kB
Step 1/17 : FROM golang:1.19-alpine as urbancabin
---> d0f5238dcb8b
Step 2/17 : ENV GO111MODULE=on GOPROXY=https://goproxy.cn,direct CGO_ENABLED=0 GOOS=linux GOARCH=amd64
---> Using cache
---> 1f82e2837c25
Step 3/17 : ENV TZ=Asia/Shanghai
---> Using cache
---> 3e6cb8a3322e
Step 4/17 : RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
---> Using cache
---> d2f36d8d1b34
Step 5/17 : WORKDIR /project/docker_go/
---> Using cache
---> b1bc9767a7a9
Step 6/17 : COPY go.* ./
---> 6982bc43881d
Step 7/17 : RUN go mod download
---> Running in 7fb3f8033b7a
go: no module dependencies to download
Removing intermediate container 7fb3f8033b7a
---> 214031849c62
Step 8/17 : COPY . .
---> fdfca09ad314
Step 9/17 : RUN go build -o /project/docker_go/build/urbancabin .
---> Running in deb81a265de2
gin.go:4:9: no required module provides package github.com/gin-gonic/gin; to add it:
go get github.com/gin-gonic/gin
The command '/bin/sh -c go build -o /project/docker_go/build/urbancabin .' returned a non-zero code: 1
解决:
[root@urbancabin go]# go get -v github.com/gin-gonic/gin
go: added github.com/gin-contrib/sse v0.1.0
go: added github.com/gin-gonic/gin v1.8.1
go: added github.com/go-playground/locales v0.14.0
go: added github.com/go-playground/universal-translator v0.18.0
go: added github.com/go-playground/validator/v10 v10.10.0
go: added github.com/goccy/go-json v0.9.7
go: added github.com/json-iterator/go v1.1.12
go: added github.com/leodido/go-urn v1.2.1
go: added github.com/mattn/go-isatty v0.0.14
go: added github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421
go: added github.com/modern-go/reflect2 v1.0.2
go: added github.com/pelletier/go-toml/v2 v2.0.1
go: added github.com/ugorji/go/codec v1.2.7
go: added golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97
go: added golang.org/x/net v0.0.0-20210226172049-e18ecbb05110
go: added golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069
go: added golang.org/x/text v0.3.6
go: added google.golang.org/protobuf v1.28.0
go: added gopkg.in/yaml.v2 v2.4.0
[root@urbancabin go]# ls
Dockerfile gin.go go.mod go.sum Shanghai
再次运行:
[root@urbancabin urbancabin]# docker build -f Dockerfile -t demo:v1 .
Sending build context to Docker daemon 14.85kB
Step 1/17 : FROM golang:1.19-alpine as urbancabin
---> d0f5238dcb8b
Step 2/17 : ENV GO111MODULE=on GOPROXY=https://goproxy.cn,direct CGO_ENABLED=0 GOOS=linux GOARCH=amd64
---> Using cache
---> 1f82e2837c25
Step 3/17 : ENV TZ=Asia/Shanghai
---> Using cache
---> 3e6cb8a3322e
Step 4/17 : RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
---> Using cache
---> d2f36d8d1b34
Step 5/17 : WORKDIR /project/docker_go/
---> Using cache
---> b1bc9767a7a9
Step 6/17 : COPY go.* ./
---> 7c1e1f8c1549
Step 7/17 : RUN go mod download
---> Running in b576c6aaa2fd
Removing intermediate container b576c6aaa2fd
---> e419701dba77
Step 8/17 : COPY . .
---> 327d03a3ef68
Step 9/17 : RUN go build -o /project/docker_go/build/urbancabin .
---> Running in a30ad081517b
Removing intermediate container a30ad081517b
---> 49e03df14b4b
Step 10/17 : EXPOSE 1111
---> Running in bd250a5ccad4
Removing intermediate container bd250a5ccad4
---> f37b5abdf367
Step 11/17 : ENTRYPOINT ["/project/docker_go/build/urbancabin"]
---> Running in 4af9809bcacf
Removing intermediate container 4af9809bcacf
---> eee2ccff7182
Step 12/17 : FROM alpine:latest
---> c059bfaa849c
Step 13/17 : RUN mkdir -p /usr/share/zoneinfo/Asia/
---> Using cache
---> 638ab17ab720
Step 14/17 : COPY Shanghai /etc/localtime
---> Using cache
---> 7c844f1d3dbd
Step 15/17 : WORKDIR /
---> Using cache
---> b90e9bf2497c
Step 16/17 : COPY --from=urbancabin /project/docker_go/build/urbancabin /
---> 366fb9933544
Step 17/17 : CMD "./urbancabin"
---> Running in 72364fc179dd
Removing intermediate container 72364fc179dd
---> 01e08eae887f
Successfully built 01e08eae887f
Successfully tagged demo:v1
7、运行容器
[root@urbancabin urbancabin]# docker run -d -p 1111:1111 demo:v1
c42384b575882b1973f337f307e0fc7c74b13566046e970b15d4dd68d6231241
[root@urbancabin urbancabin]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c42384b57588 demo:v1 "/bin/sh -c \"./urban…" 14 seconds ago Up 13 seconds 0.0.0.0:1111->1111/tcp, :::1111->1111/tcp relaxed_snyder
8、验证
[root@urbancabin urbancabin]# curl localhost:1111
{"code":200,"message":"Welcome UrbanCabin! 当前时间为:2022-08-15 10:40:21"}
至此,容器没问题,接下来将此go程序发布到k3s...
9、打包镜像
[root@urbancabin urbancabin]# docker image list|grep demo
demo v1 50c6dbfa8f74 2 minutes ago 15.9MB
[root@urbancabin urbancabin]# docker tag demo:v1 demo_go:v1
[root@urbancabin urbancabin]# docker image list|grep demo_go
demo_go v1 50c6dbfa8f74 3 minutes ago 15.9MB
10、推送到远程仓库
[root@urbancabin urbancabin]# push_remote demo_go:v1
0、下载镜像:demo_go:v1
1、将要备份的镜像名:demo_go
2、将要备份的镜像版本:v1
3、给镜像重新打阿里云Tag:registry.cn-hangzhou.aliyuncs.com/urbancabin/demo_go:v1
4、给镜像重新打腾讯云Tag:ccr.ccs.tencentyun.com/urbancabin/demo_go:v1
5、推镜像到阿里云仓库
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/urbancabin/demo_go]
b44969be388b: Pushed
80598ca4e625: Pushed
8c18e3406525: Pushed
8d3ac3489996: Mounted from urbancabin/alpine-glibc
v1: digest: sha256:98c8436c1b2c31b7103a6e0eed6f1adc8b9b323b92822c39aedf5010a58808cc size: 1153
6、推镜像到腾讯云仓库
The push refers to repository [ccr.ccs.tencentyun.com/urbancabin/demo_go]
b44969be388b: Pushed
80598ca4e625: Pushed
8c18e3406525: Pushed
8d3ac3489996: Mounted from urbancabin/alpine-glibc
v1: digest: sha256:98c8436c1b2c31b7103a6e0eed6f1adc8b9b323b92822c39aedf5010a58808cc size: 1153
7、demo_go:v1镜像推送完成!
11、更新线上demo
旧版本:
[root@urbancabin urbancabin]# curl http://localhost:30111/
当前时间:2022-08-15 10:44:57
[root@urbancabin urbancabin]# kubectl get po -n demo -oyaml|grep registry
- image: registry.cn-hangzhou.aliyuncs.com/urbancabin/demo:v1
修改镜像版本
[root@urbancabin urbancabin]# kubectl edit deploy -n demo urbancabin-demo
- image: registry.cn-hangzhou.aliyuncs.com/urbancabin/demo_go:v1
新版本:
[root@urbancabin urbancabin]# kubectl get po -n demo -oyaml|grep registry
- image: registry.cn-hangzhou.aliyuncs.com/urbancabin/demo_go:v1
[root@urbancabin urbancabin]# kubectl get po -n demo
NAME READY STATUS RESTARTS AGE
urbancabin-demo-589cd8999b-vxnwr 1/1 Running 0 71s
12、验证:
[root@urbancabin urbancabin]# curl -s http://localhost:30111/|jq
{
"code": 200,
"message": "Welcome UrbanCabin! 当前时间为:2022-08-15 10:54:33"
}
至此,发布成功!
参考链接:
https://blog.csdn.net/weixin_51261234/article/details/125906988
https://blog.csdn.net/HenryLeeJ/article/details/122896265
https://cdn.modb.pro/db/381984