{
  "title": "Cluster Pods",
  "uid": "cluster-pods",
  "schemaVersion": 39,
  "version": 4,
  "editable": true,
  "tags": ["cluster", "pod"],
  "time": { "from": "now-1h", "to": "now" },
  "refresh": "30s",
  "templating": {
    "list": [
      {
        "name": "namespace",
        "type": "query",
        "datasource": { "type": "prometheus", "uid": "prometheus" },
        "query": "label_values(kube_pod_info, namespace)",
        "refresh": 1,
        "includeAll": true,
        "multi": true,
        "current": { "text": "All", "value": "$__all" }
      }
    ]
  },
  "panels": [
    {
      "id": 1,
      "title": "Pod count by phase",
      "type": "stat",
      "datasource": { "type": "prometheus", "uid": "prometheus" },
      "targets": [
        {
          "expr": "sum by (phase) (kube_pod_status_phase{namespace=~\"$namespace\"})",
          "legendFormat": "{{phase}}",
          "refId": "A"
        }
      ],
      "gridPos": { "h": 6, "w": 12, "x": 0, "y": 0 }
    },
    {
      "id": 2,
      "title": "Pod count by namespace",
      "type": "bargauge",
      "datasource": { "type": "prometheus", "uid": "prometheus" },
      "targets": [
        {
          "expr": "count by (namespace) (kube_pod_info{namespace=~\"$namespace\"})",
          "legendFormat": "{{namespace}}",
          "refId": "A"
        }
      ],
      "gridPos": { "h": 6, "w": 12, "x": 12, "y": 0 }
    },
    {
      "id": 20,
      "title": "Pod 状態一覧",
      "type": "table",
      "datasource": { "type": "prometheus", "uid": "prometheus" },
      "targets": [
        {
          "expr": "sum by (namespace, pod, node, created_by_kind, created_by_name) (kube_pod_info{namespace=~\"$namespace\"})",
          "format": "table",
          "instant": true,
          "refId": "A"
        },
        {
          "expr": "sum by (namespace, pod, phase) (kube_pod_status_phase{namespace=~\"$namespace\"} == 1)",
          "format": "table",
          "instant": true,
          "refId": "B"
        },
        {
          "expr": "sum by (namespace, pod) (kube_pod_status_ready{condition=\"true\", namespace=~\"$namespace\"})",
          "format": "table",
          "instant": true,
          "refId": "C"
        },
        {
          "expr": "sum by (namespace, pod) (kube_pod_container_status_restarts_total{namespace=~\"$namespace\"})",
          "format": "table",
          "instant": true,
          "refId": "D"
        },
        {
          "expr": "max by (namespace, pod) (time() - kube_pod_start_time{namespace=~\"$namespace\"})",
          "format": "table",
          "instant": true,
          "refId": "E"
        },
        {
          "expr": "sum by (namespace, pod) (rate(container_cpu_usage_seconds_total{container!=\"\", namespace=~\"$namespace\"}[2m]))",
          "format": "table",
          "instant": true,
          "refId": "F"
        },
        {
          "expr": "sum by (namespace, pod) (container_memory_working_set_bytes{container!=\"\", namespace=~\"$namespace\"})",
          "format": "table",
          "instant": true,
          "refId": "G"
        },
        {
          "expr": "count by (namespace, pod) (kube_pod_container_info{namespace=~\"$namespace\"})",
          "format": "table",
          "instant": true,
          "refId": "H"
        },
        {
          "expr": "sum by (namespace, pod) (kube_pod_container_status_waiting{namespace=~\"$namespace\"})",
          "format": "table",
          "instant": true,
          "refId": "I"
        }
      ],
      "transformations": [
        { "id": "merge", "options": {} },
        {
          "id": "organize",
          "options": {
            "excludeByName": {
              "Time": true,
              "Value #A": true
            },
            "renameByName": {
              "namespace": "Namespace",
              "pod": "Pod",
              "node": "Node",
              "phase": "Phase",
              "created_by_kind": "Owner Kind",
              "created_by_name": "Owner",
              "Value #B": "_PhaseFlag",
              "Value #C": "Ready",
              "Value #D": "Restarts (Pod lifetime)",
              "Value #E": "Age",
              "Value #F": "CPU",
              "Value #G": "Memory",
              "Value #H": "Containers",
              "Value #I": "Waiting"
            },
            "indexByName": {
              "Namespace": 0,
              "Pod": 1,
              "Phase": 2,
              "Ready": 3,
              "Restarts": 4,
              "Waiting": 5,
              "Containers": 6,
              "CPU": 7,
              "Memory": 8,
              "Age": 9,
              "Node": 10,
              "Owner Kind": 11,
              "Owner": 12,
              "_PhaseFlag": 13
            }
          }
        },
        {
          "id": "organize",
          "options": {
            "excludeByName": { "_PhaseFlag": true },
            "renameByName": {},
            "indexByName": {}
          }
        }
      ],
      "fieldConfig": {
        "defaults": {
          "custom": {
            "align": "auto",
            "filterable": true,
            "inspect": false,
            "cellOptions": { "type": "auto" }
          }
        },
        "overrides": [
          {
            "matcher": { "id": "byName", "options": "Phase" },
            "properties": [
              {
                "id": "custom.cellOptions",
                "value": { "type": "color-background", "mode": "basic" }
              },
              {
                "id": "mappings",
                "value": [
                  {
                    "type": "value",
                    "options": {
                      "Running": {
                        "color": "green",
                        "text": "Running",
                        "index": 0
                      },
                      "Pending": {
                        "color": "yellow",
                        "text": "Pending",
                        "index": 1
                      },
                      "Failed": {
                        "color": "red",
                        "text": "Failed",
                        "index": 2
                      },
                      "Succeeded": {
                        "color": "blue",
                        "text": "Succeeded",
                        "index": 3
                      },
                      "Unknown": {
                        "color": "purple",
                        "text": "Unknown",
                        "index": 4
                      }
                    }
                  }
                ]
              },
              { "id": "custom.width", "value": 110 }
            ]
          },
          {
            "matcher": { "id": "byName", "options": "Ready" },
            "properties": [
              { "id": "custom.cellOptions", "value": { "type": "color-text" } },
              {
                "id": "mappings",
                "value": [
                  {
                    "type": "value",
                    "options": {
                      "0": { "text": "✗", "color": "red", "index": 0 },
                      "1": { "text": "✓", "color": "green", "index": 1 }
                    }
                  }
                ]
              },
              { "id": "custom.width", "value": 70 },
              { "id": "custom.align", "value": "center" }
            ]
          },
          {
            "matcher": { "id": "byName", "options": "Restarts" },
            "properties": [
              { "id": "unit", "value": "none" },
              { "id": "decimals", "value": 0 },
              {
                "id": "custom.cellOptions",
                "value": { "type": "color-background", "mode": "gradient" }
              },
              {
                "id": "thresholds",
                "value": {
                  "mode": "absolute",
                  "steps": [
                    { "color": "green", "value": null },
                    { "color": "yellow", "value": 1 },
                    { "color": "orange", "value": 5 },
                    { "color": "red", "value": 10 }
                  ]
                }
              },
              { "id": "custom.width", "value": 90 }
            ]
          },
          {
            "matcher": { "id": "byName", "options": "Waiting" },
            "properties": [
              { "id": "unit", "value": "none" },
              { "id": "decimals", "value": 0 },
              {
                "id": "custom.cellOptions",
                "value": { "type": "color-background", "mode": "basic" }
              },
              {
                "id": "thresholds",
                "value": {
                  "mode": "absolute",
                  "steps": [
                    { "color": "green", "value": null },
                    { "color": "orange", "value": 1 }
                  ]
                }
              },
              { "id": "custom.width", "value": 80 }
            ]
          },
          {
            "matcher": { "id": "byName", "options": "Containers" },
            "properties": [
              { "id": "unit", "value": "none" },
              { "id": "decimals", "value": 0 },
              { "id": "custom.width", "value": 100 }
            ]
          },
          {
            "matcher": { "id": "byName", "options": "CPU" },
            "properties": [
              { "id": "unit", "value": "none" },
              { "id": "decimals", "value": 3 },
              {
                "id": "custom.cellOptions",
                "value": { "type": "gauge", "mode": "gradient" }
              },
              { "id": "min", "value": 0 },
              { "id": "max", "value": 16 },
              { "id": "custom.width", "value": 140 }
            ]
          },
          {
            "matcher": { "id": "byName", "options": "Memory" },
            "properties": [
              { "id": "unit", "value": "bytes" },
              {
                "id": "custom.cellOptions",
                "value": { "type": "gauge", "mode": "gradient" }
              },
              { "id": "custom.width", "value": 160 }
            ]
          },
          {
            "matcher": { "id": "byName", "options": "Age" },
            "properties": [
              { "id": "unit", "value": "s" },
              { "id": "custom.width", "value": 110 }
            ]
          },
          {
            "matcher": { "id": "byName", "options": "Namespace" },
            "properties": [{ "id": "custom.width", "value": 140 }]
          },
          {
            "matcher": { "id": "byName", "options": "Node" },
            "properties": [{ "id": "custom.width", "value": 100 }]
          },
          {
            "matcher": { "id": "byName", "options": "Owner Kind" },
            "properties": [{ "id": "custom.width", "value": 110 }]
          }
        ]
      },
      "options": {
        "showHeader": true,
        "footer": { "show": false },
        "cellHeight": "sm",
        "sortBy": [{ "displayName": "Namespace", "desc": false }]
      },
      "gridPos": { "h": 14, "w": 24, "x": 0, "y": 6 }
    },
    {
      "id": 3,
      "title": "Top 10 Pod CPU (cores)",
      "type": "timeseries",
      "datasource": { "type": "prometheus", "uid": "prometheus" },
      "targets": [
        {
          "expr": "topk(10, sum by (namespace, pod) (rate(container_cpu_usage_seconds_total{container!=\"\", namespace=~\"$namespace\"}[2m])))",
          "legendFormat": "{{namespace}}/{{pod}}",
          "refId": "A"
        }
      ],
      "fieldConfig": { "defaults": { "unit": "none" } },
      "gridPos": { "h": 9, "w": 24, "x": 0, "y": 28 }
    },
    {
      "id": 4,
      "title": "Top 10 Pod Memory (working set bytes)",
      "type": "timeseries",
      "datasource": { "type": "prometheus", "uid": "prometheus" },
      "targets": [
        {
          "expr": "topk(10, sum by (namespace, pod) (container_memory_working_set_bytes{container!=\"\", namespace=~\"$namespace\"}))",
          "legendFormat": "{{namespace}}/{{pod}}",
          "refId": "A"
        }
      ],
      "fieldConfig": { "defaults": { "unit": "bytes" } },
      "gridPos": { "h": 9, "w": 24, "x": 0, "y": 37 }
    },
    {
      "id": 5,
      "title": "Container restarts (選択期間)",
      "description": "同じ Pod 内でコンテナが再起動した回数。Deployment の rollout による Pod 再作成は含まない。",
      "type": "table",
      "datasource": { "type": "prometheus", "uid": "prometheus" },
      "targets": [
        {
          "expr": "round(sum by (namespace, pod) (increase(kube_pod_container_status_restarts_total{namespace=~\"$namespace\"}[$__range]))) > 0",
          "format": "table",
          "instant": true,
          "refId": "A"
        },
        {
          "expr": "(max by (namespace, pod) (max_over_time(kube_pod_container_status_last_terminated_timestamp{namespace=~\"$namespace\"}[$__range])) * 1000 > 0) and on (namespace, pod) (round(sum by (namespace, pod) (increase(kube_pod_container_status_restarts_total{namespace=~\"$namespace\"}[$__range]))) > 0)",
          "format": "table",
          "instant": true,
          "refId": "B"
        },
        {
          "expr": "(max by (namespace, pod, reason) (last_over_time(kube_pod_container_status_last_terminated_reason{namespace=~\"$namespace\"}[$__range])) > 0) and on (namespace, pod) (round(sum by (namespace, pod) (increase(kube_pod_container_status_restarts_total{namespace=~\"$namespace\"}[$__range]))) > 0)",
          "format": "table",
          "instant": true,
          "refId": "C"
        }
      ],
      "transformations": [
        { "id": "merge", "options": {} },
        {
          "id": "organize",
          "options": {
            "excludeByName": { "Time": true, "Value #C": true },
            "renameByName": {
              "Value #A": "restarts in range",
              "Value #B": "last restart",
              "reason": "last reason"
            }
          }
        }
      ],
      "fieldConfig": {
        "defaults": {},
        "overrides": [
          {
            "matcher": { "id": "byName", "options": "last restart" },
            "properties": [{ "id": "unit", "value": "dateTimeAsLocal" }]
          }
        ]
      },
      "gridPos": { "h": 9, "w": 12, "x": 0, "y": 46 }
    },
    {
      "id": 6,
      "title": "Pod not Ready",
      "type": "table",
      "datasource": { "type": "prometheus", "uid": "prometheus" },
      "targets": [
        {
          "expr": "kube_pod_status_ready{condition=\"true\", namespace=~\"$namespace\"} == 0",
          "format": "table",
          "instant": true,
          "refId": "A"
        }
      ],
      "gridPos": { "h": 9, "w": 12, "x": 12, "y": 46 }
    },
    {
      "id": 30,
      "title": "Exposed Services (LoadBalancer / NodePort)",
      "type": "table",
      "datasource": { "type": "prometheus", "uid": "prometheus" },
      "targets": [
        {
          "expr": "sum by (namespace, service, type) (kube_service_spec_type{type=~\"LoadBalancer|NodePort\", namespace=~\"$namespace\"})",
          "format": "table",
          "instant": true,
          "refId": "A"
        },
        {
          "expr": "sum by (namespace, service, cluster_ip) (kube_service_info{namespace=~\"$namespace\"} * on(namespace, service) group_left() kube_service_spec_type{type=~\"LoadBalancer|NodePort\"})",
          "format": "table",
          "instant": true,
          "refId": "B"
        },
        {
          "expr": "sum by (namespace, service, ip) (kube_service_status_load_balancer_ingress{ip!=\"\", namespace=~\"$namespace\"})",
          "format": "table",
          "instant": true,
          "refId": "C"
        },
        {
          "expr": "sum by (namespace, service, hostname) (kube_service_status_load_balancer_ingress{hostname!=\"\", namespace=~\"$namespace\"})",
          "format": "table",
          "instant": true,
          "refId": "D"
        }
      ],
      "transformations": [
        { "id": "merge", "options": {} },
        {
          "id": "organize",
          "options": {
            "excludeByName": {
              "Time": true,
              "Value #A": true,
              "Value #B": true,
              "Value #C": true,
              "Value #D": true
            },
            "renameByName": {
              "namespace": "Namespace",
              "service": "Service",
              "type": "Type",
              "cluster_ip": "ClusterIP",
              "ip": "External IP",
              "hostname": "External Hostname"
            },
            "indexByName": {
              "Namespace": 0,
              "Service": 1,
              "Type": 2,
              "ClusterIP": 3,
              "External IP": 4,
              "External Hostname": 5
            }
          }
        }
      ],
      "fieldConfig": {
        "defaults": {
          "custom": {
            "align": "auto",
            "filterable": true,
            "inspect": false,
            "cellOptions": { "type": "auto" }
          }
        },
        "overrides": [
          {
            "matcher": { "id": "byName", "options": "Type" },
            "properties": [
              {
                "id": "custom.cellOptions",
                "value": { "type": "color-background", "mode": "basic" }
              },
              {
                "id": "mappings",
                "value": [
                  {
                    "type": "value",
                    "options": {
                      "LoadBalancer": {
                        "color": "green",
                        "text": "LoadBalancer",
                        "index": 0
                      },
                      "NodePort": {
                        "color": "blue",
                        "text": "NodePort",
                        "index": 1
                      }
                    }
                  }
                ]
              },
              { "id": "custom.width", "value": 130 }
            ]
          },
          {
            "matcher": { "id": "byName", "options": "Namespace" },
            "properties": [{ "id": "custom.width", "value": 140 }]
          },
          {
            "matcher": { "id": "byName", "options": "Service" },
            "properties": [{ "id": "custom.width", "value": 220 }]
          },
          {
            "matcher": { "id": "byName", "options": "ClusterIP" },
            "properties": [{ "id": "custom.width", "value": 140 }]
          },
          {
            "matcher": { "id": "byName", "options": "External IP" },
            "properties": [{ "id": "custom.width", "value": 140 }]
          }
        ]
      },
      "options": {
        "showHeader": true,
        "footer": { "show": false },
        "cellHeight": "sm",
        "sortBy": [{ "displayName": "Namespace", "desc": false }]
      },
      "gridPos": { "h": 8, "w": 24, "x": 0, "y": 20 }
    },
    {
      "id": 7,
      "title": "Container restart events (10分窓)",
      "description": "同じ Pod 内で発生したコンテナ再起動の増分。Deployment の rollout による Pod 再作成は含まない。",
      "type": "timeseries",
      "datasource": { "type": "prometheus", "uid": "prometheus" },
      "targets": [
        {
          "expr": "sum by (namespace, pod) (increase(kube_pod_container_status_restarts_total{namespace=~\"$namespace\"}[10m])) > 0",
          "legendFormat": "{{namespace}}/{{pod}}",
          "refId": "A"
        }
      ],
      "fieldConfig": {
        "defaults": {
          "unit": "short",
          "custom": { "drawStyle": "bars", "fillOpacity": 60 }
        }
      },
      "gridPos": { "h": 8, "w": 24, "x": 0, "y": 55 }
    },
    {
      "id": 8,
      "title": "Deployment rollout / spec change events (10分窓)",
      "description": "Deployment generation の変化を表示する。rollout restart、イメージ更新、replica 数変更などの spec 更新を含む。",
      "type": "timeseries",
      "datasource": { "type": "prometheus", "uid": "prometheus" },
      "targets": [
        {
          "expr": "max by (namespace, deployment) (changes(kube_deployment_metadata_generation{namespace=~\"$namespace\"}[10m])) > 0",
          "legendFormat": "{{namespace}}/{{deployment}}",
          "refId": "A"
        }
      ],
      "fieldConfig": {
        "defaults": {
          "unit": "short",
          "custom": { "drawStyle": "bars", "fillOpacity": 60 }
        }
      },
      "gridPos": { "h": 8, "w": 24, "x": 0, "y": 63 }
    }
  ]
}
