k8s_client.py 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from kubernetes.client import api_client
  2. from kubernetes.client.apis import core_v1_api
  3. from kubernetes import client, config
  4. class KubernetesTools(object):
  5. def __init__(self):
  6. self.k8s_url = "https://192.168.199.109:6443" # todo 改成配置项
  7. def get_token(self):
  8. """
  9. 获取token
  10. :return:
  11. """
  12. return "eyJhbGciOiJSUzI1NiIsImtpZCI6IjhTMXExTDJkUXBtMWZnWHE3SGgwVGtlMjdYZlAtQTlHYWdXSWlybmY4VEkifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLTc1bWo1Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiJiZWI1MjhkNC1jMmUxLTRiMjgtYTYyNC1iYWI5NjgzNjg2NTAiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZXJuZXRlcy1kYXNoYm9hcmQ6YWRtaW4tdXNlciJ9.a9TBKjgcEVSI6pDvqqfhq8NBJFcSCD4xEGI9Ne_KXfURXODUAmiM8Ld480BZudFo-RKnaWxZXUUUgQCcqH7pjsV-YrB27Lh4aySjaNfxXt-euecCF17XheP_g7K1p3XP1eS07FP-8KgHeo_p9TZK8PotR9zux5SsExuOAd7VUCLvUeWllPkLrsN6woTbDWnJTNCqfwtP04BOiDCiQpOwqfd8S1bby1lwWWI6pxM0cK1_lPQc6YydB-nB4mQRNe4qCwVx_qG_NTzSiI9uel1m0OVIZg2ungsjTzSInSTyaWjJ8V0T8uovDNTZv0_aC4DHEW7X14xtLY9UiZNy50Nebg"
  13. # with open('k8s-token.txt', 'r') as file:
  14. # Token = file.read().strip('\n')
  15. # return Token
  16. def get_api(self):
  17. """
  18. 获取API的CoreV1Api版本对象
  19. :return:
  20. """
  21. configuration = client.Configuration()
  22. configuration.host = self.k8s_url
  23. configuration.verify_ssl = False
  24. configuration.api_key = {"authorization": "Bearer " + self.get_token()}
  25. client1 = api_client.ApiClient(configuration=configuration)
  26. api = core_v1_api.CoreV1Api(client1)
  27. return api
  28. def get_namespace_list(self):
  29. """
  30. 获取命名空间列表
  31. :return:
  32. """
  33. api = self.get_api()
  34. namespace_list = []
  35. for ns in api.list_namespace().items:
  36. # print(ns.metadata.name)
  37. namespace_list.append(ns.metadata.name)
  38. return namespace_list
  39. def get_pod_info(self, namespaces, pod_name):
  40. """
  41. 查看pod信息
  42. :param namespaces: 命令空间,比如:default
  43. :param pod_name: pod完整名称,比如:flaskapp-1-5d96dbf59b-lhmp8
  44. :return:
  45. """
  46. api = self.get_api()
  47. # 示例参数
  48. namespaces = "airflow"
  49. pod_name = "python-task-1664728976-3572760bf5614ca8889930ec0af323fb"
  50. resp = api.read_namespaced_pod(namespace=namespaces, name=pod_name)
  51. # 详细信息
  52. print(resp)
  53. # def get_pod_logs(self, namespaces, pod_name, tail_lines=200):
  54. # """
  55. # 查看pod日志
  56. # :param namespaces: 命令空间,比如:default
  57. # :param pod_name: pod完整名称,比如:flaskapp-1-5d96dbf59b-lhmp8
  58. # :return:
  59. # """
  60. # api = self.get_api()
  61. # # 示例参数
  62. # # namespaces = "airflow"
  63. # # pod_name = "flaskapp-1-5d96dbf59b-lhmp8"
  64. # """
  65. # pretty美化输出
  66. # tail_lines=200输出最近200行
  67. # """
  68. # log_content = api.read_namespaced_pod_log(pod_name, namespaces, pretty=True, tail_lines=200)
  69. # return log_content
  70. def get_pod_logs(self, namespaces, labels: dict, tail_lines=10000):
  71. """
  72. 查看pod日志
  73. :param namespaces: 命令空间,比如:default
  74. :param pod_name: pod完整名称,比如:flaskapp-1-5d96dbf59b-lhmp8
  75. :return:
  76. """
  77. api = self.get_api()
  78. rets = api.list_namespaced_pod(namespaces, label_selector=','.join([f'{k}={v}' for k, v in labels.items()]),
  79. watch=False)
  80. if len(rets.items) > 0:
  81. pod_name = rets.items[0].to_dict()['metadata']['name']
  82. log_content = api.read_namespaced_pod_log(pod_name, namespaces, pretty=True, tail_lines=tail_lines)
  83. return log_content
  84. else:
  85. return None