In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how python calls kubernetesAPI". In daily operation, I believe many people have doubts about how python calls kubernetesAPI. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how python calls kubernetesAPI". Next, please follow the editor to study!
Foreword:
K8s also provides API interface, which is provided by the apiserver component of the management node, and the apiserver service is responsible for providing HTTP API so that users and other components can communicate with each other. Client library
Installation
Pip install kubernetes-I https://pypi.douban.com/simple
K8s authentication method:
HTTPS Certificate Authentication: digital Certificate Authentication based on CA Certificate signature
HTTP Token authentication: identify users through a Token
HTTPS Certificate Certification (kubeconfig)
Import osfrom kubernetes import client, configconfig.load_kube_config (file_path) # specify kubeconfig configuration file apps_api = client.AppsV1Api () # Resource interface class instantiates for dp in apps_api.list_deployment_for_all_namespaces () .items: print (dp)
HTTP Token Certification (ServiceAccount)
From kubernetes import client, configconfiguration = client.Configuration () configuration.host = "https://192.168.3.201:16443" # APISERVER address configuration.ssl_ca_cert=" # CA certificate / etc/kubernetes/pki/ca.crtconfiguration.verify_ssl = True # enable certificate verification configuration.api_key = {"authorization": "Bearer" + token} # specify the Token string client.Configuration.set_default (configuration) apps_api = client.AppsV1Api ()
For these two certifications, choose one from two.
Get the Token string: create a service account and bind the default cluster-admin administrator cluster role:
Create a user:
$kubectl create serviceaccount dashboard-admin-n kube-system
User authorization:
$kubectl create clusterrolebinding dashboard-admin-clusterrole=cluster-admin-serviceaccount=kube-system:dashboard-admin
Get the user Token:
$kubectl describe secrets-n kube-system $(kubectl-n kube-system get secret | awk'/ dashboard-admin/ {print $1}')
Other common resource interface classes are instantiated:
Core_api = client.CoreV1Api () # namespace,pod,service,pv,pvcapps_api = client.AppsV1Api () # deploymentnetworking_api = client.NetworkingV1beta1Api () # ingressstorage_api = client.StorageV1Api () # storage_class
For instance
Deployment operation:
# you must have the above authentication before the following code # create namespace = "default" name = "api-test" replicas = 3labels = {'nginx':'true'} # No distinction between data types Quote image = "nginx" body = client.V1Deployment (api_version= "apps/v1", kind= "Deployment", metadata=client.V1ObjectMeta (name=name), spec=client.V1DeploymentSpec (replicas=replicas, selector= {'matchLabels': labels}, template=client.V1PodTemplateSpec (metadata=client.V1ObjectMeta (labels=labels)) Spec=client.V1PodSpec (containers= [client.V1Container (name= "web", image=image)]),)) try: apps_api.create_namespaced_deployment (namespace=namespace Body=body) except Exception as e: status = getattr (e, "status") if status = = 400: print (e) print ("format error") elif status = = 403: print ("No permission") # Delete name= "api-test" apps_api.delete_namespaced_deployment (namespace=namespace, name=name)
But in fact, this API is quite round, an object that creates deployment, where there are more than N classes.
At this point, the study on "how to call kubernetesAPI by python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.