How to execute a command in a container (kubernet) using the API?

I am trying to execute a command in contianer (in Kubernetes POD on GKE with kubernet 1.1.2).

Reading documentation. I realized that I could use a GET or POST request to open a connection to the websocket on the API endpoint to execute the command. When I use GET, it does not work completely, it returns an error. When I try to use POST, something like this might work (but it doesn't):

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

repsponse for this

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

It seems like this should be enough to update the mail request and start using websocket streams, right? What am I missing?

I also pointed out that opening a websocket with POST is probably a violation of the websocket protocol (should only GET work?).

Besides

+6
source share
3 answers

You will probably be best off using the Kubernetes client library , which is the same code as Kubectl, but if for some reason this is not an option, than my best suggestion is to look at the client library code to execute remote commands and see which headers he sets.

+2
source

Use websocket client.

In my local kuberenetes cluster, connection metadata:

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

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

In the maxos CLI tool wsclient: wscat , you can use it as a test tool:

enter image description here

You can access the websocket example: " https://github.com/lth2015/container-terminal "

+1
source

You can use the websocket client to execute in pod, a quick demo .

The javascript code shows how to connect to the quernets:

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

You can test it in another language.

0
source

All Articles