在前面的文章中我们介绍了Pause在Kubernetes中的使用,并对源码进行了解析,这篇文章介绍一下如何进行源码方式的构建和Pause镜像的构建。
下载源码
执行命令:git clone https://github.com/kubernetes/kubernetes
[root@liumiaocn ~]# git clone https://github.com/kubernetes/kubernetes
Cloning into 'kubernetes'...
remote: Enumerating objects: 227, done.
remote: Counting objects: 100% (227/227), done.
remote: Compressing objects: 100% (136/136), done.
remote: Total 1068710 (delta 112), reused 91 (delta 91), pack-reused 1068483
Receiving objects: 100% (1068710/1068710), 663.00 MiB | 11.18 MiB/s, done.
Resolving deltas: 100% (762816/762816), done.
Checking out files: 100% (21979/21979), done.
[root@liumiaocn ~]#
如果速度过慢,也可以直接下载源码包,比如1.17.2的源码可使用如下命令下载
wget https://github.com/kubernetes/kubernetes/archive/v1.17.2.tar.gz
执行编译
- 操作系统和编译器确认
[root@liumiaocn pause]# which cc
/usr/bin/cc
[root@liumiaocn pause]# cc --version
cc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[root@liumiaocn pause]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[root@liumiaocn pause]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
[root@liumiaocn pause]#
- 编译源码和Makefile确认
[root@liumiaocn ~]# cd kubernetes/build/pause
[root@liumiaocn pause]# ls
CHANGELOG.md Dockerfile Makefile orphan.c pause.c
[root@liumiaocn pause]#
使用make pause执行编译
[root@liumiaocn pause]# make pause
cc -Os -Wall -Werror -static -DVERSION=v3.1- pause.c -o pause
[root@liumiaocn pause]# ls
CHANGELOG.md Dockerfile Makefile orphan.c pause pause.c
[root@liumiaocn pause]#
常见编译错误和对应方法
- 缺少libc
[root@liumiaocn pause]# make pause
cc -Os -Wall -Werror -static -DVERSION=v3.1- pause.c -o pause
/usr/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
make: *** [pause] Error 1
[root@liumiaocn pause]#
对应方法:yum install glibc-static -y
构建镜像
- 确认ARCH
[root@liumiaocn pause]# uname -m
x86_64
[root@liumiaocn pause]# arch
x86_64
[root@liumiaocn pause]#
- Dockerfile确认
[root@liumiaocn pause]# cat Dockerfile
# Copyright 2016 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
FROM scratch
ARG ARCH
ADD bin/pause-${ARCH} /pause
ENTRYPOINT ["/pause"]
[root@liumiaocn pause]#
- 构建镜像
传入构建的镜像的参数x86_64,生成pause镜像
[root@liumiaocn pause]# docker build --build-arg ARCH=x86_64 -t pause:latest .
Sending build context to Docker daemon 1.805 MB
Step 1/4 : FROM scratch
--->
Step 2/4 : ARG ARCH
---> Using cache
---> 9567692884cf
Step 3/4 : ADD bin/pause-${ARCH} /pause
---> f57f70cb4d01
Removing intermediate container b9c2223dceed
Step 4/4 : ENTRYPOINT /pause
---> Running in d9e680f0d7e4
---> 941e4c6fbe0e
Removing intermediate container d9e680f0d7e4
Successfully built 941e4c6fbe0e
[root@liumiaocn pause]#
- 镜像确认
[root@liumiaocn pause]# docker images |grep pause
pause latest 941e4c6fbe0e 5 minutes ago 895 kB
[root@liumiaocn pause]#
[root@liumiaocn pause]# docker history pause
IMAGE CREATED CREATED BY SIZE COMMENT
941e4c6fbe0e 5 minutes ago /bin/sh -c #(nop) ENTRYPOINT ["/pause"] 0 B
f57f70cb4d01 5 minutes ago /bin/sh -c #(nop) ADD file:66af603263ce42c... 895 kB
9567692884cf 7 minutes ago /bin/sh -c #(nop) ARG ARCH 0 B
[root@liumiaocn pause]#
与官方镜像进行比较
[root@liumiaocn pause]# docker images |grep gc
gcr.io/google_containers/pause-amd64 3.1 da86e6ba6ca1 2 years ago 742 kB
[root@liumiaocn pause]# docker history gcr.io/google_containers/pause-amd64:3.1
IMAGE CREATED CREATED BY SIZE COMMENT
da86e6ba6ca1 2 years ago /bin/sh -c #(nop) ENTRYPOINT ["/pause"] 0 B
<missing> 2 years ago /bin/sh -c #(nop) ADD file:0ea44d6c15daeb7... 742 kB
<missing> 2 years ago /bin/sh -c #(nop) ARG ARCH 0 B
[root@liumiaocn pause]#