你可以使用Kubernetes Go库创建一个简单的客户端应用程序。

huangapple go评论167阅读模式
英文:

How can I create a simple client app with the Kubernetes Go library?

问题

我正在与Kubernetes Go库进行斗争。文档(至少我找到的那些)似乎与库本身不符。提供的示例由于导入问题而无法构建。我只是想做一些简单的事情:通过名称获取一个Service对象并打印一些属性(如nodePort)。我只需要一个简单的库用法示例来帮助我入门。

我可以使用RESTful API轻松完成这个任务,但感觉这样做是在重复造轮子。

英文:

I'm struggling with the Kubernetes Go library. The docs--at least the ones I found--appear out-of-date with the library itself. The example provided does not build because of issues with the imports. I'm just trying to do something simple: get a Service object by name and print some attributes (like nodePort). I just need a simple example of library usage to get me going.

I could easily do this using the RESTful API but that feels like re-inventing the wheel.

答案1

得分: 7

以下是翻译好的内容:

所以经过一些实验和来自k8s Slack频道的提示,我有了这个示例。也许有人可以用正确的导入路径更新这个示例。

package main

import (
	"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("无法连接到Kubernetes API:", err)
	}

	s, err := c.Services(api.NamespaceDefault).Get("some-service-name")
	if err != nil {
		log.Fatalln("无法获取服务:", err)
	}
	fmt.Println("名称:", s.Name)
	for p, _ := range s.Spec.Ports {
		fmt.Println("端口:", s.Spec.Ports[p].Port)
		fmt.Println("NodePort:", s.Spec.Ports[p].NodePort)
	}
}

希望对你有帮助!

英文:

So after a little experimentation and a hint from the k8s Slack channel, I have this example. Perhaps someone can update the example with a proper import path.

package main

import (
    "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

.Port) fmt.Println("NodePort:", s.Spec.Ports

.NodePort) } }

答案2

得分: 5

以下是最新的Go客户端的操作方法。

如果你在k8s集群内部:

package main

import (
  "fmt"

  "k8s.io/client-go/1.5/kubernetes"
  "k8s.io/client-go/1.5/pkg/api/v1"
  "k8s.io/client-go/1.5/rest"
)

func main()  {
    config, err := rest.InClusterConfig()
    if err != nil {
      return nil, err
    }

    c, err := kubernetes.NewForConfig(config)
    if err != nil {
      return nil, err
    }

    // 通过名称获取Pod
    pod, err := c.Pods(v1.NamespaceDefault).Get("my-pod")
    if err != nil {
        fmt.Println(err)
        return
    }
  
    // 打印创建时间
    fmt.Println(pod.GetCreationTimestamp())
}

如果你在集群外部:

package main

import (
  "fmt"

  "k8s.io/client-go/1.5/kubernetes"
  "k8s.io/client-go/1.5/pkg/api/v1"
  "k8s.io/client-go/1.5/tools/clientcmd"
)

func main()  {
    config, err := clientcmd.BuildConfigFromFlags("", "<kube-config-path>")
    if err != nil {
      return nil, err
    }

    c, err := kubernetes.NewForConfig(config)
    if err != nil {
      return nil, err
    }

    // 通过名称获取Pod
    pod, err := c.Pods(v1.NamespaceDefault).Get("my-pod")
    if err != nil {
        fmt.Println(err)
        return
    }
  
    // 打印创建时间
    fmt.Println(pod.GetCreationTimestamp())
}

我在博客文章中对此进行了更详细的说明。

英文:

Here's how to do it with the latest Go client.

If you're inside the k8s cluster:

package main

import (
  &quot;fmt&quot;

  &quot;k8s.io/client-go/1.5/kubernetes&quot;
  &quot;k8s.io/client-go/1.5/pkg/api/v1&quot;
  &quot;k8s.io/client-go/1.5/rest&quot;
)

func main()  {
    config, err = rest.InClusterConfig()
    if err != nil {
      return nil, err
    }

    c, err := kubernetes.NewForConfig(config)
    if err != nil {
      return nil, err
    }

    // Get Pod by name
    pod, err := c.Pods(v1.NamespaceDefault).Get(&quot;my-pod&quot;)
    if err != nil {
        fmt.Println(err)
        return
    }
  
    // Print its creation time
    fmt.Println(pod.GetCreationTimestamp())
}

And if you're outside of the cluster:

package main

import (
  &quot;fmt&quot;

  &quot;k8s.io/client-go/1.5/kubernetes&quot;
  &quot;k8s.io/client-go/1.5/pkg/api/v1&quot;
  &quot;k8s.io/client-go/1.5/tools/clientcmd&quot;
)

func main()  {
    config, err := clientcmd.BuildConfigFromFlags(&quot;&quot;, &lt;kube-config-path&gt;)
    if err != nil {
      return nil, err
    }

    c, err := kubernetes.NewForConfig(config)
    if err != nil {
      return nil, err
    }

    // Get Pod by name
    pod, err := c.Pods(v1.NamespaceDefault).Get(&quot;my-pod&quot;)
    if err != nil {
        fmt.Println(err)
        return
    }
  
    // Print its creation time
    fmt.Println(pod.GetCreationTimestamp())
}

I have gone into more detail on this in a blog post.

答案3

得分: 3

使用Kubernetes Go客户端,可以按照以下方式完成:

package main

import (
	"flag"
	"fmt"

	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/pkg/api/v1"
	"k8s.io/client-go/tools/clientcmd"
)

var (
	kubeconfig = flag.String("kubeconfig", "./config", "kubeconfig文件的绝对路径")
)

func main() {
	flag.Parse()
	// 使用kubeconfig中的当前上下文
	config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
	if err != nil {
		panic(err.Error())
	}
	// 创建clientset
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		panic(err.Error())
	}
	services, err := clientset.Core().Services("").List(v1.ListOptions{})
	if err != nil {
		panic(err.Error())
	}
	fmt.Printf("集群中有 %d 个服务\n", len(services.Items))

	for _, s := range services.Items {
		for p, _ := range s.Spec.Ports {
			fmt.Println("端口:", s.Spec.Ports[p].Port)
			fmt.Println("NodePort:", s.Spec.Ports[p].NodePort)
		}
	}
}

以上是使用Kubernetes Go客户端的示例代码。

英文:

With kubernetes go client, it could be done this way:

package main

import (
	&quot;flag&quot;
	&quot;fmt&quot;

	&quot;k8s.io/client-go/kubernetes&quot;
	&quot;k8s.io/client-go/pkg/api/v1&quot;
	&quot;k8s.io/client-go/tools/clientcmd&quot;
)

var (
	kubeconfig = flag.String(&quot;kubeconfig&quot;, &quot;./config&quot;, &quot;absolute path to the kubeconfig file&quot;)
)

func main() {
	flag.Parse()
	// uses the current context in kubeconfig
	config, err := clientcmd.BuildConfigFromFlags(&quot;&quot;, *kubeconfig)
	if err != nil {
		panic(err.Error())
	}
	// creates the clientset
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		panic(err.Error())
	}
	services, err := clientset.Core().Services(&quot;&quot;).List(v1.ListOptions{})
	if err != nil {
		panic(err.Error())
	}
	fmt.Printf(&quot;There are %d pods in the cluster\n&quot;, len(services.Items))

	for _, s := range services.Items {
		for p, _ := range s.Spec.Ports {
			fmt.Println(&quot;Port:&quot;, s.Spec.Ports

.Port) fmt.Println(&quot;NodePort:&quot;, s.Spec.Ports

.NodePort) } } }

huangapple
  • 本文由 发表于 2015年9月14日 05:53:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/32554893.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定