Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google cloud platform 如何使用API在pod(kubernetes)中执行命令?_Google Cloud Platform_Kubernetes_Google Kubernetes Engine - Fatal编程技术网

Google cloud platform 如何使用API在pod(kubernetes)中执行命令?

Google cloud platform 如何使用API在pod(kubernetes)中执行命令?,google-cloud-platform,kubernetes,google-kubernetes-engine,Google Cloud Platform,Kubernetes,Google Kubernetes Engine,我试图在一个contianer中执行命令(在Kubernetes 1.1.2版本的GKE上的Kubernetes POD中) 通过阅读文档,我了解到我可以使用GET或POST查询在API端点上打开websocket连接来执行命令。当我使用GET时,它不能完全工作,返回错误。当我尝试使用POST时,类似的方法可能会奏效(但事实并非如此): 对此的回应是 unable to upgrade: missing upgrade headers in request: http.Header{"User-

我试图在一个contianer中执行命令(在Kubernetes 1.1.2版本的GKE上的Kubernetes POD中)

通过阅读文档,我了解到我可以使用GET或POST查询在API端点上打开websocket连接来执行命令。当我使用GET时,它不能完全工作,返回错误。当我尝试使用POST时,类似的方法可能会奏效(但事实并非如此):

对此的回应是

unable to upgrade: missing upgrade headers in request: http.Header{"User-Agent":[]string{"curl/7.44.0"}, "Content-Length":[]string{"0"}, "Accept":[]string{"*/*"}, "Authorization":[]string{"Basic xxx=="}, "Connection":[]string{"upgrade"}, "Upgrade":[]string{"websocket"}}
看起来这足以升级post请求并开始使用websocket流,对吗?我错过了什么

我还被指出,用POST打开websocket可能违反了websocket协议(只有GET应该工作?)


另外,您可能会最喜欢使用,这与Kubectl使用的代码相同,但如果出于某种原因,这不是一个选项,那么我的最佳建议是查看并查看它设置的标题。

使用websocket客户端,这是可行的

在我的本地kuberenetes群集中,连接元数据如下所示:

ApiServer = "172.21.1.11:8080"
Namespace = "default"
PodName = "my-nginx-3855515330-l1uqk"
ContainerName = "my-nginx"
Commands = "/bin/bash"
连接url:

"ws://172.21.1.11:8080/api/v1/namespaces/default/pods/my-nginx-3855515330-l1uqk/exec?container=my-nginx&stdin=1&stdout=1&stderr=1&tty=1&command=%2Fbin%2Fbash"
在maxos(wsclient CLI工具)上,您可以将其用作测试工具:


您可以访问websocket示例:“

您可以使用websocket客户端执行到pod中

javascript代码显示了如何连接到kubernetes:

<script type="text/javascript">
    angular.module('exampleApp', ['kubernetesUI'])
        .config(function(kubernetesContainerSocketProvider) {
            kubernetesContainerSocketProvider.WebSocketFactory = "CustomWebSockets";
        })
        .run(function($rootScope) {
            $rootScope.baseUrl = "ws://localhost:8080";
            $rootScope.selfLink = "/api/v1/namespaces/default/pods/my-nginx-3855515330-l1uqk";
            $rootScope.containerName = "my-nginx";
            $rootScope.accessToken = "";
            $rootScope.preventSocket = true;
        })
        /* Our custom WebSocket factory adapts the url */
        .factory("CustomWebSockets", function($rootScope) {
            return function CustomWebSocket(url, protocols) {
                url = $rootScope.baseUrl + url;
                if ($rootScope.accessToken)
                    url += "&access_token=" + $rootScope.accessToken;
                return new WebSocket(url, protocols);
            };
        });
</script>

angular.module('exampleApp',['kubernetesUI']))
.config(函数(kubernetesContainerSocketProvider){
kubernetesContainerSocketProvider.WebSocketFactory=“CustomWebSockets”;
})
.run(函数($rootScope){
$rootScope.baseUrl=“ws://localhost:8080”;
$rootScope.selfLink=“/api/v1/namespaces/default/pods/my-nginx-3855515330-l1uqk”;
$rootScope.containerName=“我的nginx”;
$rootScope.accessToken=“”;
$rootScope.preventSocket=true;
})
/*我们的自定义WebSocket工厂采用url*/
.factory(“CustomWebSockets”,函数($rootScope){
返回函数CustomWebSocket(url、协议){
url=$rootScope.baseUrl+url;
if($rootScope.accessToken)
url+=“&access_-token=“+$rootScope.accessToken;
返回新的WebSocket(url、协议);
};
});

你可以用其他语言测试它。

@George:链接错了?@George我在那篇博文中没有看到任何关于执行命令的内容,尤其是使用API。我遗漏了什么吗?你试过修改aiohttp吗?也许对你的情况会有帮助。@George,我试过了,没有结果。我发现我需要升级到spdy的连接,而不是websockets(至少在GCP使用的版本中)作为解决方案,现在我只是在生成进程并执行“kubectl exec”作为解决方案。我不能使用kubernetes客户端库(python)。我浏览了代码,但我在代码中发现(在我问这个问题之前)只支持SPDY协议,但我在文档中的某个地方发现WebSocket也应该工作(可能没有WebSocket,现在还不确定)。所以我误解了什么,或者websocket实现缺失了。不管怎样,作为一个解决办法,我做了,也许它现在已经修好了!?你用的是什么版本的kubernetes?我想我很久以前就累了(上次)我的kubernetes版本是1.2.0ah,就是这样,我可能使用https而不是ws://,谢谢
<script type="text/javascript">
    angular.module('exampleApp', ['kubernetesUI'])
        .config(function(kubernetesContainerSocketProvider) {
            kubernetesContainerSocketProvider.WebSocketFactory = "CustomWebSockets";
        })
        .run(function($rootScope) {
            $rootScope.baseUrl = "ws://localhost:8080";
            $rootScope.selfLink = "/api/v1/namespaces/default/pods/my-nginx-3855515330-l1uqk";
            $rootScope.containerName = "my-nginx";
            $rootScope.accessToken = "";
            $rootScope.preventSocket = true;
        })
        /* Our custom WebSocket factory adapts the url */
        .factory("CustomWebSockets", function($rootScope) {
            return function CustomWebSocket(url, protocols) {
                url = $rootScope.baseUrl + url;
                if ($rootScope.accessToken)
                    url += "&access_token=" + $rootScope.accessToken;
                return new WebSocket(url, protocols);
            };
        });
</script>