Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hello, an enquiry please, is there a method to get inside the containers using this kubernetes-client? #19

Closed
DivyaYash opened this issue Sep 1, 2016 · 6 comments · Fixed by #329

Comments

@DivyaYash
Copy link

DivyaYash commented Sep 1, 2016

I wanted to programmatically get inside the container and check if a certain process is up or not..Is that possible?

@silasbw
Copy link
Contributor

silasbw commented Sep 2, 2016

kubernetes-client does not currently support this. We should support it. Do you want to try adding support for this?

@DivyaYash
Copy link
Author

@silasbw, am very new to the whole development stuff, with my skill level, I do not think I can contribute :( But definitely in need of this feature..I do not want to use kubectl hence looking for other means..

@silasbw
Copy link
Contributor

silasbw commented Sep 13, 2016

Understood: you can use kubectl exec for getting a nice interactive shell in the container running as part of a pod. We plan on adding exec support to kubernetes-client, but we we're working on other priorities right now.

@DivyaYash
Copy link
Author

Yes, for now am going to use the kubectl exec to get the desired feature..Please let me know when this feature is added to kubernetes-client..Thank you!

@nick
Copy link
Contributor

nick commented Feb 13, 2017

It took me a while to piece this together, so leaving it here to help others. The following code lets you run the equivalent of kube exec programatically. You may need to run the script with NODE_TLS_REJECT_UNAUTHORIZED=0 if there is a certificate error. You'll also need the ws package.

const WebSocket = require('ws');

function exec(apiUri, namespace, pod, cmd, callback) {
    var response = '', uri = '';

    uri += `wss://${apiUri}/api/v1/namespaces/${namespace}/pods/${pod}/exec?`;
    uri += 'stdout=1&stdin=1&stderr=1';
    cmd.forEach(subCmd => uri += `&command=${encodeURIComponent(subCmd)}`);

    var ws = new WebSocket(uri, "base64.channel.k8s.io")
    ws.on('message', (data) => {
        if (data[0].match(/^[0-3]$/)) {
            response += Buffer.from(data.slice(1), 'base64').toString("ascii");
        }
    });
    ws.on('close', () => callback(response));
}

exec(
    'user:pass@kubecluster:6443',
    'default',
    'pod-name',
    ['/bin/sh', '-c', 'df -h | grep device'],
    (response) => {
        console.log(response);
    }
)

@silasbw
Copy link
Contributor

silasbw commented Feb 13, 2017

Oh wow, nice. Would love to see a PR for this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants