Implementation Steps of Docker Constructing kubectl Image
- 2021-10-11 19:58:43
- OfStack
If the application service is deployed using k8s integration gitlab ci/cd, the kubeclt image is required in the gitlab-ci process
There are two ways to build an kubectl image using docker
Mode 1 (the mirror image is relatively small by 1 point, about 45.8 M)
Installing kubectl executable files on Linux using curl
cd /usr/local/bin
curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.20.1/bin/linux/amd64/kubectl
chmod +x ./kubectl
Refer to the official installation documentation for details
Next, create a new Dockerfile file in the/usr/local/bin directory, and write the following contents
FROM alpine:latest
COPY kubectl /usr/local/bin/
RUN chmod +x /usr/local/bin/kubectl
After the Dockerfile file is built, execute the build command in the same directory
docker build -t registry.cn-hangzhou.aliyuncs.com/sanchar/kubectl:v1.20.1 .
Wait for the image build to complete
Mode 2 (the mirror image is relatively large, about 48.9 M)
This method is relatively convenient, but the construction is slow, and the kubectl executable file needs to be downloaded during the construction process
Create a new Dockerfile file directly, and write the following contents
FROM alpine:latest
RUN apk add --update -t deps curl
RUN curl -L https://storage.googleapis.com/kubernetes-release/release/v1.20.1/bin/linux/amd64/kubectl -o /usr/local/bin/kubectl \
&& chmod +x /usr/local/bin/kubectl
RUN apk del --purge deps \
&& rm /var/cache/apk/*
Execute the build command in the same directory
docker build -t registry.cn-hangzhou.aliyuncs.com/sanchar/kubectl:v1.20.1 .
Finally
1. Test whether the built image can be used normally
docker run -it registry.cn-hangzhou.aliyuncs.com/sanchar/kubectl:v1.20.1 kubectl version --client
The output is as follows, that is, the constructed image can be used normally
Client Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.1", GitCommit:"c4d752765b3bbac2237bf87cf0b1c2e307844666", GitTreeState:"clean", BuildDate:"2020-12-18T12:09:25Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"}
2. Pull the image from Registry
docker pull registry.cn-hangzhou.aliyuncs.com/sanchar/kubectl:v1.20.1
3. Push the mirror image to Registry, and replace [ImageId] with the corresponding mirror image ID
docker login --username= User name registry.cn-hangzhou.aliyuncs.com
docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/sanchar/kubectl:v1.20.1
docker push registry.cn-hangzhou.aliyuncs.com/sanchar/kubectl:v1.20.1