Skip to content

Metrics

List metrics of a node or pod,...

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$ oc adm top pod
NAME                              CPU(cores)   MEMORY(bytes)
router-default-7469c545dd-d795m   2m           31Mi
router-default-7469c545dd-q9zrz   2m           32Mi

$ oc adm top node
NAME        CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
compute-0   131m         8%     920Mi           13%
compute-1   139m         9%     981Mi           14%
compute-2   251m         16%    2135Mi          31%
master-0    794m         22%    3311Mi          22%
master-1    1199m        34%    4556Mi          30%
master-2    885m         25%    4669Mi          31%

Fetch Metrics API inside a POD

1
2
oc create serviceaccount viewer
oc policy add-role-to-user view -z viewer
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: cli
  name: cli
spec:
  replicas: 1
  selector:
    matchLabels:
      app: cli
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: cli
    spec:
      serviceAccount: viewer
      serviceAccountName: viewer
      containers:
      - image: image-registry.openshift-image-registry.svc:5000/openshift/cli:latest
        imagePullPolicy: Always
        name: cli
        # command: ["/usr/bin/sleep","infinity"]
        command:
          - /usr/bin/sh
          - -c
          - |
            while true; do
              date;
              echo "====== OC ======"
              oc adm top pod
              echo "====== cURL ====="
              curl -s --cacert /run/secrets/kubernetes.io/serviceaccount/ca.crt \
                --header "Authorization: Bearer $(cat /run/secrets/kubernetes.io/serviceaccount/token)" \
                https://kubernetes.default.svc.cluster.local/apis/metrics.k8s.io/v1beta1/namespaces/$KUBERNETES_NAMESPACE/pods ;
              sleep 5;
            done
        # https://access.redhat.com/solutions/5175931
        env:
          - name: HOME
            value: /tmp
          - name: KUBERNETES_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace

Last update: July 30, 2020