Kubernetes源码编译
摘要: 目前有2种方法, 一种方法是直接使用golang来编译出二进制包, 另外一种方法是通过容器来编译. 前提当然是本地需要下载k8s源码 Golang编译 这种方法比较简单, 进入源码目录直接执行make命令即可, 不过对应的k8s版本, 对go语言版本有一些要求, 具体可看k8s网站或者直接编译会提示需要哪个版本go语言.
目前有2种方向, 一种是直接使用golang来编译出二进制包, 另外一种是通过容器来编译.
前提当然是本地需要下载k8s源码
Golang编译
直接编译也可以分成2种,一种是一次编译出来, 一种是每个模块单独编译
全编译
这种方法比较简单, 进入源码目录直接执行make命令即可, 不过对应的k8s版本, 对go语言版本有一些要求, 具体可看k8s网站或者直接编译会提示需要哪个版本go语言.
编译会显示如下信息
[root@SZD-L0113231 kubernetes-release-1.9]# make
+++ [0830 08:50:54] Building the toolchain targets:
k8s.io/kubernetes/hack/cmd/teststale
k8s.io/kubernetes/vendor/github.com/jteeuwen/go-bindata/go-bindata
+++ [0830 08:50:54] Generating bindata:
test/e2e/generated/gobindata_util.go
/home/czs/code/kubernetes-release-1.9 /home/czs/code/kubernetes-release-1.9/test/e2e/generated
/home/czs/code/kubernetes-release-1.9/test/e2e/generated
+++ [0830 08:50:55] Building go targets for linux/amd64:
./vendor/k8s.io/code-generator/cmd/deepcopy-gen
+++ [0830 08:51:02] Building the toolchain targets:
k8s.io/kubernetes/hack/cmd/teststale
k8s.io/kubernetes/vendor/github.com/jteeuwen/go-bindata/go-bindata
+++ [0830 08:51:02] Generating bindata:
test/e2e/generated/gobindata_util.go
/home/czs/code/kubernetes-release-1.9 /home/czs/code/kubernetes-release-1.9/test/e2e/generated
/home/czs/code/kubernetes-release-1.9/test/e2e/generated
+++ [0830 08:51:03] Building go targets for linux/amd64:
./vendor/k8s.io/code-generator/cmd/defaulter-gen
+++ [0830 08:51:08] Building the toolchain targets:
k8s.io/kubernetes/hack/cmd/teststale
k8s.io/kubernetes/vendor/github.com/jteeuwen/go-bindata/go-bindata
+++ [0830 08:51:08] Generating bindata:
test/e2e/generated/gobindata_util.go
/home/czs/code/kubernetes-release-1.9 /home/czs/code/kubernetes-release-1.9/test/e2e/generated
/home/czs/code/kubernetes-release-1.9/test/e2e/generated
+++ [0830 08:51:09] Building go targets for linux/amd64:
./vendor/k8s.io/code-generator/cmd/conversion-gen
+++ [0830 08:51:14] Building the toolchain targets:
k8s.io/kubernetes/hack/cmd/teststale
k8s.io/kubernetes/vendor/github.com/jteeuwen/go-bindata/go-bindata
+++ [0830 08:51:14] Generating bindata:
test/e2e/generated/gobindata_util.go
/home/czs/code/kubernetes-release-1.9 /home/czs/code/kubernetes-release-1.9/test/e2e/generated
/home/czs/code/kubernetes-release-1.9/test/e2e/generated
+++ [0830 08:51:15] Building go targets for linux/amd64:
./vendor/k8s.io/code-generator/cmd/openapi-gen
+++ [0830 08:51:21] Building the toolchain targets:
k8s.io/kubernetes/hack/cmd/teststale
k8s.io/kubernetes/vendor/github.com/jteeuwen/go-bindata/go-bindata
+++ [0830 08:51:21] Generating bindata:
test/e2e/generated/gobindata_util.go
/home/czs/code/kubernetes-release-1.9 /home/czs/code/kubernetes-release-1.9/test/e2e/generated
/home/czs/code/kubernetes-release-1.9/test/e2e/generated
+++ [0830 08:51:21] Building go targets for linux/amd64:
cmd/kube-proxy
cmd/kube-apiserver
cmd/kube-controller-manager
cmd/cloud-controller-manager
cmd/kubelet
cmd/kubeadm
cmd/hyperkube
vendor/k8s.io/kube-aggregator
vendor/k8s.io/apiextensions-apiserver
plugin/cmd/kube-scheduler
cluster/gce/gci/mounter
cmd/kubectl
cmd/gendocs
cmd/genkubedocs
cmd/genman
cmd/genyaml
cmd/genswaggertypedocs
cmd/linkcheck
vendor/github.com/onsi/ginkgo/ginkgo
test/e2e/e2e.test
cmd/kubemark
vendor/github.com/onsi/ginkgo/ginkgo
test/e2e_node/e2e_node.test
cmd/gke-certificates-controller
最后在 _output
里面生成二进制文件
编译指定模块
可以指定相关参数
make WHAT=cmd/kubelet
单独编译
把k8s源码放到如下路径/root/go/src/k8s.io/
, 并且把源码文件夹名称改成kubernetes/
然后进入/cmd/kubelet
(只是以kubelet
为例子)
执行go build -v
命令,如果没出错,可以生成可执行文件
生成的可执行文件在当前文件夹下面
[root@SZD-L0113231 kubectl]# ls -l
total 66592
drwxr-x--- 2 root root 37 Aug 29 13:09 app
-rw-r----- 1 root root 1233 Aug 29 13:09 BUILD
-rwxr-x--- 1 root root 68177868 Aug 30 17:28 kubectl
-rw-r----- 1 root root 772 Aug 29 13:09 kubectl.go
-rw-r----- 1 root root 54 Aug 29 13:09 OWNERS
镜像编译
可以通过编译镜像来编译, 不过编译镜像一般是被墙的,需要自己在dockerhub
或其镜像网站先下载好,
需要注意的是, 每个对应的k8s, 对应的编译镜像有特别的版本要求, 这个可以在源代码中间的build-image/cross/VERSION
里面可以看到对应的版本
docker pull googlecontainer/kube-cross:v1.9.3-1
docker pull googlecontainer/debian-iptables-amd64:v5
然后把tag改为k8s的tag,
docker tag docker.io/googlecontainer/kube-cross:v1.9.3-1 gcr.io/google_containers/kube-cross:v1.9.3-1
docker tag docker.io/googlecontainer/debian-iptables-amd64:v5 gcr.io/googlecontainer/debian-iptables-amd64:v5
不要直接执行很多网上说的命令 ./build/release.sh , 实测会编译很多处理器(如arm,ppc之类的)的版本,直接把我60G的磁盘写满(写了27G),然后出错退出了。
执行 ./build/run.sh make
, 只编译linux/amd64
最后会打印如下信息
+++ [0830 16:30:14] Building go targets for linux/amd64:
cmd/kube-proxy
cmd/kube-apiserver
cmd/kube-controller-manager
cmd/cloud-controller-manager
cmd/kubelet
cmd/kubeadm
cmd/hyperkube
vendor/k8s.io/kube-aggregator
vendor/k8s.io/apiextensions-apiserver
plugin/cmd/kube-scheduler
cluster/gce/gci/mounter
cmd/kubectl
cmd/gendocs
cmd/genkubedocs
cmd/genman
cmd/genyaml
cmd/genswaggertypedocs
cmd/linkcheck
vendor/github.com/onsi/ginkgo/ginkgo
test/e2e/e2e.test
cmd/kubemark
vendor/github.com/onsi/ginkgo/ginkgo
test/e2e_node/e2e_node.test
cmd/gke-certificates-controller
Env for linux/amd64: GOOS=linux GOARCH=amd64 GOROOT=/usr/local/go CGO_ENABLED= CC=
+++ [0830 16:36:23] Placing binaries
+++ [0830 16:37:27] Syncing out of container
+++ [0830 16:37:27] Stopping any currently running rsyncd container
+++ [0830 16:37:27] Starting rsyncd container
+++ [0830 16:37:28] Running rsync
+++ [0830 16:38:47] Stopping any currently running rsyncd container
并且把生成的二进制放到 _output/dockerized/
下面
更多推荐
所有评论(0)