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

How to get status of pods? #16

Closed
DivyaYash opened this issue Aug 30, 2016 · 9 comments
Closed

How to get status of pods? #16

DivyaYash opened this issue Aug 30, 2016 · 9 comments

Comments

@DivyaYash
Copy link

When I do a k8.namespaces.replicationcontrollers.pod.get(print); Its a very long o/p..I am interested in knowing if the pod is in "Running" status or not..How can I fetch this information?
Please help!

@silasbw
Copy link
Contributor

silasbw commented Aug 30, 2016

@DivyaYash that short hand is useful when you want to get pods associated with a specific replication controller. For example,

k8.ns('my-ns').rc.po.get('my-rc', print);

You can see what pods are running be inspecting that result:

function print(err, result) {
  if (err) return console.warn(err);
  // Interesting stuff about about the Pod                                                                                                                                                                                     
  const statuses = result.podList.items.map(pod => pod.status);
  // The phase the Pod is in (e.g., 'Running' or 'Terminating')                                                                                                                                                                
  const phases = statuses.map(status => status.phase);
  console.log(`Pod phases for RC ${ result.rc.metadata.name }:`);
  console.log(phases);
}

@silasbw silasbw closed this as completed Aug 30, 2016
@DivyaYash
Copy link
Author

Getting a Type error..
const statuses = result.podList.items.map(pod => pod.status);
^
TypeError: Cannot read property 'items' of undefined

@silasbw
Copy link
Contributor

silasbw commented Aug 30, 2016

can you post the code you're running?

@DivyaYash
Copy link
Author

Just copied urs and trying...am very new, do not know much ..
var fs = require('fs');
const K8Api = require('kubernetes-client');
const k8 = new K8Api({
url: 'https://bfk8201:6443',
version: 'v1', // Defaults to 'v1'
ca: fs.readFileSync('ca.pem'),
cert: fs.readFileSync('kubernetes-caasadmin-user.pem'),
key: fs.readFileSync('kubernetes-caasadmin-user-key.pem'),
namespace: 'netflow'
});
function print(err, result) {
if (err) return console.warn(err);
// Interesting stuff about about the Pod
const statuses = result.podList.item.map(pod => pod.status);
// The phase the Pod is in (e.g., 'Running' or 'Terminating')
const phases = statuses.map(status => status.phase);
console.log(Pod phases for RC ${ result.rc.metadata.name }:);
console.log(phases);
}

k8.namespaces.replicationcontrollers.pod.get(print);

@silasbw
Copy link
Contributor

silasbw commented Aug 31, 2016

Change this line:

k8.namespaces.replicationcontrollers.pod.get(print);

to include the name of the replication controller you're interested in. For example, if you want to get all pods associated with your replication controller named 'foo-rc', you'd do:

k8.namespaces.replicationcontrollers.pod.get('foo-rc', print);

If you're interested in getting status for all the Pods in a namespace, just do:

function print(err, result) {
  const statuses = result.item.map(pod => pod.status);
  const phases = statuses.map(status => status.phase);
  console.log(phases);
}
k8.namespaces.pods.get(print);

@silasbw silasbw reopened this Aug 31, 2016
@DivyaYash
Copy link
Author

Thanks for the quick response, I am interested in status of all pods, I tried as you suggested, but getting error - const statuses = result.item.map(pod => pod.status);
^

TypeError: Cannot read property 'map' of undefined

var fs = require('fs');
const K8Api = require('kubernetes-client');
const k8 = new K8Api({
//url: 'http://9.45.125.92:8080',
url: 'https://bfk8s01:6443',
version: 'v1', // Defaults to 'v1'
ca: fs.readFileSync('ca.pem'),
cert: fs.readFileSync('kubernetes-caasadmin-user.pem'),
key: fs.readFileSync('kubernetes-caasadmin-user-key.pem'),
namespace: 'bluecloud2'
//namespace: 'netflow'
});

function print(err, result) {
const statuses = result.item.map(pod => pod.status);
const phases = statuses.map(status => status.phase);
console.log(phases);
}
k8.namespaces.pods.get(print);

@DivyaYash
Copy link
Author

Also, where can I know on "how to construct below calls"? which documentation I need to refer?
k8.namespaces.pods.get(print);
result.item.map(pod => pod.status);
statuses.map(status => status.phase);

@silasbw
Copy link
Contributor

silasbw commented Aug 31, 2016

sorry, I typoed:

function print(err, result) {
  const statuses = result.items.map(pod => pod.status);
  const phases = statuses.map(status => status.phase);
  console.log(phases);
}

(notice items instead of item)

For general JavaScript reference there's MDN

For Kubernetes API documentation there's the Kubernetes Docs. E.g., here's the definition of the PodList which is the thing we're operating on above: http://kubernetes.io/docs/api-reference/v1/definitions/#_v1_podlist

Since this is more of JavaScript help question and less specific to kubernetes-client, I'm closing this again.

@silasbw silasbw closed this as completed Aug 31, 2016
@DivyaYash
Copy link
Author

Ok, thanks for your help, it worked!
The o/p seem to be listing some old non existing pods as well...The result of this call has a few pods in "Failed" status, however, when I check via kubectl get pods cmd, the results are different...

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

No branches or pull requests

2 participants