【问题标题】:How to invoke the Pod proxy verb using the Kubernetes Go client?如何使用 Kubernetes Go 客户端调用 Pod 代理动词?
【发布时间】:2019-10-09 06:59:21
【问题描述】:

Kubernetes 远程 API 允许使用代理动词 HTTP 访问任意 pod 端口,即使用 API 路径 /api/v1/namespaces/{namespace}/pods/{name}/proxy

Python 客户端提供corev1.connect_get_namespaced_pod_proxy_with_path() 来调用上述代理动词。

尽管阅读、浏览和搜索 Kubernetes 客户端-go 已经有一段时间了,但我仍然不知道如何使用 goclient 执行与 python 客户端相同的操作。我的另一个印象是,如果没有现成的 API corev1 调用可用,我可能需要深入了解客户端变更集的其余客户端?

如何正确使用其余客户端和上述路径构造 GET 调用?

【问题讨论】:

    标签: kubernetes proxy kubernetes-go-client


    【解决方案1】:

    在深入了解 Kubernetes 客户端资源后发现,只有在下降到 RESTClient 的级别,然后手动构建 GET/... 请求时,才能访问代理动词。以下代码以完整工作示例的形式展示了这一点:

    package main
    
    import (
        "fmt"
    
        "k8s.io/client-go/kubernetes"
        "k8s.io/client-go/tools/clientcmd"
    )
    
    func main() {
        clcfg, err := clientcmd.NewDefaultClientConfigLoadingRules().Load()
        if err != nil {
            panic(err.Error())
        }
        restcfg, err := clientcmd.NewNonInteractiveClientConfig(
            *clcfg, "", &clientcmd.ConfigOverrides{}, nil).ClientConfig()
        if err != nil {
            panic(err.Error())
        }
        clientset, err := kubernetes.NewForConfig(restcfg)
        res := clientset.CoreV1().RESTClient().Get().
            Namespace("default").
            Resource("pods").
            Name("hello-world:8000").
            SubResource("proxy").
            // The server URL path, without leading "/" goes here...
            Suffix("index.html").
            Do()
        if err != nil {
            panic(err.Error())
        }
        rawbody, err := res.Raw()
        if err != nil {
            panic(err.Error())
        }
        fmt.Print(string(rawbody))
    }
    

    例如,您可以在本地 kind 集群(Docker 中的 Kubernetes)上对此进行测试。以下命令启动了一个 kind 集群,为唯一的节点启动所需的 hello-world 网络服务器,然后告诉 Kubernetes 使用该 hello-world 网络服务器启动 pod。

    kind create cluster
    docker pull crccheck/hello-world
    docker tag crccheck/hello-world crccheck/hello-world:current
    kind load docker-image crccheck/hello-world:current
    kubectl run hello-world --image=crccheck/hello-world:current --port=8000 --restart=Never --image-pull-policy=Never
    

    现在运行示例:

    export KUBECONFIG=~/.kube/kind-config-kind; go run .
    

    然后它应该显示这个 ASCII 艺术:

    <xmp>
    Hello World
    
    
                                           ##         .
                                     ## ## ##        ==
                                  ## ## ## ## ##    ===
                               /""""""""""""""""\___/ ===
                          ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
                               \______ o          _,/
                                \      \       _,'
                                 `'--.._\..--''
    </xmp>
    

    【讨论】:

    • 这还能用吗?如果尝试在 Name(') 方法中添加端口,但调用响应错误,指出 pod-name-21812-aksj:15014 不存在。
    猜你喜欢
    • 2019-05-30
    • 2019-03-16
    • 2019-02-19
    • 2018-07-01
    • 1970-01-01
    • 2022-01-02
    • 2021-05-30
    • 2018-02-27
    • 2017-07-04
    相关资源
    最近更新 更多