如何使用python kubernetes客户端以编程方式控制SparkApplication?

时间:2019-02-14 10:02:27

标签: python apache-spark kubernetes kubernetes-python-client

我想以编程方式从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以编程方式开始这项工作。

有人知道该怎么做吗?

2 个答案:

答案 0 :(得分:0)

这里是提到的示例,说明如何使用kubernetes python客户端在kubernetes上创建第三方资源。

  

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

希望这会有所帮助。

答案 1 :(得分:0)

这可能是您想要的:

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