Wednesday, December 12, 2018

How To Monitor Any Kubernetes Objects Status Live via Core Kubernetes API


   As core kubernetes APIs are implemented as HTTP interface, it provides ability to check and monitor any kubernetes objects live via curl . For example, we have a pod running  ORDS for APEX.  We can use curl to watch it via Kubernetes API . If the pod is deleted or fails, we can get some update on curl output.  The concept applies to any objects in the K8S cluster. You can do the same for  node,service, pv, pvc......etc

Watch Node Steps:

  • kubectl proxy --port=8080 &   refer my other note
  • curl

Watch Pod Steps:

  • Get yaml output details of the pod you would like to watch
$ kubectl get pod
NAME                                                      READY     STATUS    RESTARTS   AGE
apexords-mt-deployment-7c7f95c954-5k7c5                   1/1       Running   0          53m
$ kubectl get pod apexords-mt-deployment-7c7f95c954-5k7c5 -o yaml
apiVersion: v1
kind: Pod
    controller: true
    kind: ReplicaSet
    name: apexords-mt-deployment-7c7f95c954
    uid: bc88c05f-dcb0-11e8-9ee8-000017010a8f
  resourceVersion: "5067431"
  selfLink: /api/v1/namespaces/default/pods/apexords-mt-deployment-7c7f95c954-5k7c5
  uid: aadc94f8-fe66-11e8-b83f-000017010a8f
  • Find resourceVersion : 5067431 which we will use in curl
  • We need to proxy the Kubernetes API locally using kubectl proxy, we can discover the object we would like to watch apexords-mt-deployment-7c7f95c954-5k7c5  refer my other note
$kubectl proxy --port=8080 &
  • Use curl to discover the object apexords-mt-deployment-7c7f95c954-5k7c5

  "kind": "Pod",
  "apiVersion": "v1",
  "metadata": {
    "name": "apexords-mt-deployment-7c7f95c954-5k7c5",
    "generateName": "apexords-mt-deployment-7c7f95c954-",
    "namespace": "default",
    "selfLink": "/api/v1/namespaces/default/pods/apexords-mt-deployment-7c7f95c954-5k7c5",
    "uid": "aadc94f8-fe66-11e8-b83f-000017010a8f",
    "resourceVersion": "5067431",
    "creationTimestamp": "2018-12-12
  • Use curl to watch the object apexords-mt-deployment-7c7f95c954-5k7c5
curl -f
<don't click enter, otherwise it exits>
  • Open another session to delete this pod, see the a live update from API server via curl watch command
$kubectl delete pod apexords-mt-deployment-7c7f95c954-5k7c5

No comments: