赞
踩
在 Mac OS 平台编译 kubelet 遇到如下错误:
# make WHAT=cmd/kubelet KUBE_BUILD_PLATFORMS=linux/amd64
+++ [0222 14:39:32] Building go targets for linux/amd64
k8s.io/kubernetes/cmd/kubelet (non-static)
# runtime/cgo
cgo: C compiler "x86_64-linux-gnu-gcc" not found: exec: "x86_64-linux-gnu-gcc": executable file not found in $PATH
!!! [0222 14:39:33] Call tree:
!!! [0222 14:39:33] 1: /Users/qiushida/workspace/gopath/src/k8s.io/kubernetes/hack/lib/golang.sh:773 kube::golang::build_some_binaries(...)
!!! [0222 14:39:33] 2: /Users/qiushida/workspace/gopath/src/k8s.io/kubernetes/hack/lib/golang.sh:943 kube::golang::build_binaries_for_platform(...)
!!! [0222 14:39:33] 3: hack/make-rules/build.sh:27 kube::golang::build_binaries(...)
!!! [0222 14:39:33] Call tree:
!!! [0222 14:39:33] 1: hack/make-rules/build.sh:27 kube::golang::build_binaries(...)
!!! [0222 14:39:33] Call tree:
!!! [0222 14:39:33] 1: hack/make-rules/build.sh:27 kube::golang::build_binaries(...)
make: *** [all] Error 1
提示,找不到 CGO 依赖的 x86_64-linux-gnu-gcc
交叉编译器。
使用如下方法,安装交叉编译环境:
brew install filosottile/musl-cross/musl-cross
ln -sf /usr/local/Cellar/musl-cross/0.9.9_1/libexec/bin/x86_64-linux-musl-gcc /usr/local/bin/x86_64-linux-gnu-gcc
重新编译 kubelet,顺利完成
# make WHAT=cmd/kubelet KUBE_BUILD_PLATFORMS=linux/amd64
+++ [0222 14:45:37] Building go targets for linux/amd64
k8s.io/kubernetes/cmd/kubelet (non-static)
注意,启用 CGO 时,采用了非静态编译,依赖操作系统安装的动态链接库才能运行。
例如,上述命令编译出的二进制程序,依赖 /lib/ld-musl-x86_64.so.1 库,如果运行的目标机器未安装库文件,将无法执行 kubelet 程序。
# file ./_output/local/bin/linux/amd64/kubelet
./_output/local/bin/linux/amd64/kubelet: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-x86_64.so.1, Go BuildID=hVloIGVsz3dNrPzeiL28/bnCwn-OrPQ3iSXYxoQvR/q6jIKm-vPYmuxE673BwN/RtNGbusXOw4wjg4LtX5u, stripped
Ubuntu 下通过如下命令安装 musl 库文件:
apt-get install musl-dev
另外一种交叉编译方式是直接使用容器交叉编译环境,使用方式如下:
# shidaqiu/kube-cross:v1.27.0-go1.20.1-bullseye.0 镜像同步自 registry.k8s.io/build-image/kube-cross:v1.27.0-go1.20.1-bullseye.0
docker pull shidaqiu/kube-cross:v1.27.0-go1.20.1-bullseye.0
docker run -it -v $GOPATH/src/k8s.io/kubernetes:/go/src/k8s.io/kubernetes shidaqiu/kube-cross:v1.27.0-go1.20.1-bullseye.0 bash
# 在容器内执行编译
root@5a0c20d98867:/go# cd src/k8s.io/kubernetes/
root@5a0c20d98867:/go/src/k8s.io/kubernetes# make WHAT=cmd/kubelet KUBE_BUILD_PLATFORMS=linux/amd64
+++ [0222 07:54:12] Building go targets for linux/amd64
k8s.io/kubernetes/cmd/kubelet (non-static)
root@5a0c20d98867:/go/src/k8s.io/kubernetes# file ./_output/local/bin/linux/amd64/kubelet
./_output/local/bin/linux/amd64/kubelet: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, Go BuildID=792IpIY6JuAQlbFCP9gQ/Tl4giLUz9QS964287ACq/q6jIKm-vPYmuxE673BwN/16Cpga3_uPWU7gip_L76, stripped
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。