nerdctl incomplete usage guide (developers)

Table of contents

background

Environment configuration

1. Compile the golang executable file

2. Quickly build mirror images


background

K8s adopted containerd after abandoning docker as the runtime in version 1.22. There have been many open source integration tools or solutions for the k8s installation method using containerd as the runtime, so I won’t go into details here. This article only describes some developer skills after docker is replaced.

After switching from docker to containerd, the command line tools ctr and crictl are not very easy to use, so there is nerdctl.
The use of nerdctl is consistent with that of docker, and it has the same experience as docker, which is used to replace docker cli for push/pull/run operations.

Environment configuration

v1.25.0 k8s cluster built by three hosts

Installed with:

nerdctl (if not, please download it yourself and put it in the environment variable path)

containerd

No golang locale

no buildkitd

1. Compile the golang executable file

Development and debugging usually do not require ci/cd to compile and package the image of the entire link, which is time-consuming and may require upstream and downstream collaboration, which increases communication and time costs. Especially in the field of operation and maintenance development, when doing stateless services or invoking some system services, you can use the development test cluster or local debugging. The easiest way is to compile the program directly on the server and start the executable program by fixing the node and mounting the data volume, which saves the step of packaging the image.

And because of well-known reasons, the speed of uploading locally compiled executable programs to the cloud server is very slow. It is better to directly synchronize the code on the server and complete the compilation directly on the server.

The following command shows how to quickly compile project files on the server through a container with a golang compilation environment:

nerdctl run --rm -it  \
-v ${project_dir}:/app \ # 主机项目目录挂载进容器工作目录
-v ${GOPATH}:/go \ # 主机任一目录 用来存放 go pkg
-w /app \
-e CGO_ENABLED=0  \
-e GOPROXY=https://goproxy.cn \
golang:1.18-alpine3.16 \
go build -o build/prods prods.go

2. Quickly build mirror images

Sometimes it is not only necessary to run the workload on k8s to start the executable program, but may also need to be packaged into a container image for testing. If you package it directly on the server through nerdctl build, you need to install the buildkitd background program to support it. The official description is as follows

Commands:
  build       Build an image from a Dockerfile. Needs buildkitd to be running.

Since laziness is our purpose, we simply build it directly in the container, that is, dind (docker in docker). After converting from docker to containerd, it should be called cinc, which is also a common position in the ci/cd pipeline. I use kaniko as the build tool in the container. We can specify the dockerfile path and output path (such as docker hub, harbor) by passing in environment variables for the container. For details, see the official documentation.

nerdctl run  --name myapp \
   -v /mnt/project/myapp/build:/workspace \ # 挂载编译的可执行文件和dockfile到工具目录
    aiotceo/kaniko-executor:v1.6.0 \ # 安装有kaniko的镜像
    --dockerfile /workspace/Dockerfile \
    --destination "192.168.0.152:5000/myapp:v1" \ # 输出到内网harbor
    --context dir:///workspace/

Guess you like

Origin blog.csdn.net/kingu_crimson/article/details/129361786