🔒 Kubernetes deployment & pod monitoring with Telegram alerts

346 views · 🔒 SecOps & Security Automation

Description

SETUP INSTRUCTIONS

1. Configure Kubeconfig

Example kubeconfig format:

apiVersion: v1
kind: Config
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUd...
    server: https://your-cluster.example.com:6443
  name: your-cluster
contexts:
- context:
    cluster: your-cluster
    user: your-user
  name: your-context
current-context: your-context
users:
- name: your-user
  user:
    token: eyJhbGciOiJSUzI1...

2. Telegram Configuration

3. Schedule Configuration

4. kubectl Installation

HOW IT WORKS

Workflow Steps

  1. Schedule Trigger

    • Runs automatically based on configured interval
    • Initiates the monitoring cycle
  2. Kubeconfig Setup

    • Loads the kubeconfig and namespace configuration
    • Passes credentials to kubectl commands
  3. Parallel Data Collection

    • Get Pods: Fetches all pods from the specified namespace
    • Get Deployments: Fetches all deployments from the specified namespace
    • Both commands run in parallel for efficiency
  4. Process & Generate Report

    • Parses pod and deployment data
    • Groups pods by their owner (Deployment, DaemonSet, StatefulSet, or Node)
    • Calculates readiness statistics for each workload
    • Detects alerts: workloads with 0 ready pods
    • Generates a comprehensive Markdown report including:
      • Deployments with replica counts and pod details
      • Other workloads (DaemonSets, StatefulSets, Static Pods)
      • Standalone pods (if any)
      • Pod-level details: status, node, restart count
  5. Has Alerts?

    • Checks if any workloads have 0 ready pods
    • Routes to appropriate action
  6. Send Telegram Alert (if alerts exist)

    • Sends formatted alert message to Telegram
    • Includes:
      • Namespace information
      • List of problematic workloads
      • Full status report
  7. Save Report

    • Saves the Markdown report to a file
    • Filename format: k8s-report-YYYY-MM-DD-HHmmss.md
    • Always executes, regardless of alert status

Security Features

Alert Logic

Alerts are triggered when any workload has zero ready pods:

Report Sections

  1. Deployments: All Deployment-managed pods (via ReplicaSets)
  2. Other Workloads: DaemonSets, StatefulSets, and Static Pods (kube-system components)
  3. Standalone Pods: Pods without recognized owners (rare)
  4. Alerts: Summary of workloads requiring attention

KEY FEATURES

TROUBLESHOOTING

Issue: “Cannot read properties of undefined”

Issue: No alerts when there should be

Issue: Telegram message not sent

Issue: kubectl download fails

CUSTOMIZATION

Change Alert Threshold

Edit the Process & Generate Report node to change when alerts trigger:

// Change from "< 1" to your desired threshold
if (readyReplicas < 2) {  // Alert if less than 2 ready pods
  alerts.push({...});
}

Monitor Multiple Namespaces

Custom Report Format

Edit the markdown generation in Process & Generate Report node to customize:

Additional Notification Channels

Add nodes after “Has Alerts?” to send notifications via:

🔗 Nodes Used

Write Binary File, Telegram, Schedule Trigger

📥 Import

Download workflow.json and import into n8n: Workflow menu → Import from File

📖 Importing guide · 🔑 Credential setup