下载kubernetes SDK源代码
git clone https://github.com/kubernetes/client-go.git kubernet-client
,然后cd kubernet-client/examples/in-cluster-client-configuration/
和类型go run main.go
,有错误
➜ in-cluster-client-configuration git:(master) go run main.go
# net
/usr/local/Cellar/go/1.18.2/libexec/src/net/dial.go:351:11: d.DialContext undefined (type *Dialer has no field or method DialContext)
/usr/local/Cellar/go/1.18.2/libexec/src/net/dial.go:372:18: invalid character U+2013 '–' in identifier
/usr/local/Cellar/go/1.18.2/libexec/src/net/lookup_unix.go:70:14: d.DialContext undefined (type Dialer has no field or method DialContext)
➜ in-cluster-client-configuration git:(master) pwd
go_sourcecode/kubernet-client/examples/in-cluster-client-configuration
go版本
➜ in-cluster-client-configuration git:(master) go version
go version go1.18.2 darwin/amd64
怎么了?
一步一步的过程已经写在markdown文件(https://github.com/kubernetes/client-go/tree/master/examples/in-cluster-client-configuration)中,为什么您不遵循它呢?
由于这是一个集群内示例,因此必须从kubernetes集群内部运行。go run main.go
不起作用。要运行此命令,
- 生成二进制文件:
go build -o ./app .
- 建立一个映像:
docker build -t YOUR_DOCKER_ID/controller:in-cluster .
- 将它推送到docker hub:
docker push IMAGE
或者将这个映像加载到集群中。对于类簇:kind load docker-image IMAGE
- 然后运行
kubectl run --rm -i demo --image=IMAGE
kubectl create clusterrolebinding default-view --clusterrole=view --serviceaccount=default:default