如何使用 Kubernetes Go 库创建一个简单的客户端应用程序?

我正在为 Kubernetes Go 库苦苦挣扎。文档——至少是我发现的那些——与库本身看起来已经过时了。由于导入问题,提供的示例无法构建。我只是想做一些简单的事情:按名称获取服务对象并打印一些属性(如 nodePort)。我只需要一个简单的库使用示例就可以了。

我可以使用 RESTful API 轻松地做到这一点,但这感觉就像是在重新发明轮子。


慕后森
浏览 220回答 3
3回答

暮色呼如

所以经过一些实验和来自 k8s Slack 频道的提示,我有了这个例子。也许有人可以使用正确的导入路径更新示例。package mainimport (    "fmt"    "log"    "github.com/kubernetes/kubernetes/pkg/api"    client "github.com/kubernetes/kubernetes/pkg/client/unversioned")func main() {    config := client.Config{        Host: "http://my-kube-api-server.me:8080",    }    c, err := client.New(&config)    if err != nil {        log.Fatalln("Can't connect to Kubernetes API:", err)    }    s, err := c.Services(api.NamespaceDefault).Get("some-service-name")    if err != nil {        log.Fatalln("Can't get service:", err)    }    fmt.Println("Name:", s.Name)    for p, _ := range s.Spec.Ports {        fmt.Println("Port:", s.Spec.Ports[p].Port)        fmt.Println("NodePort:", s.Spec.Ports[p].NodePort)    }}

大话西游666

以下是如何使用最新的 Go 客户端执行此操作。如果您在 k8s 集群中:package mainimport (&nbsp; "fmt"&nbsp; "k8s.io/client-go/1.5/kubernetes"&nbsp; "k8s.io/client-go/1.5/pkg/api/v1"&nbsp; "k8s.io/client-go/1.5/rest")func main()&nbsp; {&nbsp; &nbsp; config, err = rest.InClusterConfig()&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; return nil, err&nbsp; &nbsp; }&nbsp; &nbsp; c, err := kubernetes.NewForConfig(config)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; return nil, err&nbsp; &nbsp; }&nbsp; &nbsp; // Get Pod by name&nbsp; &nbsp; pod, err := c.Pods(v1.NamespaceDefault).Get("my-pod")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; // Print its creation time&nbsp; &nbsp; fmt.Println(pod.GetCreationTimestamp())}如果您在集群之外:package mainimport (&nbsp; "fmt"&nbsp; "k8s.io/client-go/1.5/kubernetes"&nbsp; "k8s.io/client-go/1.5/pkg/api/v1"&nbsp; "k8s.io/client-go/1.5/tools/clientcmd")func main()&nbsp; {&nbsp; &nbsp; config, err := clientcmd.BuildConfigFromFlags("", <kube-config-path>)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; return nil, err&nbsp; &nbsp; }&nbsp; &nbsp; c, err := kubernetes.NewForConfig(config)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; return nil, err&nbsp; &nbsp; }&nbsp; &nbsp; // Get Pod by name&nbsp; &nbsp; pod, err := c.Pods(v1.NamespaceDefault).Get("my-pod")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; // Print its creation time&nbsp; &nbsp; fmt.Println(pod.GetCreationTimestamp())}我在一篇博文中对此进行了更详细的介绍。

呼啦一阵风

使用 kubernetes go client,可以这样做:package mainimport (&nbsp; &nbsp; "flag"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "k8s.io/client-go/kubernetes"&nbsp; &nbsp; "k8s.io/client-go/pkg/api/v1"&nbsp; &nbsp; "k8s.io/client-go/tools/clientcmd")var (&nbsp; &nbsp; kubeconfig = flag.String("kubeconfig", "./config", "absolute path to the kubeconfig file"))func main() {&nbsp; &nbsp; flag.Parse()&nbsp; &nbsp; // uses the current context in kubeconfig&nbsp; &nbsp; config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err.Error())&nbsp; &nbsp; }&nbsp; &nbsp; // creates the clientset&nbsp; &nbsp; clientset, err := kubernetes.NewForConfig(config)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err.Error())&nbsp; &nbsp; }&nbsp; &nbsp; services, err := clientset.Core().Services("").List(v1.ListOptions{})&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err.Error())&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Printf("There are %d pods in the cluster\n", len(services.Items))&nbsp; &nbsp; for _, s := range services.Items {&nbsp; &nbsp; &nbsp; &nbsp; for p, _ := range s.Spec.Ports {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Port:", s.Spec.Ports[p].Port)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("NodePort:", s.Spec.Ports[p].NodePort)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go