docker构建goweb多架构镜像混合部署
[root@ecs-arm-ky-005 go-web]# kubectl get pods -n goweb-ns -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES go-web-dm-6d65fdc94b-2dwbd 1/1 Running 0 7s 10.244.3.84 ecs-arm-ky-002 <none> <none> go-web-dm-6d65fdc94b-hfxdq 1/1 Running 0 7s 10.244.1.33 ecs-x86-centos-002 <none> <none> go-web-dm-6d65fdc94b-hw6fr 1/1 Running 0 7s 10.244.2.37 ecs-x86-centos-001 <none> <none> go-web-dm-6d65fdc94b-l4nzz 1/1 Running 0 7s 10.244.2.36 ecs-x86-centos-001 <none> <none> go-web-dm-6d65fdc94b-ss8r2 1/1 Running 0 7s 10.244.1.32 ecs-x86-centos-002 <none> <none> [root@ecs-arm-ky-005 go-web]# kubectl get nodes -o wide NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME ecs-arm-ky-002 Ready <none> 9d v1.18.2 192.168.0.208 <none> Kylin Linux Advanced Server V10 (Tercel) 4.19.90-17.5.ky10.aarch64 containerd://1.6.0 ecs-arm-ky-005 Ready master 13d v1.18.2 192.168.0.170 <none> Kylin Linux Advanced Server V10 (Tercel) 4.19.90-17.5.ky10.aarch64 containerd://1.6.0 ecs-x86-centos-001 Ready <none> 9d v1.18.2 192.168.0.228 <none> CentOS Linux 7 (Core) 3.10.0-1160.62.1.el7.x86_64 containerd://1.5.11 ecs-x86-centos-002 Ready <none> 9d v1.18.2 192.168.0.240 <none> CentOS Linux 7 (Core) 3.10.0-1160.62.1.el7.x86_64 docker://20.10.14 [root@ecs-arm-ky-005 go-web]#
[root@ecs-75124 go-web]# cat Dockerfile FROM --platform=$BUILDPLATFORM golang:1.14 as builder ARG TARGETARCH WORKDIR /app COPY main.go /app/main.go RUN go mod init gin RUN go mod edit -require github.com/gin-gonic/gin@latest RUN mkdir /app/output RUN CGO_ENABLED=0 GOOS=linux GOARCH=$TARGETARCH go build -a -o output/main main.go FROM alpine:latest WORKDIR /root COPY --from=builder /app/output/main . CMD ["/bin/sh","-c","/root/main"] [root@ecs-75124 go-web]# cat main.go package main import ( "net/http" "github.com/gin-gonic/gin" ) func statusOKHandler(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"status": "success"}) } func versionHandler(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"version": "v1.0"}) } func main() { router := gin.New() router.Use(gin.Recovery()) router.GET("/ping", statusOKHandler) router.GET("/version", versionHandler) router.Run(":8080") } [root@ecs-75124 go-web]#
[root@ecs-75124 go-web]# docker buildx build -t swr.cn-north-4.myhuaweicloud.com/metrics/go-web:v1.0.1 --platform=linux/arm,linux/arm64,linux/amd64 . --push [+] Building 95.0s (35/35) FINISHED => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 425B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [linux/amd64 internal] load metadata for docker.io/library/alpine:latest 2.1s => [linux/amd64 internal] load metadata for docker.io/library/golang:1.14 2.4s => [linux/arm64 internal] load metadata for docker.io/library/alpine:latest 2.1s => [linux/arm/v7 internal] load metadata for docker.io/library/alpine:latest 2.0s => [linux/arm64 stage-1 1/3] FROM docker.io/library/alpine:latest@sha256:4edbd2beb5f78b1014028f4fbb99f3237d9561100b6881aabbf5acce2c4f9454 0.1s => => resolve docker.io/library/alpine:latest@sha256:4edbd2beb5f78b1014028f4fbb99f3237d9561100b6881aabbf5acce2c4f9454 0.0s => [internal] load build context 0.0s => => transferring context: 29B 0.0s => [linux/amd64 builder 1/7] FROM docker.io/library/golang:1.14@sha256:1a7173b5b9a3af3e29a5837e0b2027e1c438fd1b83bbee8f221355087ad416d6 0.1s => => resolve docker.io/library/golang:1.14@sha256:1a7173b5b9a3af3e29a5837e0b2027e1c438fd1b83bbee8f221355087ad416d6 0.1s => [linux/amd64 stage-1 1/3] FROM docker.io/library/alpine:latest@sha256:4edbd2beb5f78b1014028f4fbb99f3237d9561100b6881aabbf5acce2c4f9454 0.1s => => resolve docker.io/library/alpine:latest@sha256:4edbd2beb5f78b1014028f4fbb99f3237d9561100b6881aabbf5acce2c4f9454 0.1s => [linux/arm/v7 stage-1 1/3] FROM docker.io/library/alpine:latest@sha256:4edbd2beb5f78b1014028f4fbb99f3237d9561100b6881aabbf5acce2c4f9454 0.1s => => resolve docker.io/library/alpine:latest@sha256:4edbd2beb5f78b1014028f4fbb99f3237d9561100b6881aabbf5acce2c4f9454 0.1s => CACHED [linux/amd64 builder 2/7] WORKDIR /app 0.0s => CACHED [linux/amd64 builder 3/7] COPY main.go /app/main.go 0.0s => [linux/amd64 builder 4/7] RUN go mod init gin 0.2s => [linux/amd64 builder 4/7] RUN go mod init gin 0.2s => [linux/amd64 builder 4/7] RUN go mod init gin 0.2s => [linux/amd64 builder 5/7] RUN go mod edit -require github.com/gin-gonic/gin@latest 0.3s => [linux/amd64 builder 5/7] RUN go mod edit -require github.com/gin-gonic/gin@latest 0.3s => [linux/amd64 builder 5/7] RUN go mod edit -require github.com/gin-gonic/gin@latest 0.2s => [linux/amd64 builder 6/7] RUN mkdir /app/output 0.2s => [linux/amd64 builder 6/7] RUN mkdir /app/output 0.2s => [linux/amd64 builder 6/7] RUN mkdir /app/output 0.2s => [linux/amd64 builder 7/7] RUN GOOS=linux GOARCH=arm64 go build -a -o output/main main.go 54.9s => [linux/amd64 builder 7/7] RUN GOOS=linux GOARCH=arm go build -a -o output/main main.go 55.2s => [linux/amd64 builder 7/7] RUN GOOS=linux GOARCH=amd64 go build -a -o output/main main.go 56.1s => CACHED [linux/arm64 stage-1 2/3] WORKDIR /root 0.0s => [linux/arm64 stage-1 3/3] COPY --from=builder /app/output/main . 0.2s => CACHED [linux/arm/v7 stage-1 2/3] WORKDIR /root 0.0s => [linux/arm/v7 stage-1 3/3] COPY --from=builder /app/output/main . 0.1s => CACHED [linux/amd64 stage-1 2/3] WORKDIR /root 0.0s => [linux/amd64 stage-1 3/3] COPY --from=builder /app/output/main . 0.1s => exporting to image 35.4s => => exporting layers 1.5s => => exporting manifest sha256:1a7b12f579bd49b468e3256f2c3873628bd30549a7777674b7ccad1509d11046 0.0s => => exporting config sha256:39e86cbb03e02e40fef0ef4de28a8348276c0996105f9435f89d83f52b171b8d 0.0s => => exporting manifest sha256:0563a08fcadc6b610d813f7ebb4110d2dc5f405505f8c494d95bdc9447a95c39 0.0s => => exporting config sha256:d889cd8219af0d9e96d328c78952868cb95f8280dd3298794265b4d3a65c48ee 0.0s => => exporting manifest sha256:bd4fa8f08b065ddb8dcfdce0baefff93e2bc0e086586fb9b04867d5b236a70d7 0.0s => => exporting config sha256:da0109907bc70ae86ed21c821ae6eb205daa1286929e1067c6b1a05b97890c39 0.0s => => exporting manifest list sha256:4085134edb891fa2664028865f1e1e74e238cd177ffb12377fbd662eb93ec2d5 0.0s => => pushing layers 32.7s => => pushing manifest for swr.cn-north-4.myhuaweicloud.com/metrics/go-web:v1.0.1@sha256:4085134edb891fa2664028865f1e1e74e238cd177ffb12377fbd662eb93ec2d5 1.0s => [auth] metrics/go-web:,pull,push token for swr.cn-north-4.myhuaweicloud.com 0.0s => [auth] metrics/go-web:,pull,push token for swr.cn-north-4.myhuaweicloud.com 0.0s => [auth] metrics/go-web:,pull,push token for swr.cn-north-4.myhuaweicloud.com 0.0s
访问网站:
http://117.78.9.69:31080/version
增加ELB
http://124.70.16.59/version
https://zhuanlan.zhihu.com/p/299116754
https://blog.csdn.net/Canger_/article/details/122239139
架构相关变量
Dockerfile支持如下变量
TARGETPLATFORM
构建镜像的目标平台,例如 linux/amd64, linux/arm/v7, windows/amd64
TARGETOS
TARGETPLATFORM的 OS 类型,例如linux,windows
TARGETARCH
TARGETPLATFORM 的架构类型,例如 amd64, arm
TARGETVARIANT
TARGETPLATFORM的变种,该变量可能为空,例如v7
BUILDPLATFORM
构建镜像主机平台,例如 linux/amd64
BUILDOS
BUILDPLATFORM的 OS 类型,例如linux
BUILDARCH
BUILDPLATFORM的架构类型,例如amd64
BUILDVARIANT
BUILDPLATFORM的变种,该变量可能为空,例如v7