如何使用Go客户端访问我的Kubernetes中的Pod?

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

How to access my pod in k8s using go-client

问题

我正在使用go-client来访问我的环境中的k8s资源。有一些API可以获取/列出pod、命名空间等。

我如何访问当前正在运行的pod?

英文:

I am using go-client to access k8s resources in my environment. There are APIs to get/list pods, namespaces, etc.

How do I access the pod that I am currently running on?

答案1

得分: 1

您可以使用环境变量将Pod信息传递给容器,具体方法请参考Kubernetes官方文档中的"Expose Pod Information to Containers Through Environment Variables"部分,使用Pod字段作为环境变量的值。

以下是示例代码:

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: dapi-envars-fieldref
  5. spec:
  6. containers:
  7. - name: test-container
  8. ...
  9. ...
  10. env:
  11. - name: MY_NODE_NAME
  12. valueFrom:
  13. fieldRef:
  14. fieldPath: spec.nodeName
  15. - name: MY_POD_NAME
  16. valueFrom:
  17. fieldRef:
  18. fieldPath: metadata.name
  19. - name: MY_POD_NAMESPACE
  20. valueFrom:
  21. fieldRef:
  22. fieldPath: metadata.namespace
  23. - name: MY_POD_IP
  24. valueFrom:
  25. fieldRef:
  26. fieldPath: status.podIP
  27. - name: MY_POD_SERVICE_ACCOUNT
  28. valueFrom:
  29. fieldRef:
  30. fieldPath: spec.serviceAccountName
  31. restartPolicy: Never

然后,在您的Go代码中可以通过查找这些环境变量来使用它们:

  1. log.Printf("MY_POD_NAME: %q", os.Getenv("MY_POD_NAME"))

请注意,以上代码仅为示例,您需要根据实际情况进行适当的修改和调整。

英文:

You can Expose Pod Information to Containers Through Environment Variables using pod fields:

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: dapi-envars-fieldref
  5. spec:
  6. containers:
  7. - name: test-container
  8. ...
  9. ...
  10. env:
  11. - name: MY_NODE_NAME
  12. valueFrom:
  13. fieldRef:
  14. fieldPath: spec.nodeName
  15. - name: MY_POD_NAME
  16. valueFrom:
  17. fieldRef:
  18. fieldPath: metadata.name
  19. - name: MY_POD_NAMESPACE
  20. valueFrom:
  21. fieldRef:
  22. fieldPath: metadata.namespace
  23. - name: MY_POD_IP
  24. valueFrom:
  25. fieldRef:
  26. fieldPath: status.podIP
  27. - name: MY_POD_SERVICE_ACCOUNT
  28. valueFrom:
  29. fieldRef:
  30. fieldPath: spec.serviceAccountName
  31. restartPolicy: Never

then simply look up these env vars in your Go code:

  1. log.Printf("MY_POD_NAME: %q", os.Getenv("MY_POD_NAME"))

答案2

得分: 0

Kubernetes的client-go库提供了访问Pod的API,CoreV1包提供了获取所有Pod的API,可以查看文档:https://pkg.go.dev/k8s.io/client-go/kubernetes/typed/core/v1#CoreV1Client.Pods

你可以通过命名空间、标签或查询来轻松过滤或获取Pod。

以下是一个示例代码,用于获取命名空间中的所有Pod:

  1. config, err := clientcmd.BuildConfigFromFlags("", "集群配置文件的路径")
  2. clientset, err := kubernetes.NewForConfig(config)
  3. if err != nil {
  4. panic(err.Error())
  5. }
  6. // 获取命名空间中的所有Pod
  7. pods, err := clientset.CoreV1().Pods("命名空间").List(context.TODO(), metav1.ListOptions{})
  8. if err != nil {
  9. panic(err.Error())
  10. }

以下是使用标签获取Pod的示例代码:

  1. label := fmt.Sprintf("标签名称=%s", "某个标签的值")
  2. ap1 := sm.clientset.CoreV1()
  3. pods, _ := ap1.Pods(namespace).List(context.Background(), metav1.ListOptions{LabelSelector: label})
  4. pods_num := len(pods.Items)
  5. fmt.Printf("找到以下数量的Pod:%d\n", pods_num)
  6. for i, pod := range pods.Items {
  7. fmt.Printf("[%2d] %s,阶段:%s,创建时间:%s,主机IP:%s\n", i, pod.GetName(), string(pod.Status.Phase), pod.GetCreationTimestamp(), string(pod.Status.HostIP))
  8. }
英文:

The Kubernetes client-go supplies API to access pods, and the CoreV1 package gives an API to get All Pods see the documentation
https://pkg.go.dev/k8s.io/client-go/kubernetes/typed/core/v1#CoreV1Client.Pods

You can easily filter or fetch Pods by Namespace, Label or Query,

See a sample code to get all pods in Namespace.

  1. config, err := clientcmd.BuildConfigFromFlags("", "PATH to cluster config yaml")
  2. clientset, err := kubernetes.NewForConfig(config)
  3. if err != nil {
  4. panic(err.Error())
  5. }
  6. // Get all pods in namespace
  7. pods, err := clientset.CoreV1().Pods("NANESPACE").List(context.TODO(),
  8. metav1.ListOptions{})
  9. if err != nil {
  10. panic(err.Error())
  11. }

See the Sample code for getting pods using a label:

  1. labal := fmt.Sprintf("LABLE-NAME=%s", "SOME LABEL VALUE")
  2. ap1 := sm.clientset.CoreV1()
  3. pods, _ := ap1.Pods(namespace).List(context.Background(),
  4. metav1.ListOptions{LabelSelector: labal})
  5. pods_num := len(pods.Items)
  6. fmt.Printf("Found the following number of Pods: %d\n", pods_num)
  7. for i, pod := range pods.Items {
  8. fmt.Printf("[%2d] %s, Phase: %s, Created: %s, HostIP: %s\n", i,
  9. pod.GetName(), string(pod.Status.Phase),
  10. pod.GetCreationTimestamp(),
  11. string(pod.Status.HostIP))
  12. }

huangapple
  • 本文由 发表于 2022年1月8日 00:50:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/70624571.html
匿名

发表评论

匿名网友

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

确定