下载源码并切换到需要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开头就已经说明
    • 注意:CPU和内存一定不要给少了,我用4核4G,虚拟机会报OOM,一晚上都没有编译完成,建议内存给到6G以上
    • 编译出来的二进制文件保存在:_output/local/bin目录下
      • 	[root@k8s-master1 kubernetes]# make clean && KUBE_BUILD_PLATFORMS=linux/amd64 make all DBG=1
        	+++ [1206 08:32:29] Verifying Prerequisites....
        	+++ [1206 08:32:31] Removing _output directory
        	Makefile.generated_files:61: .make/go-pkgdeps.mk: No such file or directory
        	+++ [1206 08:32:44] Building go targets for linux/amd64
        	    k8s.io/kubernetes/hack/make-rules/helpers/go2make (non-static)
        	+++ [1206 08:33:13] Building go targets for linux/amd64
        	    k8s.io/code-generator/cmd/prerelease-lifecycle-gen (non-static)
        	+++ [1206 08:33:24] Generating prerelease lifecycle code for 27 targets
        	+++ [1206 08:33:32] Building go targets for linux/amd64
        	    k8s.io/code-generator/cmd/deepcopy-gen (non-static)
        	+++ [1206 08:33:36] Generating deepcopy code for 242 targets
        	+++ [1206 08:33:59] Building go targets for linux/amd64
        	    k8s.io/code-generator/cmd/defaulter-gen (non-static)
        	+++ [1206 08:34:02] Generating defaulter code for 95 targets
        	+++ [1206 08:34:29] Building go targets for linux/amd64
        	    k8s.io/code-generator/cmd/conversion-gen (non-static)
        	+++ [1206 08:34:32] Generating conversion code for 132 targets
        	+++ [1206 08:35:25] Building go targets for linux/amd64
        	    k8s.io/kube-openapi/cmd/openapi-gen (non-static)
        	+++ [1206 08:35:45] Generating openapi code for KUBE
        	+++ [1206 08:36:53] Generating openapi code for AGGREGATOR
        	+++ [1206 08:36:58] Generating openapi code for APIEXTENSIONS
        	+++ [1206 08:37:03] Generating openapi code for CODEGEN
        	+++ [1206 08:37:07] Generating openapi code for SAMPLEAPISERVER
        	+++ [1206 08:37:13] 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)
        	[root@k8s-master1 kubernetes]#
        	[root@k8s-master1 kubernetes]#
        	[root@k8s-master1 kubernetes]# date
        	Tue Dec  6 09:25:48 CST 2022
        	[root@k8s-master1 kubernetes]#
        	[root@k8s-master1 kubernetes]# ls _output/bin/
        	apiextensions-apiserver  defaulter-gen  gendocs      genswaggertypedocs  go2make    kube-aggregator          kubectl          kube-log-runner  kube-scheduler  openapi-gen
        	conversion-gen           e2e_node.test  genkubedocs  genyaml             go-runner  kube-apiserver           kubectl-convert  kubemark         linkcheck       prerelease-lifecycle-gen
        	deepcopy-gen             e2e.test       genman       ginkgo              kubeadm    kube-controller-manager  kubelet          kube-proxy       mounter
        	[root@k8s-master1 kubernetes]# ls _output/bin/ -ll
        	total 2440440
        	-rwxr-xr-x 1 root root  83910015 Dec  6 09:04 apiextensions-apiserver
        	-rwxr-xr-x 1 root root   9906622 Dec  6 08:34 conversion-gen
        	-rwxr-xr-x 1 root root   9523027 Dec  6 08:33 deepcopy-gen
        	-rwxr-xr-x 1 root root   9511723 Dec  6 08:34 defaulter-gen
        	-rwxr-xr-x 1 root root 212991856 Dec  6 09:04 e2e_node.test
        	-rwxr-xr-x 1 root root 252555552 Dec  6 09:04 e2e.test
        	-rwxr-xr-x 1 root root  83432576 Dec  6 09:04 gendocs
        	-rwxr-xr-x 1 root root 287383472 Dec  6 09:04 genkubedocs
        	-rwxr-xr-x 1 root root 298369808 Dec  6 09:04 genman
        	-rwxr-xr-x 1 root root   6521108 Dec  6 09:04 genswaggertypedocs
        	-rwxr-xr-x 1 root root  83359024 Dec  6 09:04 genyaml
        	-rwxr-xr-x 1 root root  14480167 Dec  6 09:04 ginkgo
        	-rwxr-xr-x 1 root root   3713576 Dec  6 08:32 go2make
        	-rwxr-xr-x 1 root root   2700034 Dec  6 09:04 go-runner
        	-rwxr-xr-x 1 root root  67310531 Dec  6 09:04 kubeadm
        	-rwxr-xr-x 1 root root  75157629 Dec  6 09:04 kube-aggregator
        	-rwxr-xr-x 1 root root 179023572 Dec  6 09:03 kube-apiserver
        	-rwxr-xr-x 1 root root 165703469 Dec  6 09:04 kube-controller-manager
        	-rwxr-xr-x 1 root root  68343839 Dec  6 09:04 kubectl
        	-rwxr-xr-x 1 root root  80221984 Dec  6 09:04 kubectl-convert
        	-rwxr-xr-x 1 root root 164584160 Dec  6 09:04 kubelet
        	-rwxr-xr-x 1 root root   2317348 Dec  6 09:04 kube-log-runner
        	-rwxr-xr-x 1 root root 164343010 Dec  6 09:04 kubemark
        	-rwxr-xr-x 1 root root  63235431 Dec  6 09:03 kube-proxy
        	-rwxr-xr-x 1 root root  72264982 Dec  6 09:04 kube-scheduler
        	-rwxr-xr-x 1 root root   7612375 Dec  6 09:04 linkcheck
        	-rwxr-xr-x 1 root root   2198973 Dec  6 09:04 mounter
        	-rwxr-xr-x 1 root root  18808606 Dec  6 08:35 openapi-gen
        	-rwxr-xr-x 1 root root   9470758 Dec  6 08:33 prerelease-lifecycle-gen
        	[root@k8s-master1 kubernetes]#
        	[root@k8s-master1 kubernetes]# ./_output/bin/kube-apiserver --version
        	Kubernetes v1.24.0-beta.0.2462+df6de7f1e4738e
        	[root@k8s-master1 kubernetes]#
        
        
    • 验证编译出来的二进制文件可以使用以下两种命令验证是否能够debug
      • 1、objdump --syms _output/bin/kubelet
        • xsss
        • 在这里插入图片描述
      • 2、file _output/bin/kubelet
        • 在这里插入图片描述
      • 3、dlv exec _output/bin/kubelet
        • 在这里插入图片描述
    • 如果需要编译特定组件,那么需要执行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
    在这里插入图片描述

单节点的K8S搭建可以参考这篇文章:Kubernetes v1.25 搭建单节点集群用于Debug K8S源码

[root@k8s-master1 ~]# kubectl get componentstatus
Warning: v1 ComponentStatus is deprecated in v1.19+
NAME                 STATUS    MESSAGE                         ERROR
etcd-0               Healthy   {"health":"true","reason":""}
controller-manager   Healthy   ok
scheduler            Healthy   ok
[root@k8s-master1 ~]#
[root@k8s-master1 kubernetes]# kubectl get pods -A
NAMESPACE     NAME                                       READY   STATUS    RESTARTS      AGE
default       nginx-deployment-965685897-4zr9v           1/1     Running   2 (11m ago)   24h
default       nginx-deployment-965685897-jf675           1/1     Running   2 (11m ago)   24h
default       nginx-deployment-965685897-xzxh5           1/1     Running   2 (11m ago)   24h
kube-system   calico-kube-controllers-58dbc876ff-n57bg   1/1     Running   4 (11m ago)   24h
kube-system   calico-node-tpj5t                          1/1     Running   2 (11m ago)   24h
kube-system   coredns-78cdc77856-98xdq                   1/1     Running   3 (11m ago)   24h
kube-system   metrics-server-6bbcb9f574-5w4xz            1/1     Running   4 (11m ago)   24h
[root@k8s-master1 kubernetes]#
[root@k8s-master1 kubernetes]#
[root@k8s-master1 kubernetes]#
[root@k8s-master1 kubernetes]# ps -ef|grep "/usr/local/bin/kube*"
root        895      1  2 12:37 ?        00:00:22 /usr/local/bin/kube-controller-manager --v=4 --bind-address=127.0.0.1 --root-ca-file=/etc/kubernetes/pki/ca.pem --cluster-signing-cert-file=/etc/kubernetes/pki/ca.pem --cluster-signing-key-file=/etc/kubernetes/pki/ca-key.pem --service-account-private-key-file=/etc/kubernetes/pki/sa.key --kubeconfig=/etc/kubernetes/kube-controller-manager.kubeconfig --leader-elect=true --use-service-account-credentials=true --node-monitor-grace-period=40s --node-monitor-period=5s --pod-eviction-timeout=2m0s --controllers=*,bootstrapsigner,tokencleaner --allocate-node-cidrs=true --service-cluster-ip-range=10.96.0.0/12 --cluster-cidr=172.16.0.0/12 --node-cidr-mask-size-ipv4=24 --requestheader-client-ca-file=/etc/kubernetes/pki/front-proxy-ca.pem
root        899      1  0 12:37 ?        00:00:03 /usr/local/bin/kube-scheduler --v=4 --bind-address=127.0.0.1 --leader-elect=true --kubeconfig=/etc/kubernetes/kube-scheduler.kubeconfig
root       1202      1  0 12:37 ?        00:00:02 /usr/local/bin/kube-proxy --config=/etc/kubernetes/kube-proxy.yaml --v=8
root       1365      1  2 12:37 ?        00:00:21 /usr/local/bin/kubelet --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.kubeconfig --kubeconfig=/etc/kubernetes/kubelet.kubeconfig --config=/etc/kubernetes/kubelet-conf.yml --container-runtime-endpoint=unix:///run/containerd/containerd.sock --node-labels=node.kubernetes.io/node= --v=8
root       1533      1  4 12:37 ?        00:00:42 /usr/local/bin/kube-apiserver --v=4 --allow-privileged=true --bind-address=0.0.0.0 --secure-port=6443 --advertise-address=192.168.11.71 --service-cluster-ip-range=10.96.0.0/12 --service-node-port-range=30000-50000 --etcd-servers=https://192.168.11.71:2379 --etcd-cafile=/etc/etcd/ssl/etcd-ca.pem --etcd-certfile=/etc/etcd/ssl/etcd.pem --etcd-keyfile=/etc/etcd/ssl/etcd-key.pem --client-ca-file=/etc/kubernetes/pki/ca.pem --tls-cert-file=/etc/kubernetes/pki/kube-apiserver.pem --tls-private-key-file=/etc/kubernetes/pki/kube-apiserver-key.pem --kubelet-client-certificate=/etc/kubernetes/pki/kube-apiserver.pem --kubelet-client-key=/etc/kubernetes/pki/kube-apiserver-key.pem --service-account-key-file=/etc/kubernetes/pki/sa.pub --service-account-signing-key-file=/etc/kubernetes/pki/sa.key --service-account-issuer=https://kubernetes.default.svc.cluster.local --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname --enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,DefaultTolerationSeconds,NodeRestriction,ResourceQuota --authorization-mode=Node,RBAC --enable-bootstrap-token-auth=true --requestheader-client-ca-file=/etc/kubernetes/pki/front-proxy-ca.pem --proxy-client-cert-file=/etc/kubernetes/pki/front-proxy-client.pem --proxy-client-key-file=/etc/kubernetes/pki/front-proxy-client-key.pem --requestheader-allowed-names=aggregator --requestheader-group-headers=X-Remote-Group --requestheader-extra-headers-prefix=X-Remote-Extra- --requestheader-username-headers=X-Remote-User --token-auth-file=/etc/kubernetes/token.csv --enable-aggregator-routing=true
root     101868   1661  0 12:51 pts/0    00:00:00 grep --color=auto /usr/local/bin/kube*
[root@k8s-master1 kubernetes]#
[root@k8s-master1 kubernetes]# systemctl stop kubelet.service
[root@k8s-master1 kubernetes]#

[root@k8s-master1 kubernetes]#
[root@k8s-master1 kubernetes]# dlv --listen=:12345 --headless=true --api-version=2 --accept-multiclient exec _output/bin/kubelet --  --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.kubeconfig --kubeconfig=/etc/kubernetes/kubelet.kubeconfig --config=/etc/kubernetes/kubelet-conf.yml --container-runtime-endpoint=unix:///run/containerd/containerd.sock --node-labels=node.kubernetes.io/node= --v=8
API server listening at: [::]:12345
2022-12-06T12:54:38+08:00 warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)


在这里插入图片描述

Windows平台的用户记得修改IDEA Go语言的配置,否则源码中区分平台的代码将无法显示
Kubernetes v1.25 搭建单节点集群用于Debug K8S源码

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐