下载源码并切换到需要debug
的分支
git clone git@github.com:kubernetes/kubernetes.git
git checkout -b v1.25 remotes/origin/release-1.25
cd kubernetes
编译源码
-
执行:
make clean && KUBE_BUILD_PLATFORMS=linux/amd64 make all DBG=1
会编译所有的组件- 网上参考了很多文章都是,需要修改
hack/lib/golang.sh
,实际上加上DBG=1
这个参数,并不需要。Makefile
开头就已经说明 - 编译出来的二进制文件保存在:
_output/local/bin
目录下-
[root@k8s-master1 kubernetes]# make clean && KUBE_BUILD_PLATFORMS=linux/amd64 make all DBG=1 +++ [1205 14:29:27] Verifying Prerequisites.... Makefile.generated_files:61: .make/go-pkgdeps.mk: No such file or directory +++ [1205 14:29:30] Building go targets for linux/amd64 k8s.io/kubernetes/hack/make-rules/helpers/go2make (non-static) +++ [1205 14:29:45] Building go targets for linux/amd64 k8s.io/code-generator/cmd/prerelease-lifecycle-gen (non-static) +++ [1205 14:29:51] Generating prerelease lifecycle code for 27 targets +++ [1205 14:29:55] Building go targets for linux/amd64 k8s.io/code-generator/cmd/deepcopy-gen (non-static) +++ [1205 14:29:57] Generating deepcopy code for 242 targets +++ [1205 14:30:12] Building go targets for linux/amd64 k8s.io/code-generator/cmd/defaulter-gen (non-static) +++ [1205 14:30:14] Generating defaulter code for 95 targets +++ [1205 14:30:34] Building go targets for linux/amd64 k8s.io/code-generator/cmd/conversion-gen (non-static) +++ [1205 14:30:35] Generating conversion code for 132 targets +++ [1205 14:31:12] Building go targets for linux/amd64 k8s.io/kube-openapi/cmd/openapi-gen (non-static) +++ [1205 14:31:29] Generating openapi code for KUBE +++ [1205 14:32:27] Generating openapi code for AGGREGATOR +++ [1205 14:32:30] Generating openapi code for APIEXTENSIONS +++ [1205 14:32:33] Generating openapi code for CODEGEN +++ [1205 14:32:36] Generating openapi code for SAMPLEAPISERVER +++ [1205 14:32:40] Building go targets for linux/amd64 k8s.io/kubernetes/cmd/kube-proxy (static) k8s.io/kubernetes/cmd/kube-apiserver (static) k8s.io/kubernetes/cmd/kube-controller-manager (static) k8s.io/kubernetes/cmd/kubelet (non-static) k8s.io/kubernetes/cmd/kubeadm (static) k8s.io/kubernetes/cmd/kube-scheduler (static) k8s.io/component-base/logs/kube-log-runner (static) k8s.io/kube-aggregator (non-static) k8s.io/apiextensions-apiserver (non-static) k8s.io/kubernetes/cluster/gce/gci/mounter (non-static) k8s.io/kubernetes/cmd/kubectl (static) k8s.io/kubernetes/cmd/kubectl-convert (non-static) k8s.io/kubernetes/cmd/gendocs (non-static) k8s.io/kubernetes/cmd/genkubedocs (non-static) k8s.io/kubernetes/cmd/genman (non-static) k8s.io/kubernetes/cmd/genyaml (non-static) k8s.io/kubernetes/cmd/genswaggertypedocs (non-static) k8s.io/kubernetes/cmd/linkcheck (non-static) github.com/onsi/ginkgo/v2/ginkgo (non-static) k8s.io/kubernetes/test/e2e/e2e.test (test) k8s.io/kubernetes/test/conformance/image/go-runner (non-static) k8s.io/kubernetes/cmd/kubemark (static) github.com/onsi/ginkgo/v2/ginkgo (non-static) k8s.io/kubernetes/test/e2e_node/e2e_node.test (test)
-
- 验证编译出来的二进制文件可以使用以下两种命令验证是否能够
debug
:- 1、
objdump --syms _output/bin/kubelet
- 2、
file _output/bin/kubelet
- 3、
dlv exec _output/bin/kubelet
- 1、
- 如果需要编译特定组件,那么需要执行
make clean && KUBE_BUILD_PLATFORMS=linux/amd64 make WHAT=cmd/<component>
,以下是几个重要组件的单独编译命令make clean && KUBE_BUILD_PLATFORMS=linux/amd64 make all DBG=1 WHAT=cmd/kube-apiserver
make clean && KUBE_BUILD_PLATFORMS=linux/amd64 make all DBG=1 WHAT=cmd/kube-controller-manager
make clean && KUBE_BUILD_PLATFORMS=linux/amd64 make all DBG=1 WHAT=cmd/kube-proxy
make clean && KUBE_BUILD_PLATFORMS=linux/amd64 make all DBG=1 WHAT=cmd/kube-scheduler
make clean && KUBE_BUILD_PLATFORMS=linux/amd64 make all DBG=1 WHAT=cmd/kubeadm
make clean && KUBE_BUILD_PLATFORMS=linux/amd64 make all DBG=1 WHAT=cmd/kubectl
make clean && KUBE_BUILD_PLATFORMS=linux/amd64 make all DBG=1 WHAT=cmd/kubelet
- 本地运行K8S,执行
./heak/local-up-cluster.up
- 网上参考了很多文章都是,需要修改
-
delve启动代码
dlv --listen=:12345 --headless=true --api-version=2 --accept-multiclient exec /usr/local/bin/kubelet -- <kubelet的启动参数>
-
IDEA连接,开始debug