How to get logs from kubernetes using golang?

前端 未结 4 1775
情话喂你
情话喂你 2020-12-17 00:59

I\'m looking for the solution of how to get logs from a pod in Kubernetes cluster using golang. I\'ve looked at \"https://github.com/kubernetes/client-go\" and \"https://god

相关标签:
4条回答
  • 2020-12-17 01:28

    Here is what we came up with eventually using client-go library:

    func getPodLogs(pod corev1.Pod) string {
        podLogOpts := corev1.PodLogOptions{}
        config, err := rest.InClusterConfig()
        if err != nil {
            return "error in getting config"
        }
        // creates the clientset
        clientset, err := kubernetes.NewForConfig(config)
        if err != nil {
            return "error in getting access to K8S"
        }
        req := clientset.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &podLogOpts)
        podLogs, err := req.Stream()
        if err != nil {
            return "error in opening stream"
        }
        defer podLogs.Close()
    
        buf := new(bytes.Buffer)
        _, err = io.Copy(buf, podLogs)
        if err != nil {
            return "error in copy information from podLogs to buf"
        }
        str := buf.String()
    
        return str
    }
    

    I hope it will help someone. Please share your thoughts or solutions of how you get logs from pods in Kubernetes.

    0 讨论(0)
  • 2020-12-17 01:31

    And if you want read stream in client-go v11.0.0+, the code is like this, feel free for create clientset by yourself:

    func GetPodLogs(namespace string, podName string, containerName string, follow bool) error {
        count := int64(100)
        podLogOptions := v1.PodLogOptions{
            Container: containerName,
            Follow:    follow,
            TailLines: &count,
        }
    
        podLogRequest := clientSet.CoreV1().
            Pods(namespace).
            GetLogs(podName, &podLogOptions)
        stream, err := podLogRequest.Stream(context.TODO())
        if err != nil {
            return err
        }
        defer stream.Close()
    
        for {
            buf := make([]byte, 2000)
            numBytes, err := stream.Read(buf)
            if numBytes == 0 {
                continue
            }
            if err == io.EOF {
                break
            }
            if err != nil {
                return err
            }
            message := string(buf[:numBytes])
            fmt.Print(message)
        }
        return nil
    }
    
    
    0 讨论(0)
  • 2020-12-17 01:32

    @Emixam23

    I believe you will find this snippet useful.

    How to get the dynamic name of a pod?

    import  metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    
        labelSelector := metav1.LabelSelector{MatchLabels: map[string]string{<LABEL_KEY>: <LABEL_VALUE>}}
        listOptions := metav1.ListOptions{
            LabelSelector: labels.Set(labelSelector.MatchLabels).String(),
        }
        pod, err := k8sClient.CoreV1().Pods(<NAMESPACE>).List(listOptions)
        podName := pod.Items[0].ObjectMeta.Name
    
    0 讨论(0)
  • 2020-12-17 01:45

    The controller-runtime client library does not yet support subresources other than /status, so you would have to use client-go as shown in the other question.

    0 讨论(0)
提交回复
热议问题