如何使用python kubernetes客户机以编程方式控制spark应用程序?

2024-04-24 03:14:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从python以编程方式向Kubernetes集群提交一个SparkApplication。在

像这样的作业定义job.yaml

apiVersion: sparkoperator.k8s.io/v1beta1
kind: SparkApplication
metadata:
  name: my-test
  namespace: default
spec:
  sparkVersion: "2.4.0"
  type: Python
...

使用kubectl apply -f job.yaml运行时没有问题,但我无法确定是否以及如何使用kubernetes-client以编程方式启动此作业。在

有人知道怎么做吗?在


Tags: ioyaml定义编程作业方式job集群
2条回答

下面是所提到的示例,如何使用kubernetes python客户机在kubernetes上创建第三方资源。在

https://github.com/kubernetes-client/python/blob/master/examples/create_thirdparty_resource.md

希望这有帮助。在

这可能就是你要找的:

from __future__ import print_function
import time
import kubernetes.client
from kubernetes.client.rest import ApiException
from pprint import pprint

# Configure API key authorization: BearerToken
configuration = kubernetes.client.Configuration()
configuration.api_key['authorization'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['authorization'] = 'Bearer'

# create an instance of the API class
api_instance = kubernetes.client.CustomObjectsApi(kubernetes.client.ApiClient(configuration))
group = 'group_example' # str | The custom resource's group name
version = 'version_example' # str | The custom resource's version
namespace = 'namespace_example' # str | The custom resource's namespace
plural = 'plural_example' # str | The custom resource's plural name. For TPRs this would be lowercase plural kind.
body = NULL # object | The JSON schema of the Resource to create.
pretty = 'pretty_example' # str | If 'true', then the output is pretty printed. (optional)

try: 
    api_response = api_instance.create_namespaced_custom_object(group, version, namespace, plural, body, pretty=pretty)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling CustomObjectsApi->create_namespaced_custom_object: %s\n" % e)

https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CustomObjectsApi.md#create_namespaced_custom_object

相关问题 更多 >