Kubernetes基礎:源碼編譯並構建Pause鏡像

在前面的文章中我們介紹了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]#
發佈了1086 篇原創文章 · 獲贊 1301 · 訪問量 403萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章