使用 python 收集 kubernetes 集群的 events 并写入 elasticsearch
2021/4/28 20:28:44
本文主要是介绍使用 python 收集 kubernetes 集群的 events 并写入 elasticsearch,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
from kubernetes import client, config, watch from elasticsearch import Elasticsearch import arrow import sys import requests import json dingding_webhook = "https://oapi.dingtalk.com/robot/send?access_token=xxxxx" hosts = [ '172.16.21.39:9200', '172.16.21.40:9200', '172.16.21.41:9200' ] def send_text(content): data = { "msgtype": "text", "text": { "content": content } } requests.post(url=dingding_webhook, json=data) now = arrow.now('Asia/Shanghai') index = "kube-events" index_today = "{}.{}".format(index, now.format("YYYY.MM.DD")) cluster_name = sys.argv[1] kube_config = sys.argv[2] with Elasticsearch(hosts=hosts, timeout=120) as es: if not es.indices.exists(index=index_today): es.indices.create(index=index_today) # Configs can be set in Configuration class directly or using helper utility config.load_kube_config(config_file=kube_config) v1 = client.CoreV1Api() w = watch.Watch() try: for event in w.stream(v1.list_event_for_all_namespaces): doc = { "cluster_name": cluster_name, "time_iso8601": arrow.Arrow.fromdatetime(event['object'].metadata.creation_timestamp).isoformat(), "namespace": event['object'].metadata.namespace, "type": event['object'].type, "reason": event['object'].reason, "message": event['object'].message } # print(json.dumps(doc)) # if doc["type"] == "Warning": # send_text('[ {} ]- {}'.format(cluster_name, doc["message"])) if es.exists(index=index_today, doc_type=index, id=event['object'].metadata.uid): continue es.create(index=index_today, doc_type=index, body=doc, id=event['object'].metadata.uid) except Exception as e: pass w.stop()
使用 supervisord 将程序后台运行。
[program:ali-14] command = /usr/local/bin/python3.6 /ops/scripts/kube_events.py ali-14 /ops/k8s_config/ali-14 autorestart = true redirect_stderr = true stdout_logfile = /ops/logs/ali-14.log stopasgroup = true [program:m7] command = /usr/local/bin/python3.9 /ops/scripts/kube_events.py m7 /ops/k8s_config/m7 autorestart = true redirect_stderr = true stdout_logfile = /ops/logs/m7.log stopasgroup = true [program:cm] command = /usr/local/bin/python3.9 /ops/scripts/kube_events.py cm /ops/k8s_config/cm autorestart = true redirect_stderr = true stdout_logfile = /ops/logs/cm.log stopasgroup = true [program:meteo] command = /usr/local/bin/python3.6 /ops/scripts/kube_events.py meteo /ops/k8s_config/meteo autorestart = true redirect_stderr = true stdout_logfile = /ops/logs/meteo.log stopasgroup = true
写入 ES 之后就可以通过 kibana 查看了。
这篇关于使用 python 收集 kubernetes 集群的 events 并写入 elasticsearch的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-29一文搞定 KubeKey 3.1.1 离线部署 KubeSphere 3.4.1 和 Kubernetes v1.28
- 2024-05-29云原生周刊:K8s 上的 gRPC 名称解析和负载平衡
- 2024-05-08云原生周刊:Kubernetes v1.30 发布 | 2024.4.22
- 2024-05-08云原生周刊:K8s 中的服务和网络 | 2024.4.29
- 2024-03-29kubernetes crashloopbackoff
- 2024-03-13ubuntu install kubernetes
- 2024-03-07k8s coredns containercreating
- 2024-03-04k8s for dummies
- 2024-03-01pulumi kubernetes operator
- 2024-02-06云原生周刊:K8s 1.26 到 1.29 版本的更新 | 2024.1.29