2023年7月10日发(作者:)
KubernetesPythonAPI中⽂使⽤说明创建管理员⽤户,授权,获取Token创建⽤户vi iVersion: v1kind: ServiceAccountmetadata: name: admin-user namespace: kube-systemkubectl create -f ⽤户授权vi iVersion: /v1beta1kind: ClusterRoleBindingmetadata: name: admin-userroleRef: apiGroup: kind: ClusterRole name: cluster-adminsubjects:- kind: ServiceAccount name: admin-user namespace: kube-systemkubectl create -f 获取Tokenkubectl describe secret $(kubectl get secret -n kube-system | grep ^admin-user | awk '{print $1}') -n kube-system | grep -E '^token'| awk '{print $2}'安装kubernetes python sdk模块安装pip install kubernetes测试demofrom kubernetes import client, configApiToken = "xxxxx" #ApiTokenconfiguration = uration()setattr(configuration, 'verify_ssl', False)_default(configuration) = "xxxx:6443" #_ssl = = _key = {"authorization": "Bearer " + ApiToken}_default(configuration)k8s_api_obj = 1Api(ent(configuration))ret = k8s_api__namespaced_pod("dev")print(ret)接⼝操作案例创建namespacefrom kubernetes import client, configApiToken = "xxxxx" #ApiTokenconfiguration = uration()setattr(configuration, 'verify_ssl', False)_default(configuration) = "xxxx:6443" #_ssl = = _key = {"authorization": "Bearer " + ApiToken}_default(configuration)k8s_api_obj = 1Api(ent(configuration))body = { "apiVersion": "v1", "kind": "Namespace", "metadata": { "name": "test123", }}ret = k8s_api__namespace(body=body)print (ret)删除namespacefrom kubernetes import client, configApiToken = "xxxxx" #ApiTokenconfiguration = uration()setattr(configuration, 'verify_ssl', False)_default(configuration) = "xxxx:6443" #_ssl = = _key = {"authorization": "Bearer " + ApiToken}_default(configuration)k8s_api_obj = 1Api(ent(configuration))body = client.V1DeleteOptions()_version = "v1"_period_seconds = 0ret = k8s_api__namespace("test123", body=body)print(ret)查看namespace列表from import ApiExceptionApiToken = "xxxxx" #ApiTokenconfiguration = uration()setattr(configuration, 'verify_ssl', False)_default(configuration) = "xxxx:6443" #_ssl = = _key = {"authorization": "Bearer " + ApiToken}_default(configuration)k8s_api_obj = 1Api()limit = 56 #返回最⼤值,可选参数可以不写timeout_seconds = 56 #超时时间可选参数watch = False #监听资源,可选参数可以不填try: api_response = k8s_api__namespace(limit=limit,timeout_seconds=timeout_seconds, watch=watch) for namespace in api_: print()except ApiException as e: print("Exception when calling CoreV1Api->list_namespace: %sn" % e)创建podfrom kubernetes import client, configApiToken = "xxxxx" #ApiTokenconfiguration = uration()setattr(configuration, 'verify_ssl', False)_default(configuration) = "xxxx:6443" #_ssl = = _key = {"authorization": "Bearer " + ApiToken}_default(configuration)k8s_api_obj = 1beta2Api(ent(configuration))body=eval("{'kind': 'Deployment', 'spec': {'replicas': 1, 'template': {'spec': {'containers': [{'image': 'nginx:1.7.9', 'name': 'nginx', 'ports': [{'containerPort': 80}]}]}, 'metadata': {'labels': {'app': 'nginx-deployment'}}}, 'selector': {'matchLabels': {'app': 'nginx-deployment'}}}, 'apiVersion': 'apps/v1beta2', 'metadata': {'labels': {'app': 'nginx-deployment'}, 'namespace': 'default', 'name': 'nginx-deployment'}}")resp = k8s_api__namespaced_deployment(body=body, namespace="default")print(resp)删除podfrom kubernetes import client, configApiToken = "xxxxx" #ApiTokenconfiguration = uration()setattr(configuration, 'verify_ssl', False)_default(configuration) = "xxxx:6443" #_ssl = = _key = {"authorization": "Bearer " + ApiToken}_default(configuration)k8s_api_obj = 1beta2Api(ent(configuration))resp = k8s_api__namespaced_deployment(name="nginx-deployment", namespace="default", body=client.V1DeleteOptions( propagation_policy='Foreground', grace_period_seconds=0) )print(resp)更新podfrom kubernetes import client, configApiToken = "xxxxx" #ApiTokenconfiguration = uration()setattr(configuration, 'verify_ssl', False)_default(configuration) = "xxxx:6443" #_ssl = = _key = {"authorization": "Bearer " + ApiToken}_default(configuration)k8s_api_obj = 1beta2Api(ent(configuration))body=eval("{'kind': 'Deployment', 'spec': {'replicas': 1, 'template': {'spec': {'containers': [{'image': 'nginx', 'name': 'nginx', 'ports': [{'containerPort': 80}]}]}, 'metadata': {'labels': {'app': 'nginx-deployment'}}}, 'selector': {'matchLabels': {'app': 'nginx-deployment'}}}, 'apiVersion': 'apps/v1beta2', 'metadata':
{'labels': {'app': 'nginx-deployment'}, 'namespace': 'default', 'name': 'nginx-deployment'}}")resp = k8s_api__namespaced_deployment( name="nginx-deployment", namespace="default", body=body )print(resp)查询podfrom kubernetes import client, configfrom import ApiExceptionApiToken = "xxxxx" #ApiTokenconfiguration = uration()setattr(configuration, 'verify_ssl', False)_default(configuration) = "xxxx:6443" #_ssl = = _key = {"authorization": "Bearer " + ApiToken}_default(configuration)k8s_api_obj = 1Api()resp = k8s_api__namespaced_pod("default", label_selector="app=" + "nginx-deployment")print(resp)创建svcfrom kubernetes import client, configfrom import ApiExceptionApiToken = "xxxxx" #ApiTokenconfiguration = uration()setattr(configuration, 'verify_ssl', False)_default(configuration) = "xxxx:6443" #_ssl = = _key = {"authorization": "Bearer " + ApiToken}_default(configuration)k8s_api_obj = 1Api()namespace = "default"body = {'apiVersion': 'v1', 'kind': 'Service', 'metadata': {'name': 'nginx-service', 'labels': {'app': 'nginx'}}, 'spec': {'ports': [{'port': 80, 'targetPort': 80}], 'selector': {'app': 'nginx'}}}try: api_response = k8s_api__namespaced_service(namespace , body) print(api_response)except ApiException as e: print("Exception when calling CoreV1Api->create_namespaced_service: %sn" % e)删除svcfrom kubernetes import client, configfrom import ApiExceptionApiToken = "xxxxx" #ApiTokenconfiguration = uration()setattr(configuration, 'verify_ssl', False)_default(configuration) = "xxxx:6443" #_ssl = = _key = {"authorization": "Bearer " + ApiToken}_default(configuration)k8s_api_obj = 1Api()name = 'nginx-service' #要删除svc名称namespace = 'default' #命名空间grace_period_seconds = 0 #延迟时间,0⽴即删除body = client.V1DeleteOptions() #删除选项try: api_response = k8s_api__namespaced_service(name, namespace,body, grace_period_seconds=grace_period_seconds) print(api_response)except ApiException as e: print("Exception when calling CoreV1Api->delete_namespaced_service: %sn" % e)pod列表from kubernetes import client, configfrom import ApiExceptionApiToken = "xxxxx" #ApiTokenconfiguration = uration()setattr(configuration, 'verify_ssl', False)_default(configuration) = "xxxx:6443" #_ssl = = _key = {"authorization": "Bearer " + ApiToken}_default(configuration)k8s_api_obj = 1beta2Api(ent(configuration))namespace = 'dev' #命名空间try: api_response = k8s_api__namespaced_deployment(namespace) for deployment in api_: print()except ApiException as e: print("Exception when calling AppsV1beta2Api->list_namespaced_deployment: %sn" % e)创建ConfigMapfrom kubernetes import client, configApiToken = "xxxxx" #ApiTokenconfiguration = uration()setattr(configuration, 'verify_ssl', False)_default(configuration) = "xxxx:6443" #_ssl = = _key = {"authorization": "Bearer " + ApiToken}_default(configuration)k8s_api_obj = 1Api()body = { 'apiVersion': 'v1', 'kind': 'ConfigMap', 'metadata': { 'name': 'filebeat-configmap', 'namespace': 'default' }, 'data': { '': 'ctors: n - input_type: log n paths: n - "/mnt/*/logs/app/"n tags: ["json"] n _under_root: true n ite_keys: true csearch: n hosts: [":9200"] n username: "elastic"n password: "elastic"n d: false n index: "dev_namespace_name_java_log-%{+}"n '}
}resp = k8s_api__namespaced_config_map( body=body, namespace="default")print(resp)删除ConfigMapfrom kubernetes import client, configApiToken = "xxxxx" #ApiTokenconfiguration = uration()setattr(configuration, 'verify_ssl', False)_default(configuration) = "xxxx:6443" #_ssl = = _key = {"authorization": "Bearer " + ApiToken}_default(configuration)k8s_api_obj = 1Api()resp = k8s_api__namespaced_config_map( name="filebeat-configmap", namespace="default", body=client.V1DeleteOptions() )print(resp)查看ConfigMapfrom kubernetes import client, configfrom import ApiExceptionApiToken = "xxxxx" #ApiTokenconfiguration = uration()setattr(configuration, 'verify_ssl', False)_default(configuration) = "xxxx:6443" #_ssl = = _key = {"authorization": "Bearer " + ApiToken}_default(configuration)k8s_api_obj = 1Api()namespace = 'default'try: api_response = k8s_api__namespaced_config_map(namespace) for config_map in api_: print(config_)except ApiException as e: print("Exception when calling CoreV1Api->list_namespaced_config_map: %sn" % e)获取Node节点信息from kubernetes import client, configfrom import ApiExceptionApiToken = "xxxxx" #ApiTokenconfiguration = uration()setattr(configuration, 'verify_ssl', False)_default(configuration) = "xxxx:6443" #_ssl = = _key = {"authorization": "Bearer " + ApiToken}_default(configuration)k8s_api_obj = 1Api()exact = Trueexport = Truename = "192.168.1.50" #此处填写node名称try: api_response = k8s_api__node(name, exact=exact, export=export) print(api_response)except ApiException as e: print("Exception when calling CoreV1Api->read_node: %sn" % e)获取Node状态信息from kubernetes import client, configfrom import ApiExceptionApiToken = "xxxxx" #ApiTokenconfiguration = uration()setattr(configuration, 'verify_ssl', False)_default(configuration) = "xxxx:6443" #_ssl = = _key = {"authorization": "Bearer " + ApiToken}_default(configuration)name = "192.168.1.50" #此处填写node名称k8s_api_obj = 1Api()try: api_response = k8s_api__node_status(name, pretty=True) print(api_response)except ApiException as e: print("Exception when calling CoreV1Api->read_node_status: %sn"
发布者:admin,转转请注明出处:http://www.yc00.com/web/1688932970a185022.html
评论列表(0条)