千家信息网

chaos-mesh开发中的镜像编译过程

发表于:2025-01-31 作者:千家信息网编辑
千家信息网最后更新 2025年01月31日,chaos-mesh开发中的镜像编译过程,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。chaosmesh是一款K8S环境用的混沌工程轻量
千家信息网最后更新 2025年01月31日chaos-mesh开发中的镜像编译过程

chaos-mesh开发中的镜像编译过程,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

chaosmesh是一款K8S环境用的混沌工程轻量级实现,支持多种混沌场景构建。 它可通过docker镜像方式部署,但如果想修改/二次开发,它的编译不是很便捷(you may try..)。 因此这里基于官方方式,拆分成几个子步骤:

  • 最重的部分是第一步

  • 而和代码相关是第二步

  • 第三步是为了生成可部署的镜像

以下执行目录都是在 chaos-mesh/ 前置步骤是:

docker环境golang开发环境git clone https://github.com/pingcap/chaos-mesh

编译生成镜像

1.基础镜像

剥离出,基础镜像生成部分 命名为独立Dockerfile文件:Dockerfile.buildbase

FROM golang:1.14.4-alpine3.12 AS build_baseARG HTTPS_PROXYARG HTTP_PROXYRUN apk add --no-cache gcc g++ make bash gitRUN apk add --update nodejs yarn

执行镜像生成 2C8G,约耗时2小时,主要耗时是apk安装过程

docker build -f Dockerfile.buildbase -t chaosmesh_buildbase:0.1 .

执行过程输出

Sending build context to Docker daemon  2.267MBStep 1/5 : FROM golang:1.14.4-alpine3.12 AS build_base ---> 3289bf11c284Step 2/5 : ARG HTTPS_PROXY ---> Using cache ---> b71cc9152dbaStep 3/5 : ARG HTTP_PROXY ---> Using cache ---> 00340bb53cfbStep 4/5 : RUN apk add --no-cache gcc g++ make bash git ---> Running in 25a64b772133fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gzfetch http://dl-cdn.alpinelinux.org/alpine/v3.12/community/x86_64/APKINDEX.tar.gz(1/24) Installing ncurses-terminfo-base (6.2_p20200523-r0)(2/24) Installing ncurses-libs (6.2_p20200523-r0)(3/24) Installing readline (8.0.4-r0)(4/24) Installing bash (5.0.17-r0)Executing bash-5.0.17-r0.post-install(5/24) Installing libgcc (9.3.0-r2)(6/24) Installing libstdc++ (9.3.0-r2)(7/24) Installing binutils (2.34-r1)(8/24) Installing gmp (6.2.0-r0)(9/24) Installing isl (0.18-r0)(10/24) Installing libgomp (9.3.0-r2)(11/24) Installing libatomic (9.3.0-r2)(12/24) Installing libgphobos (9.3.0-r2)(13/24) Installing mpfr4 (4.0.2-r4)(14/24) Installing mpc1 (1.1.0-r1)(15/24) Installing gcc (9.3.0-r2)(16/24) Installing musl-dev (1.1.24-r8)(17/24) Installing libc-dev (0.7.2-r3)(18/24) Installing g++ (9.3.0-r2)(19/24) Installing nghttp2-libs (1.41.0-r0)(20/24) Installing libcurl (7.69.1-r0)(21/24) Installing expat (2.2.9-r1)(22/24) Installing pcre2 (10.35-r0)(23/24) Installing git (2.26.2-r0)(24/24) Installing make (4.3-r0)Executing busybox-1.31.1-r16.triggerOK: 218 MiB in 39 packagesRemoving intermediate container 25a64b772133 ---> 7b35c82ec76aStep 5/5 : RUN apk add --update nodejs yarn ---> Running in 9b8192793f49fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gzfetch http://dl-cdn.alpinelinux.org/alpine/v3.12/community/x86_64/APKINDEX.tar.gz(1/5) Installing brotli-libs (1.0.7-r5)(2/5) Installing c-ares (1.16.1-r0)(3/5) Installing libuv (1.37.0-r0)(4/5) Installing nodejs (12.17.0-r0)(5/5) Installing yarn (1.22.4-r0)Executing busybox-1.31.1-r16.triggerOK: 253 MiB in 44 packagesRemoving intermediate container 9b8192793f49 ---> 6ec726d66bc3Successfully built 6ec726d66bc3Successfully tagged chaosmesh_buildbase:0.1

产生内容

# docker images ...chaosmesh_buildbase                                                           0.1                 6ec726d66bc3        45 seconds ago      625MB

2.镜像+代码合集

剥离出,业务镜像生成部分 命名为独立Dockerfile文件:Dockerfile.buildbinary

FROM chaosmesh_buildbase:0.1 as build_baseENV GO111MODULE=onENV GOPROXY=https://goproxy.cn       # 新增的,处理包下载失败问题WORKDIR /srcCOPY go.mod .COPY go.sum .RUN go mod downloadFROM build_base AS binary_builderARG HTTPS_PROXYARG HTTP_PROXYARG UIARG SWAGGERCOPY . /srcWORKDIR /srcRUN make binary

执行镜像生成 2C8G,约耗时0.5小时,后续执行有只需2分钟

docker build -f Dockerfile.buildbinary -t chaosmesh_my:0.1 .

执行过程输出

Sending build context to Docker daemon  2.268MBStep 1/15 : FROM chaosmesh_buildbase:0.1 as build_base ---> 6ec726d66bc3Step 2/15 : ENV GO111MODULE=on ---> Using cache ---> 77f7eb612c78Step 3/15 : ENV GOPROXY=https://goproxy.cn ---> Running in ff92b4113844Removing intermediate container ff92b4113844 ---> faaa5e678ee0Step 4/15 : WORKDIR /src ---> Running in e677d7457f11Removing intermediate container e677d7457f11 ---> a919ebec1df6Step 5/15 : COPY go.mod . ---> 1651592cc8ebStep 6/15 : COPY go.sum . ---> 6a2c3e26a54aStep 7/15 : RUN go mod download ---> Running in f7a15063e33aRemoving intermediate container f7a15063e33a ---> e406d3f23575Step 8/15 : FROM build_base AS binary_builder ---> e406d3f23575Step 9/15 : ARG HTTPS_PROXY ---> Running in 360b9686a970Removing intermediate container 360b9686a970 ---> f7a35aac4e60Step 10/15 : ARG HTTP_PROXY ---> Running in 0d53c5bd7bb3Removing intermediate container 0d53c5bd7bb3 ---> 242f81bdf7bcStep 11/15 : ARG UI ---> Running in 9a8a3a6a3aeeRemoving intermediate container 9a8a3a6a3aee ---> 5ec849e5d942Step 12/15 : ARG SWAGGER ---> Running in dd1f9766dfefRemoving intermediate container dd1f9766dfef ---> 646f623900e5Step 13/15 : COPY . /src ---> 780dcca532a0Step 14/15 : WORKDIR /src ---> Running in efe4967d715dRemoving intermediate container efe4967d715d ---> 39b9806a50f9Step 15/15 : RUN make binary ---> Running in ebf1e23916a0GO15VENDOREXPERIMENT="1" CGO_ENABLED=0 GOOS="" GOARCH="" go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.2.5go: found sigs.k8s.io/controller-tools/cmd/controller-gen in sigs.k8s.io/controller-tools v0.2.5/go/bin/controller-gen object:headerFile=./hack/boilerplate/boilerplate.generatego.txt paths="./..."GO15VENDOREXPERIMENT="1" CGO_ENABLED=1 GOOS="" GOARCH="" go build -ldflags '-s -w -X 'github.com/pingcap/chaos-mesh/pkg/version.buildDate=2020-06-12T09:08:08Z'' -o bin/chaos-daemon ./cmd/chaos-daemon/main.goGO15VENDOREXPERIMENT="1" CGO_ENABLED=0 GOOS="" GOARCH="" go build -ldflags '-s -w -X 'github.com/pingcap/chaos-mesh/pkg/version.buildDate=2020-06-12T09:08:50Z'' -o bin/chaos-controller-manager ./cmd/controller-manager/*.goGO15VENDOREXPERIMENT="1" CGO_ENABLED=0 GOOS="" GOARCH="" go build -ldflags '-s -w -X 'github.com/pingcap/chaos-mesh/pkg/version.buildDate=2020-06-12T09:08:58Z'' -o bin/chaosfs ./cmd/chaosfs/*.goGO15VENDOREXPERIMENT="1" CGO_ENABLED=1 GOOS="" GOARCH="" go build -ldflags '-s -w -X 'github.com/pingcap/chaos-mesh/pkg/version.buildDate=2020-06-12T09:09:02Z'' -tags "" -o bin/chaos-dashboard cmd/chaos-dashboard/*.goRemoving intermediate container ebf1e23916a0 ---> 166906930d82Successfully built 166906930d82Successfully tagged chaosmesh_my:0.1

产生内容 生成的镜像大了不少,还有几个中间镜像文件"()

# docker images...chaosmesh_my                                                                  0.1                 166906930d82        3 minutes ago       2.67GB                                                                                      f6e5468571fe        9 minutes ago       625MB                                                                                      6255c2ee2630        20 minutes ago      625MBchaosmesh_buildbase                                                           0.1                 6ec726d66bc3        29 minutes ago      625MB

对比官方安装镜像的效果:

# docker images...pingcap/chaos-dashboard                                                       latest              763cddd4c303        23 hours ago        57.5MBpingcap/chaos-mesh                                                            latest              6e2777640799        42 hours ago        40.6MBpingcap/chaos-daemon                                                          latest              af1cb5058c1d        42 hours ago        59.7MB

3.业务镜像

注:Dockerfile的主体,来自chaos-mesh项目本身的 ./images/各个组件/Dockerfile 修改的是 COPY --from=二进制文件来源镜像

chaos-daemon

业务镜像 命名为独立Dockerfile文件:Dockerfile.chaos-daemon

FROM alpine:3.10ARG HTTPS_PROXYARG HTTP_PROXYRUN apk add --no-cache tzdata iptables ipset stress-ng iproute2COPY --from=chaosmesh_my:0.1 /src/bin/chaos-daemon /usr/local/bin/chaos-daemon

执行镜像生成

docker build -f Dockerfile.chaos-daemon -t chaos-daemon:my .

产生内容

chaos-daemon                                                                  my                  72f767fd07a3        15 seconds ago      59.7MB

chaos-mesh

业务镜像 命名为独立Dockerfile文件:Dockerfile.chaos-mesh

FROM alpine:3.10ARG HTTPS_PROXYARG HTTP_PROXYRUN apk add tzdata --no-cacheCOPY --from=chaosmesh_my:0.1 /src/bin/chaos-controller-manager /usr/local/bin/chaos-controller-manager

执行镜像生成

docker build -f Dockerfile.chaos-mesh -t chaos-mesh:my .

产生内容

chaos-mesh                                                                    my                  36bf11bc3837        41 seconds ago      40.6MB

扩展

对比镜像

对官方镜像 vs 自生成镜像,可以简单对比

docker history 镜像ID --no-trunc

因为Dockerfile都是一致的,差别就是最后copy的那个二进制文件

看完上述内容,你们掌握chaos-mesh开发中的镜像编译过程的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!

0