6

私はcontianer(kubernetes 1.1.2でGKE上のKubernetes PODで)でコマンドを実行しようとしています。APIを使ってポッド(kubernetes)でコマンドを実行する方法は?

ドキュメントを読む私はGETまたはPOSTクエリを使用してコマンドを実行するAPIエンドポイントでWebSocket接続を開くことができると理解しました。 GETを使用すると、完全には機能しませんが、エラーを返します。私はPOSTを使用しようとすると、そのようなことは、おそらく仕事ができる(しかし、そうではありません)。そのため

curl 'https://admin:[email protected]/api/v1/namespaces/default/pods/hello-whue1/exec?stdout=1&stderr=1&command=ls' -H "Connection: upgrade" -k -X POST -H 'Upgrade: websocket' 

repsponseは

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"}} 

それはポストの要求をアップグレードし、使用を開始するのに十分でなければならないように見えていますwebsocketストリーム、右?私は何が欠けているのですか?

POSTでwebsocketを開いても、おそらくwebsocketプロトコルに違反していると指摘されました(GETだけで動作するはずです)。

また

+0

@George:間違ったリンク? – spinus

+0

このBlogpostが役立ちます。 http://kamalmarhubi.com/blog/2015/09/06/kubernetes-from-the-ground-up-the-api-server/ – George

+0

@George私はそのブログの投稿、特にAPIを使用して何か不足していますか? – spinus

答えて

2

おそらくKubectlが使用するのと同じコードである、Kubernetes client libraryを使用して最高の時間を持っていますが、私の最高の提案を一読するよりも、オプションではありませんいくつかの理由であればよthe client library's code for executing remote commandsとそれが設定するヘッダを見てください。

+0

kubernetesクライアントライブラリ(python)を使用できませんでした。私はコードを調べましたが、コードで見つけたのは(私がその質問をする前に)SPDYプロトコルだけがサポートされているということですが、WebSocketも同様に動作するはずです。確かに)。だから私は何かを誤解しているか、WebSocketの実装が欠落しています。とにかく私は回避策としてhttp://stackoverflow.com/questions/34373472/how-to-execute-command-in-a-pod-kubernetes-using-api#comment58212217_34373472 – spinus

1

websocketクライアントを使用してください。

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

接続URL:MAXOSオン

"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" 

、wsclientのCLIツール:私の地元のkuberenetesクラスター、このような接続のメタデータで

wscat、あなたはとしてそれを使用することができますテストツール:

enter image description here

websocketの例にアクセスできます。「https://github.com/lth2015/container-terminal

+1

多分それは今修正されました!どのバージョンのkubernetesを使用していますか?私は前もって長時間前に疲れたと思う) – spinus

+0

私のkubernetesバージョンは1.2.0 – litanhua

+0

ああ、それは、おそらくws://の代わりにhttpsを使った – spinus

0

websocketクライアントを使用してポッドにexecすることができます。a quick demo

<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> 

あなたが他の言語でそれをテストすることができます。kubernetesに接続する方法を示しjavascriptのコード

関連する問題