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

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

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频道的提示,我有了这个示例。也许有人可以用正确的导入路径更新这个示例。

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "github.com/kubernetes/kubernetes/pkg/api"
  6. client "github.com/kubernetes/kubernetes/pkg/client/unversioned"
  7. )
  8. func main() {
  9. config := client.Config{
  10. Host: "http://my-kube-api-server.me:8080",
  11. }
  12. c, err := client.New(&config)
  13. if err != nil {
  14. log.Fatalln("无法连接到Kubernetes API:", err)
  15. }
  16. s, err := c.Services(api.NamespaceDefault).Get("some-service-name")
  17. if err != nil {
  18. log.Fatalln("无法获取服务:", err)
  19. }
  20. fmt.Println("名称:", s.Name)
  21. for p, _ := range s.Spec.Ports {
  22. fmt.Println("端口:", s.Spec.Ports[p].Port)
  23. fmt.Println("NodePort:", s.Spec.Ports[p].NodePort)
  24. }
  25. }

希望对你有帮助!

英文:

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.

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "github.com/kubernetes/kubernetes/pkg/api"
  6. client "github.com/kubernetes/kubernetes/pkg/client/unversioned"
  7. )
  8. func main() {
  9. config := client.Config{
  10. Host: "http://my-kube-api-server.me:8080",
  11. }
  12. c, err := client.New(&config)
  13. if err != nil {
  14. log.Fatalln("Can't connect to Kubernetes API:", err)
  15. }
  16. s, err := c.Services(api.NamespaceDefault).Get("some-service-name")
  17. if err != nil {
  18. log.Fatalln("Can't get service:", err)
  19. }
  20. fmt.Println("Name:", s.Name)
  21. for p, _ := range s.Spec.Ports {
  22. fmt.Println("Port:", s.Spec.Ports

    .Port)

  23. fmt.Println("NodePort:", s.Spec.Ports

    .NodePort)

  24. }

  25. }

答案2

得分: 5

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

如果你在k8s集群内部:

  1. package main
  2. import (
  3. "fmt"
  4. "k8s.io/client-go/1.5/kubernetes"
  5. "k8s.io/client-go/1.5/pkg/api/v1"
  6. "k8s.io/client-go/1.5/rest"
  7. )
  8. func main() {
  9. config, err := rest.InClusterConfig()
  10. if err != nil {
  11. return nil, err
  12. }
  13. c, err := kubernetes.NewForConfig(config)
  14. if err != nil {
  15. return nil, err
  16. }
  17. // 通过名称获取Pod
  18. pod, err := c.Pods(v1.NamespaceDefault).Get("my-pod")
  19. if err != nil {
  20. fmt.Println(err)
  21. return
  22. }
  23. // 打印创建时间
  24. fmt.Println(pod.GetCreationTimestamp())
  25. }

如果你在集群外部:

  1. package main
  2. import (
  3. "fmt"
  4. "k8s.io/client-go/1.5/kubernetes"
  5. "k8s.io/client-go/1.5/pkg/api/v1"
  6. "k8s.io/client-go/1.5/tools/clientcmd"
  7. )
  8. func main() {
  9. config, err := clientcmd.BuildConfigFromFlags("", "<kube-config-path>")
  10. if err != nil {
  11. return nil, err
  12. }
  13. c, err := kubernetes.NewForConfig(config)
  14. if err != nil {
  15. return nil, err
  16. }
  17. // 通过名称获取Pod
  18. pod, err := c.Pods(v1.NamespaceDefault).Get("my-pod")
  19. if err != nil {
  20. fmt.Println(err)
  21. return
  22. }
  23. // 打印创建时间
  24. fmt.Println(pod.GetCreationTimestamp())
  25. }

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

英文:

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

If you're inside the k8s cluster:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;k8s.io/client-go/1.5/kubernetes&quot;
  5. &quot;k8s.io/client-go/1.5/pkg/api/v1&quot;
  6. &quot;k8s.io/client-go/1.5/rest&quot;
  7. )
  8. func main() {
  9. config, err = rest.InClusterConfig()
  10. if err != nil {
  11. return nil, err
  12. }
  13. c, err := kubernetes.NewForConfig(config)
  14. if err != nil {
  15. return nil, err
  16. }
  17. // Get Pod by name
  18. pod, err := c.Pods(v1.NamespaceDefault).Get(&quot;my-pod&quot;)
  19. if err != nil {
  20. fmt.Println(err)
  21. return
  22. }
  23. // Print its creation time
  24. fmt.Println(pod.GetCreationTimestamp())
  25. }

And if you're outside of the cluster:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;k8s.io/client-go/1.5/kubernetes&quot;
  5. &quot;k8s.io/client-go/1.5/pkg/api/v1&quot;
  6. &quot;k8s.io/client-go/1.5/tools/clientcmd&quot;
  7. )
  8. func main() {
  9. config, err := clientcmd.BuildConfigFromFlags(&quot;&quot;, &lt;kube-config-path&gt;)
  10. if err != nil {
  11. return nil, err
  12. }
  13. c, err := kubernetes.NewForConfig(config)
  14. if err != nil {
  15. return nil, err
  16. }
  17. // Get Pod by name
  18. pod, err := c.Pods(v1.NamespaceDefault).Get(&quot;my-pod&quot;)
  19. if err != nil {
  20. fmt.Println(err)
  21. return
  22. }
  23. // Print its creation time
  24. fmt.Println(pod.GetCreationTimestamp())
  25. }

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

答案3

得分: 3

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

  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "k8s.io/client-go/kubernetes"
  6. "k8s.io/client-go/pkg/api/v1"
  7. "k8s.io/client-go/tools/clientcmd"
  8. )
  9. var (
  10. kubeconfig = flag.String("kubeconfig", "./config", "kubeconfig文件的绝对路径")
  11. )
  12. func main() {
  13. flag.Parse()
  14. // 使用kubeconfig中的当前上下文
  15. config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
  16. if err != nil {
  17. panic(err.Error())
  18. }
  19. // 创建clientset
  20. clientset, err := kubernetes.NewForConfig(config)
  21. if err != nil {
  22. panic(err.Error())
  23. }
  24. services, err := clientset.Core().Services("").List(v1.ListOptions{})
  25. if err != nil {
  26. panic(err.Error())
  27. }
  28. fmt.Printf("集群中有 %d 个服务\n", len(services.Items))
  29. for _, s := range services.Items {
  30. for p, _ := range s.Spec.Ports {
  31. fmt.Println("端口:", s.Spec.Ports[p].Port)
  32. fmt.Println("NodePort:", s.Spec.Ports[p].NodePort)
  33. }
  34. }
  35. }

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

英文:

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

  1. package main
  2. import (
  3. &quot;flag&quot;
  4. &quot;fmt&quot;
  5. &quot;k8s.io/client-go/kubernetes&quot;
  6. &quot;k8s.io/client-go/pkg/api/v1&quot;
  7. &quot;k8s.io/client-go/tools/clientcmd&quot;
  8. )
  9. var (
  10. kubeconfig = flag.String(&quot;kubeconfig&quot;, &quot;./config&quot;, &quot;absolute path to the kubeconfig file&quot;)
  11. )
  12. func main() {
  13. flag.Parse()
  14. // uses the current context in kubeconfig
  15. config, err := clientcmd.BuildConfigFromFlags(&quot;&quot;, *kubeconfig)
  16. if err != nil {
  17. panic(err.Error())
  18. }
  19. // creates the clientset
  20. clientset, err := kubernetes.NewForConfig(config)
  21. if err != nil {
  22. panic(err.Error())
  23. }
  24. services, err := clientset.Core().Services(&quot;&quot;).List(v1.ListOptions{})
  25. if err != nil {
  26. panic(err.Error())
  27. }
  28. fmt.Printf(&quot;There are %d pods in the cluster\n&quot;, len(services.Items))
  29. for _, s := range services.Items {
  30. for p, _ := range s.Spec.Ports {
  31. fmt.Println(&quot;Port:&quot;, s.Spec.Ports

    .Port)

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

    .NodePort)

  33. }

  34. }

  35. }

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:

确定