How to copy files between pods or execute in a pod?

6/25/2019

I'm trying to execute a terminal command in a pod to get the file content. It works fine on local:

const Client = require('kubernetes-client').Client;
const Config = require('kubernetes-client/backends/request').config;
const Request = require('kubernetes-client/backends/request');

const config = Config.fromKubeconfig();
const client = new Client({ config: config, version: '1.13' });

const podResponse = await client.api.v1.namespaces(config.namespace).pods(<pod name>).exec.get({
        qs: {
            command: ['cat', 'README.md'],
            container: <container name>,
            stdout: true,
            stderr: true,
        },
    });

console.log(podResponse.body);

When I run my Node.js app on Cluster with following changes:

const config = Request.config.getInCluster();
const backend = new Request(config);
const client = new Client({ backend });

it still works fine. I can get pods/services info (Node.js app run on same cluster/namespace).. But .exec.get doesn't work. It fails with:

{ Error: Unexpected server response: 401
        at ClientRequest.req.on (/usr/local/app/node_modules/ws/lib/websocket.js:579:7)
        at ClientRequest.emit (events.js:182:13)
        at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:556:21)
        at HTTPParser.parserOnHeadersComplete (_http_common.js:109:17)
        at TLSSocket.socketOnData (_http_client.js:442:20)
        at TLSSocket.emit (events.js:182:13)
        at addChunk (_stream_readable.js:283:12)
        at readableAddChunk (_stream_readable.js:264:11)
        at TLSSocket.Readable.push (_stream_readable.js:219:10)
        at TLSWrap.onStreamRead (internal/stream_base_commons.js:94:17) messages: [] }

Again, I don't need to pass any auth info.. It works fine to get pods/services details. Seems the issue will be fixed in new release: kubernetes-client repo

So wondering if there is other way to copy a file from a pod by JavaScript, i.e. analog of:

kubectl cp <file-spec-src> <file-spec-dest>
-- John Glabb
javascript
kubernetes
kubernetes-pod
node.js

0 Answers