欢迎访问我的GitHub
这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
本篇概览
-
《Kubernetes源码学习》系列是我在学习Kubernetes过程中的收获和总结,希望能对读者您起到参考作用,也希望得到您的宝贵意见和指导,共同进步;
-
本文是系列文章的第一篇,实战Kubernetes源码的下载和编译,这项技能会在后面的深入学习过程中用到,修改和运行源码用来验证自己的推测,是学习的常用手段;
关于Kubernetes版本
- 整个系列文章使用的源码和环境都是1.13版本;
准备环境
- 操作系统:CentOS 7.6.1810
- go版本:1.12
下载源码
- 确保GO环境已经设置完成,执行以下命令,创建目录用于存放Kubernetes源码:
mkdir -p $GOPATH/src/k8s.io
- 进入目录$GOPATH/src/k8s.io,执行以下命令即可开始下载:
git clone https://github.com/kubernetes/kubernetes -b release-1.13
提示:源码文件达到了八百多兆,下载时间会很长,推荐通过修改host的方法加速github下载,具体方法请自行搜索;
- 下载完毕后,k8s.io目录下出现一个名为kubernetes的文件夹,里面内容如下:
[root@golang ~]# ls $GOPATH/src/k8s.io
kubernetes
[root@golang ~]# ls $GOPATH/src/k8s.io/kubernetes
api CHANGELOG-1.12.md cmd docs LICENSE Makefile.generated_files pkg SECURITY_CONTACTS test vendor
build CHANGELOG.md code-of-conduct.md Godeps logo OWNERS plugin staging third_party WORKSPACE
BUILD.bazel cluster CONTRIBUTING.md hack Makefile OWNERS_ALIASES README.md SUPPORT.md translations
全量编译
- 全量编译对硬件有一定要求(实测i5-8400处理器,10G内存可以编译通过),若编译时出现严重卡顿和内存不足请按"Ctrl+c"退出:
- 进入目录**$GOPATH/src/k8s.io/kubernetes**,执行以下命令即可全量构建,并且构建结果只包含linux平台的:
KUBE_BUILD_PLATFORMS=linux/amd64 make
- 编译过程中,会出现CPU或内存消耗突然增加的状况,如下图,link进程消耗内存比较多,10G的内存基本耗尽:
- 过程有些耗时请耐心等待(我的机器用了11分钟完成),构建完成后控制台输出以下内容:
[root@golang kubernetes]# KUBE_BUILD_PLATFORMS=linux/amd64 make
+++ [0316 13:44:33] Building go targets for linux/amd64:
./vendor/k8s.io/code-generator/cmd/deepcopy-gen
+++ [0316 13:44:44] Building go targets for linux/amd64:
./vendor/k8s.io/code-generator/cmd/defaulter-gen
+++ [0316 13:44:48] Building go targets for linux/amd64:
./vendor/k8s.io/code-generator/cmd/conversion-gen
+++ [0316 13:44:53] Building go targets for linux/amd64:
./vendor/k8s.io/kube-openapi/cmd/openapi-gen
2019/03/16 13:45:00 Code for OpenAPI definitions generated
+++ [0316 13:45:00] Building go targets for linux/amd64:
./vendor/github.com/jteeuwen/go-bindata/go-bindata
+++ [0316 13:45:02] 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
cmd/kube-scheduler
vendor/k8s.io/apiextensions-apiserver
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
- 在$GOPATH/src/k8s.io/kubernetes/_output目录下可以见到构建结果:
[root@golang _output]# ls
bin local violations.report
[root@golang _output]# cd bin
[root@golang bin]# ls
apiextensions-apiserver deepcopy-gen e2e.test genman ginkgo hyperkube kube-controller-manager kubemark linkcheck
cloud-controller-manager defaulter-gen gendocs genswaggertypedocs go2make kubeadm kubectl kube-proxy mounter
conversion-gen e2e_node.test genkubedocs genyaml go-bindata kube-apiserver kubelet kube-scheduler openapi-gen
- 以上就是全量编译构建的过程,得到的结果只适用于linux平台;
修改源码再编译
- 接下来修改源码,再运行编译的结果试试:
- 本次要修改的是kubectl命令的源码,在启动kubectl命令的时候在控制台打印一段内容;
- 清除前面的构建结果,进入目录$GOPATH/src/k8s.io/kubernetes,执行命令make clean:
[root@golang kubernetes]# make clean
+++ [0316 13:12:24] Verifying Prerequisites....
+++ [0316 13:12:24] Removing _output directory
Removing pkg/generated/openapi/zz_generated.openapi.go ..
Removing test/e2e/generated/bindata.go ..
- kubectl命令对应的源码文件:$GOPATH/src/k8s.io/kubernetes/cmd/kubectl/kubectl.go,用vim打开此文件,增加init方法:
func init() {
fmt.Println("你好,欢迎使用kubectl!")
}
- 修改后的效果如下图所示:
- 在目录**$GOPATH/src/k8s.io/kubernetes**执行以下命令,就会构建linux平台下的kubectl文件:
KUBE_BUILD_PLATFORMS=linux/amd64 make WHAT=cmd/kubectl
- 构建完成后控制台输出如下:
[root@golang kubernetes]# KUBE_BUILD_PLATFORMS=linux/amd64 make WHAT=cmd/kubectl
+++ [0316 11:49:25] Building go targets for linux/amd64:
./vendor/k8s.io/code-generator/cmd/deepcopy-gen
+++ [0316 11:49:30] Building go targets for linux/amd64:
./vendor/k8s.io/code-generator/cmd/defaulter-gen
+++ [0316 11:49:34] Building go targets for linux/amd64:
./vendor/k8s.io/code-generator/cmd/conversion-gen
+++ [0316 11:49:39] Building go targets for linux/amd64:
./vendor/k8s.io/kube-openapi/cmd/openapi-gen
2019/03/16 11:49:44 Code for OpenAPI definitions generated
+++ [0316 11:49:45] Building go targets for linux/amd64:
./vendor/github.com/jteeuwen/go-bindata/go-bindata
+++ [0316 11:49:46] Building go targets for linux/amd64:
cmd/kubectl
- 在目录 $GOPATH/src/k8s.io/kubernetes/_output/bin ,可以见到最新构建的kubectl文件,执行命令 $GOPATH/src/k8s.io/kubernetes/_output/bin/kubectl ,可见以下内容:
[root@golang ~]# $GOPATH/src/k8s.io/kubernetes/_output/bin/kubectl
你好,欢迎使用kubectl!
kubectl controls the Kubernetes cluster manager.
Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/
Basic Commands (Beginner):
...
-
可见之前修改的代码已经生效;
-
至此,Kubernetes1.13版本源码下载和编译我们都实战过一遍了,后面的文章我们一起继续学习和实战Kubernetes;