Grafana dashboard: Monitor Pod CPU and Memory usage

Base kube-state-metrics

{
  "__inputs": [
    {
      "name": "DS_VM-CLUSTER",
      "label": "vm-cluster",
      "description": "",
      "type": "datasource",
      "pluginId": "prometheus",
      "pluginName": "Prometheus"
    }
  ],
  "__requires": [
    {
      "type": "grafana",
      "id": "grafana",
      "name": "Grafana",
      "version": "8.0.4"
    },
    {
      "type": "datasource",
      "id": "prometheus",
      "name": "Prometheus",
      "version": "1.0.0"
    },
    {
      "type": "panel",
      "id": "timeseries",
      "name": "Time series",
      "version": ""
    }
  ],
  "annotations": {
    "list": [
      {
        "builtIn": 1,
        "datasource": "-- Grafana --",
        "enable": true,
        "hide": true,
        "iconColor": "rgba(0, 211, 255, 1)",
        "name": "Annotations & Alerts",
        "type": "dashboard"
      }
    ]
  },
  "editable": true,
  "gnetId": 15055,
  "graphTooltip": 0,
  "id": null,
  "iteration": 1632885227666,
  "links": [],
  "panels": [
    {
      "datasource": null,
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "palette-classic"
          },
          "custom": {
            "axisLabel": "",
            "axisPlacement": "auto",
            "barAlignment": 0,
            "drawStyle": "line",
            "fillOpacity": 0,
            "gradientMode": "none",
            "hideFrom": {
              "legend": false,
              "tooltip": false,
              "viz": false
            },
            "lineInterpolation": "linear",
            "lineWidth": 1,
            "pointSize": 5,
            "scaleDistribution": {
              "type": "linear"
            },
            "showPoints": "auto",
            "spanNulls": false,
            "stacking": {
              "group": "A",
              "mode": "none"
            },
            "thresholdsStyle": {
              "mode": "off"
            }
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              },
              {
                "color": "red",
                "value": 80
              }
            ]
          },
          "unit": "string"
        },
        "overrides": []
      },
      "gridPos": {
        "h": 9,
        "w": 12,
        "x": 0,
        "y": 0
      },
      "id": 2,
      "options": {
        "legend": {
          "calcs": [],
          "displayMode": "list",
          "placement": "bottom"
        },
        "tooltip": {
          "mode": "single"
        }
      },
      "targets": [
        {
          "exemplar": true,
          "expr": "max(rate(container_cpu_usage_seconds_total{pod=\"$pod\",container=\"$container\"}[5m])) by (container)",
          "interval": "",
          "legendFormat": "container usage",
          "refId": "A"
        },
        {
          "exemplar": true,
          "expr": "max(kube_pod_container_resource_requests{pod=\"$pod\",container=\"$container\",resource=\"cpu\"}) by (container)",
          "hide": false,
          "interval": "",
          "legendFormat": "container request",
          "refId": "B"
        },
        {
          "exemplar": true,
          "expr": "max(kube_pod_container_resource_limits{pod=\"$pod\",container=\"$container\",resource=\"cpu\"}) by (container)",
          "hide": false,
          "interval": "",
          "legendFormat": "container limit",
          "refId": "C"
        },
        {
          "exemplar": true,
          "expr": "sum(increase(container_cpu_cfs_throttled_periods_total{pod=\"$pod\",container=\"$container\"}[5m])) / ignoring(id) sum(increase(container_cpu_cfs_periods_total{}[5m]))",
          "hide": false,
          "interval": "",
          "legendFormat": "throttling ",
          "refId": "D"
        }
      ],
      "title": "CPU(cores)",
      "type": "timeseries"
    },
    {
      "datasource": null,
      "description": "",
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "palette-classic"
          },
          "custom": {
            "axisLabel": "",
            "axisPlacement": "auto",
            "barAlignment": 0,
            "drawStyle": "line",
            "fillOpacity": 0,
            "gradientMode": "none",
            "hideFrom": {
              "legend": false,
              "tooltip": false,
              "viz": false
            },
            "lineInterpolation": "linear",
            "lineWidth": 1,
            "pointSize": 5,
            "scaleDistribution": {
              "type": "linear"
            },
            "showPoints": "auto",
            "spanNulls": false,
            "stacking": {
              "group": "A",
              "mode": "none"
            },
            "thresholdsStyle": {
              "mode": "off"
            }
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              },
              {
                "color": "red",
                "value": 80
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 12,
        "y": 0
      },
      "id": 4,
      "options": {
        "legend": {
          "calcs": [],
          "displayMode": "list",
          "placement": "bottom"
        },
        "tooltip": {
          "mode": "single"
        }
      },
      "targets": [
        {
          "exemplar": true,
          "expr": "max(container_memory_working_set_bytes{container=\"$container\",pod=\"$pod\"} / (1024*1024)) by (container)",
          "interval": "",
          "legendFormat": "container usage",
          "refId": "A"
        },
        {
          "exemplar": true,
          "expr": "max(kube_pod_container_resource_requests{container=\"$container\",pod=\"$pod\",resource=\"memory\"}) by (container) / (1024*1024)",
          "hide": false,
          "interval": "",
          "legendFormat": "container request",
          "refId": "B"
        },
        {
          "exemplar": true,
          "expr": "max(kube_pod_container_resource_limits{pod=\"$pod\",container=\"$container\",resource=\"memory\"}) by (container) / (1024*1024)",
          "hide": false,
          "interval": "",
          "legendFormat": "container limit",
          "refId": "C"
        }
      ],
      "title": "Memory(Mi)",
      "type": "timeseries"
    },
    {
      "datasource": null,
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "palette-classic"
          },
          "custom": {
            "axisLabel": "",
            "axisPlacement": "auto",
            "barAlignment": 0,
            "drawStyle": "line",
            "fillOpacity": 0,
            "gradientMode": "none",
            "hideFrom": {
              "legend": false,
              "tooltip": false,
              "viz": false
            },
            "lineInterpolation": "linear",
            "lineWidth": 1,
            "pointSize": 5,
            "scaleDistribution": {
              "type": "linear"
            },
            "showPoints": "auto",
            "spanNulls": false,
            "stacking": {
              "group": "A",
              "mode": "none"
            },
            "thresholdsStyle": {
              "mode": "off"
            }
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              },
              {
                "color": "red",
                "value": 80
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 12,
        "y": 8
      },
      "id": 8,
      "options": {
        "legend": {
          "calcs": [],
          "displayMode": "list",
          "placement": "bottom"
        },
        "tooltip": {
          "mode": "single"
        }
      },
      "targets": [
        {
          "exemplar": true,
          "expr": "max by (owner_name,container)((container_memory_usage_bytes{container=\"$container\",pod=\"$pod\"}) * on(pod) group_left(owner_name) avg by (namespace,pod,owner_name)(kube_pod_owner{owner_kind=~\"DaemonSet|StatefulSet|ReplicaSet\"})) / (1024*1024)",
          "interval": "",
          "legendFormat": "Conservative Strategy",
          "refId": "A"
        },
        {
          "exemplar": true,
          "expr": "quantile by (owner_name,container)(0.99,(container_memory_usage_bytes{container=\"$container\",pod=\"$pod\"}) * on(pod) group_left(owner_name) avg by (namespace,pod,owner_name)(kube_pod_owner{owner_kind=~\"DaemonSet|StatefulSet|ReplicaSet\"})) / (1024*1024)",
          "hide": false,
          "interval": "",
          "legendFormat": "Aggressive Strategy",
          "refId": "B"
        }
      ],
      "title": "Expect Memory Setting",
      "type": "timeseries"
    },
    {
      "datasource": null,
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "palette-classic"
          },
          "custom": {
            "axisLabel": "",
            "axisPlacement": "auto",
            "barAlignment": 0,
            "drawStyle": "line",
            "fillOpacity": 0,
            "gradientMode": "none",
            "hideFrom": {
              "legend": false,
              "tooltip": false,
              "viz": false
            },
            "lineInterpolation": "linear",
            "lineWidth": 1,
            "pointSize": 5,
            "scaleDistribution": {
              "type": "linear"
            },
            "showPoints": "auto",
            "spanNulls": false,
            "stacking": {
              "group": "A",
              "mode": "none"
            },
            "thresholdsStyle": {
              "mode": "off"
            }
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              },
              {
                "color": "red",
                "value": 80
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 0,
        "y": 9
      },
      "id": 6,
      "options": {
        "legend": {
          "calcs": [],
          "displayMode": "list",
          "placement": "bottom"
        },
        "tooltip": {
          "mode": "single"
        }
      },
      "targets": [
        {
          "exemplar": true,
          "expr": "max by (container,owner_name)((rate(container_cpu_usage_seconds_total{pod=\"$pod\",container=\"$container\"}[5m])) * on(pod) group_left(owner_name) avg by (namespace,pod,owner_name)(kube_pod_owner{owner_kind=~\"DaemonSet|StatefulSet|ReplicaSet\"}))",
          "hide": false,
          "interval": "",
          "legendFormat": "Conservative Strategy ",
          "refId": "A"
        },
        {
          "exemplar": true,
          "expr": "quantile by (owner_name,container)(0.99,(rate(container_cpu_usage_seconds_total{pod=\"$pod\",container=\"$container\"}[5m])) * on(pod) group_left(owner_name) avg by (namespace,pod,owner_name)(kube_pod_owner{owner_kind=~\"DaemonSet|StatefulSet|ReplicaSet\"}))",
          "hide": false,
          "interval": "",
          "legendFormat": "Aggressive Strategy",
          "refId": "B"
        }
      ],
      "title": "Expect CPU Setting",
      "type": "timeseries"
    }
  ],
  "refresh": "",
  "schemaVersion": 30,
  "style": "dark",
  "tags": [],
  "templating": {
    "list": [
      {
        "allValue": null,
        "current": {},
        "datasource": "${DS_VM-CLUSTER}",
        "definition": "label_values(kube_pod_owner{owner_kind=~\"DaemonSet|StatefulSet|ReplicaSet\"},owner_name)",
        "description": null,
        "error": null,
        "hide": 0,
        "includeAll": false,
        "label": "App",
        "multi": false,
        "name": "app",
        "options": [],
        "query": {
          "query": "label_values(kube_pod_owner{owner_kind=~\"DaemonSet|StatefulSet|ReplicaSet\"},owner_name)",
          "refId": "StandardVariableQuery"
        },
        "refresh": 1,
        "regex": "",
        "skipUrlSync": false,
        "sort": 1,
        "type": "query"
      },
      {
        "allValue": null,
        "current": {},
        "datasource": "${DS_VM-CLUSTER}",
        "definition": "label_values(kube_pod_owner{owner_name=\"$app\"},pod)",
        "description": null,
        "error": null,
        "hide": 0,
        "includeAll": false,
        "label": "Pod",
        "multi": false,
        "name": "pod",
        "options": [],
        "query": {
          "query": "label_values(kube_pod_owner{owner_name=\"$app\"},pod)",
          "refId": "StandardVariableQuery"
        },
        "refresh": 2,
        "regex": "",
        "skipUrlSync": false,
        "sort": 0,
        "type": "query"
      },
      {
        "allValue": null,
        "current": {},
        "datasource": "${DS_VM-CLUSTER}",
        "definition": "label_values(kube_pod_container_info{pod=\"$pod\"},container)",
        "description": null,
        "error": null,
        "hide": 0,
        "includeAll": false,
        "label": "Container",
        "multi": false,
        "name": "container",
        "options": [],
        "query": {
          "query": "label_values(kube_pod_container_info{pod=\"$pod\"},container)",
          "refId": "StandardVariableQuery"
        },
        "refresh": 1,
        "regex": "",
        "skipUrlSync": false,
        "sort": 0,
        "type": "query"
      }
    ]
  },
  "time": {
    "from": "now-3h",
    "to": "now"
  },
  "timepicker": {},
  "timezone": "",
  "title": "Monitor Pod CPU and Memory usage",
  "uid": "wu-8KEN7z",
  "version": 69
}