如何使用客户端获取服务下的pod-去kubernetes的客户端库?

时间:2017-01-09 09:49:59

标签: kubernetes

任何能告诉我如何使用客户端获取服务的pod的人都去kubernetes的客户端库?

感谢

2 个答案:

答案 0 :(得分:4)

我找到了答案:

                services, err := clientset.Core().Services(name).List(api.ListOptions{})
                if err != nil {
                    log.Errorf("Get service from kubernetes cluster error:%v", err)
                    return
                }

                for _, service := range services.Items {
                    if name == "default" && service.GetName() == "kubernetes" {
                        continue
                    }
                    log.Infoln("namespace", name, "serviceName:", service.GetName(), "serviceKind:", service.Kind, "serviceLabels:", service.GetLabels(), service.Spec.Ports, "serviceSelector:", service.Spec.Selector)

                    // labels.Parser
                    set := labels.Set(service.Spec.Selector)

                    if pods, err := clientset.Core().Pods(name).List(api.ListOptions{LabelSelector: set.AsSelector()}); err != nil {
                        log.Errorf("List Pods of service[%s] error:%v", service.GetName(), err)
                    } else {
                        for _, v := range pods.Items {
                            log.Infoln(v.GetName(), v.Spec.NodeName, v.Spec.Containers)
                        }
                    }
                }

答案 1 :(得分:1)

据我所知,我发现这个公认的答案有些欠缺。此代码在1.10下运行。在此示例中,我的svc部署均基于其所面向的工件具有受控名称,而pod也会利用该名称。请注意,我是一名学习Go语言的Java程序员,所以对于一些围棋爱好者来说,OO可能太多了。

package main

import (
    "os"
    "log"
    "path/filepath"
    "k8s.io/client-go/tools/clientcmd"
    "k8s.io/client-go/kubernetes"
    typev1 "k8s.io/client-go/kubernetes/typed/core/v1"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    corev1 "k8s.io/api/core/v1"
    "fmt"
    "strings"
    "errors"
    "k8s.io/apimachinery/pkg/labels"
)

func main(){
    kubeconfig := filepath.Join(
        os.Getenv("HOME"), ".kube", "config",
    )
    namespace:="FOO"
    k8sClient, err:= getClient(kubeconfig)
    if err!=nil{
        fmt.Fprintf(os.Stderr, "error: %v\n", err)
        os.Exit(1)
    }

    svc, err:=getServiceForDeployment("APP_NAME", namespace, k8sClient)
    if err!=nil{
        fmt.Fprintf(os.Stderr, "error: %v\n", err)
        os.Exit(2)
    }

    pods, err:=getPodsForSvc(svc, namespace, k8sClient)
    if err!=nil{
        fmt.Fprintf(os.Stderr, "error: %v\n", err)
        os.Exit(2)
    }

}

func getClient(configLocation string) (typev1.CoreV1Interface, error){
    kubeconfig := filepath.Clean(configLocation)
    config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
    if err != nil {
        log.Fatal(err)
    }
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        return nil, err
    }
    return clientset.CoreV1(), nil
}

func getServiceForDeployment(deployment string, namespace string, k8sClient typev1.CoreV1Interface) (*corev1.Service, error){
    listOptions := metav1.ListOptions{}
    svcs, err := k8sClient.Services(namespace).List(listOptions)
    if err != nil{
        log.Fatal(err)
    }
    for _, svc:=range svcs.Items{
        if strings.Contains(svc.Name, deployment){
            fmt.Fprintf(os.Stdout, "service name: %v\n", svc.Name)
            return &svc, nil
        }
    }
    return nil, errors.New("cannot find service for deployment")
}

func getPodsForSvc(svc *corev1.Service, namespace string, k8sClient typev1.CoreV1Interface) (*corev1.PodList, error){
    set := labels.Set(svc.Spec.Selector)
    listOptions:= metav1.ListOptions{LabelSelector: set.AsSelector().String()}
    pods, err:=  k8sClient.Pods(namespace).List(listOptions)
    for _,pod:= range pods.Items{
        fmt.Fprintf(os.Stdout, "pod name: %v\n", pod.Name)
    }
    return pods, err
}