我很难使用node.js websockets客户端向交互式bash shell发送命令。我正在运行一个带有kops的集群,我能够与pod建立连接并获得提示,但我无法发送命令并收到响应。
所以我的连接看起来像:
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', (data, flags)
部分完全打印shell提示符root@nginx-726417742-s1nv2:/#
但是我可以输入任何内容并且它会通过ws.send(data)
但是没有收到任何消息并且没有生成错误。我已经尝试将数据转换为base64并通过char而不是整行发送char,但结果和行为始终是相同的。
如果我通过kubectl proxy --disable-filter=true --port=8080
代理API并使用https://github.com/kubernetes-ui/container-terminal指向ws://localhost:8080
并使用/api/v1/namespaces/default/pods/${pod_name}/exec
我可以获得一个工作终端,但本地代理不是一个选项我
非常感谢任何帮助。
答案 0 :(得分:0)
我知道我的答案来晚了。但是也许我可以帮助其他人。我注意到,使用websocket时,Kubernetes API通常需要设置一个来源。否则,API有时会返回奇怪的错误。在您的情况下,您可以尝试按照以下步骤完成选择:
Euro area annual inflation up to 0.4%
GDP down by 12.1% in the euro area and by 11.9% in the EU
Euro area unemployment at 7.8%
Sharpest drop of household real consumption per capita in both euro area and EU
Government debt up to 86.3% of GDP in euro area
Seasonally adjusted government deficit rose sharply to 2.2% of GDP in the euro area
Annual inflation up to 0.3% in the euro area
Production in construction up by 27.9% in euro area and 21.2% in EU
Euro area international trade in goods surplus €9.4 bn
Industrial production up by 12.4% in euro area and 11.4% in EU
EU population in 2020: almost 448 million
Absences from work at record high
House prices up by 5.0% in the euro area
EU current account surplus €59.9 bn
Volume of retail trade up by 17.8% in euro area
Business profit share recorded sharpest drop to 37.9% while business investment is slightly down to 25.5% in the euro area
Household saving rate all time high at 16.9% in the euro area while household investment rate down to 8.7%
Euro area unemployment at 7.4%
Industrial producer prices down by 0.6% in euro area
Euro area annual inflation up to 0.3%
此外,我认为不需要将const options = {
headers: {
'Authorization': `Bearer ${access_token}`
},
ca: fs.readFileSync('ca.crt'),
origin: 'https://<the_api_url>:<port>'
}
设置为协议(在您的wss
变量中)。通常,在连接过程中,协议应该从host_address
自动升级到https
。