简体   繁体   English

使用 go 客户端在 k8s 的 pod 中执行的示例

[英]example of exec in k8s's pod by using go client

I want to use k8s go client to exec command in a pod.我想使用 k8s go 客户端在 pod 中执行命令。 However I cannot find any example about this.但是我找不到任何关于这个的例子。 So I read kubectl exec source code, and write code as below.所以我阅读kubectl exec源代码,并编写了如下代码。 And err = exec.Stream(sopt) always get an error without any message.并且err = exec.Stream(sopt)总是在没有任何消息的情况下出现错误。 Can anyone tell me how to debug this problem, or give me a correct example.谁能告诉我如何调试这个问题,或者给我一个正确的例子。

config := &restclient.Config{
    Host:     "http://192.168.8.175:8080",
    Insecure: true,
}

config.ContentConfig.GroupVersion = &api.Unversioned
config.ContentConfig.NegotiatedSerializer = api.Codecs

restClient, err := restclient.RESTClientFor(config)
if err != nil {
    panic(err.Error())
}

req := restClient.Post().Resource("pods").Name("wordpress-mysql-213049546-29s7d").Namespace("default").SubResource("exec").Param("container", "mysql")
req.VersionedParams(&api.PodExecOptions{
    Container: "mysql",
    Command:   []string{"ls"},
    Stdin:     true,
    Stdout:    true,
}, api.ParameterCodec)

exec, err := remotecommand.NewExecutor(config, "POST", req.URL())
if err != nil {
    panic(err.Error())
}
sopt := remotecommand.StreamOptions{
    SupportedProtocols: remotecommandserver.SupportedStreamingProtocols,
    Stdin:              os.Stdin,
    Stdout:             os.Stdout,
    Stderr:             os.Stderr,
    Tty:                false,
}

err = exec.Stream(sopt)
if err != nil {
    panic(err.Error())
}
package k8s

import (
    "io"

    v1 "k8s.io/api/core/v1"
    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/kubernetes/scheme"
    restclient "k8s.io/client-go/rest"
    "k8s.io/client-go/tools/remotecommand"
)

// ExecCmd exec command on specific pod and wait the command's output.
func ExecCmdExample(client kubernetes.Interface, config *restclient.Config, podName string,
    command string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
    cmd := []string{
        "sh",
        "-c",
        command,
    }
    req := client.CoreV1().RESTClient().Post().Resource("pods").Name(podName).
        Namespace("default").SubResource("exec")
    option := &v1.PodExecOptions{
        Command: cmd,
        Stdin:   true,
        Stdout:  true,
        Stderr:  true,
        TTY:     true,
    }
    if stdin == nil {
        option.Stdin = false
    }
    req.VersionedParams(
        option,
        scheme.ParameterCodec,
    )
    exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
    if err != nil {
        return err
    }
    err = exec.Stream(remotecommand.StreamOptions{
        Stdin:  stdin,
        Stdout: stdout,
        Stderr: stderr,
    })
    if err != nil {
        return err
    }

    return nil
}

It works to me.它对我有用。

你可能会感兴趣,看看e2e/framework/exec_util.go

当命令是一个以/bin/sh开头的字符串数组时,它对我/bin/sh

[]string{"/bin/sh", "-c", "ls", "-ll", "."}

I faced this problem in exec ing into the Pod using Kubernetes' Client-go for long.我在长时间使用 Kubernetes 的Client-go exec Pod 时遇到了这个问题。 But finally I found a way to make it work.但最后我找到了让它工作的方法。 I have written a simple code to perform this task here in my repo.我写了一个简单的代码来执行此任务在这里我的回购协议。 I request you to check it.我请你检查一下。 I believe it will be helpful.我相信它会有所帮助。

创建请求时您会错过.CoreV1()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM