Monitor Kubernetes with Zabbix Using Python and Zabbix Sender
This guide shows how to retrieve the Kubernetes API endpoint, generate an authentication token, deploy a Python script, configure Zabbix host and external check, and manually trigger the monitoring to collect cluster metrics via Zabbix Sender.
Overview
The article explains a step‑by‑step method for monitoring a Kubernetes (K8s) cluster through Zabbix by using a Python script that calls the K8s API with the requests library and sends the results to Zabbix via zabbix_sender.
Step 1 – Obtain the Kubernetes API address
Run the following command on the K8s master node (executing on a worker node may return an outdated address): kubectl cluster-info The command output provides the API server URL, which will be used by the Python script.
Step 2 – Generate a service‑account token on the master
Create a token that the script can use for authentication. Execute on the master host:
kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')Save the token value to a local file; later it will be inserted into the Python script.
Step 3 – Deploy the Python monitoring script
Create a directory under Zabbix external scripts, e.g. /usr/local/zabbix/share/zabbix/external_scripts/k8s, and place the get_k8s.py script there.
Modify three placeholders in the script:
Insert the API server URL obtained in Step 1.
Insert the token from Step 2.
Adjust any cluster‑specific parameters (namespace, resource types, etc.).
Step 4 – Create a Zabbix host and link the script
Add a new host in Zabbix with the exact name k8s_master (the host name must match this value, otherwise Zabbix cannot retrieve the data).
Configure an external check item that calls the script, for example:
/usr/local/zabbix/share/zabbix/external_scripts/k8s/get_k8s.pyStep 5 – Manually trigger the check
Select the newly created monitoring item in the Zabbix UI and click “Check now”. The script contacts the K8s API, gathers the desired metrics, and sends them back to Zabbix via zabbix_sender.
Successful execution is shown in Zabbix as the collected data, confirming that the API query worked.
Key Files and Commands
kubectl -n kube-system describe secret …– retrieve token. get_k8s.py – Python script that uses requests to call the API. zabbix_sender – sends metric data to the Zabbix server.
After completing these steps, Zabbix will continuously monitor the Kubernetes cluster through the custom script.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
