Kubernetes Websockets API pod exec node.js客户端发送方法
我很难使用客户端将命令发送到交互式bashshell。我正在运行一个带有kops的集群,我能够与pod建立连接并得到它的提示,但我无法发送命令和接收响应 所以我的连接看起来像:Kubernetes Websockets API pod exec node.js客户端发送方法,node.js,websocket,kubernetes,Node.js,Websocket,Kubernetes,我很难使用客户端将命令发送到交互式bashshell。我正在运行一个带有kops的集群,我能够与pod建立连接并得到它的提示,但我无法发送命令和接收响应 所以我的连接看起来像: const WebSocket = require('ws'); const fs = require('fs'); const readline = require('readline'); const unescape = require('querystring').unescape; const escape =
const WebSocket = require('ws');
const fs = require('fs');
const readline = require('readline');
const unescape = require('querystring').unescape;
const escape = require('querystring').escape;
const channel = '0';
const access_token = "[the_api_token]";
const pod_name = 'nginx-726417742-s1nv2';
const host_address = 'wss://[the_api_url]';
const cmd = `${escape('/bin/bash')}&command=-i`;
const params = `stdout=1&stdin=1&stderr=1&tty=1&container=nginx&command=${cmd}`;
const url = `${host_address}/api/v1/namespaces/default/pods/${pod_name}/exec?${params}`;
const options = {
headers: {
'Authorization': `Bearer ${access_token}`
},
ca: fs.readFileSync('ca.crt'),
}
const ws = new WebSocket(url, options);
ws.on('open', () => {
console.log('connected');
});
ws.on('message', (data, flags) => {
process.stdout.write(data);
});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function (cmd) {
const data = channel + cmd;
if (ws && ws.readyState === 1) {
ws.send(data);
}
});
ws.on('message',(数据,标志)
部分完美地打印shell提示符root@nginx-726417742-s1nv2:/#
但我可以键入任何内容,然后通过ws.send(数据)发送
但没有收到任何消息,也没有生成任何错误。我已经尝试将数据转换为base64并逐字符发送,而不是整行,但结果和行为始终相同
如果我通过kubectl proxy--disable filter=true--port=8080
代理API,使用指向ws://localhost:8080
并使用/API/v1/namespaces/default/pods/${pod_name}/exec
我可以得到一个工作终端,但本地代理不是我的选项
任何帮助都将不胜感激。我知道我的答案来得晚。但也许我可以在这方面帮助其他人。我注意到,Kubernetes API在使用WebSocket时通常需要设置原点。否则,API有时会返回奇怪的错误。在您的情况下,您可以尝试完成以下选项:
const options = {
headers: {
'Authorization': `Bearer ${access_token}`
},
ca: fs.readFileSync('ca.crt'),
origin: 'https://<the_api_url>:<port>'
}
const选项={
标题:{
'Authorization':'Bearer${access_token}`
},
ca:fs.readFileSync('ca.crt'),
来源:“https://:”
}
此外,我认为不需要将
wss
设置为协议(在host\u address
变量中)。通常,在连接过程中,协议应该自动从https
升级到wss
。Hey@Marcelo,您有没有找到解决问题的方法?