gpt4 book ai didi

kubernetes - 在Kubernetes中以编程方式运行作业并从container命令获取输出?

转载 作者:行者123 更新时间:2023-12-02 11:36:51 26 4
gpt4 key购买 nike

在查看Kubernetes API的客户端库时,我找不到一个选项来检索容器内命令执行返回的输出。如果我们以下面的python代码示例为例,是否可以获取container start命令返回的实际输出?

# Copyright 2016 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Creates, updates, and deletes a job object.
"""

from os import path

import yaml

from kubernetes import client, config

JOB_NAME = "pi"


def create_job_object():
# Configureate Pod template container
container = client.V1Container(
name="pi",
image="perl",
command=["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"])
# Create and configurate a spec section
template = client.V1PodTemplateSpec(
metadata=client.V1ObjectMeta(labels={"app": "pi"}),
spec=client.V1PodSpec(restart_policy="Never", containers=[container]))
# Create the specification of deployment
spec = client.V1JobSpec(
template=template,
backoff_limit=4)
# Instantiate the job object
job = client.V1Job(
api_version="batch/v1",
kind="Job",
metadata=client.V1ObjectMeta(name=JOB_NAME),
spec=spec)

return job


def create_job(api_instance, job):
api_response = api_instance.create_namespaced_job(
body=job,
namespace="default")
print("Job created. status='%s'" % str(api_response.status))


def update_job(api_instance, job):
# Update container image
job.spec.template.spec.containers[0].image = "perl"
api_response = api_instance.patch_namespaced_job(
name=JOB_NAME,
namespace="default",
body=job)
print("Job updated. status='%s'" % str(api_response.status))


def delete_job(api_instance):
api_response = api_instance.delete_namespaced_job(
name=JOB_NAME,
namespace="default",
body=client.V1DeleteOptions(
propagation_policy='Foreground',
grace_period_seconds=5))
print("Job deleted. status='%s'" % str(api_response.status))


def main():
# Configs can be set in Configuration class directly or using helper
# utility. If no argument provided, the config will be loaded from
# default location.
config.load_kube_config()
batch_v1 = client.BatchV1Api()
# Create a job object with client-python API. The job we
# created is same as the `pi-job.yaml` in the /examples folder.
job = create_job_object()

create_job(batch_v1, job)

update_job(batch_v1, job)

delete_job(batch_v1)


if __name__ == '__main__':
main()


我只附加了Python客户端库中的示例代码,但是我有一个特定的任务,在这项任务完成后,我需要实际的输出。我需要在API响应中输出该信息,因为我需要以编程方式对其进行访问。

所以基本上:
  • 在Python中运行作业
  • 工作完成
  • 从作业内部的容器获取输出(从启动命令输出)
  • 最佳答案

    作业完成后,此代码将打印日志(stdout输出):

    from kubernetes import client, config


    JOB_NAMESPACE = "default"
    JOB_NAME = "pi"


    def main():
    config.load_kube_config()

    batch_v1 = client.BatchV1Api()

    job_def = batch_v1.read_namespaced_job(name=JOB_NAME, namespace=JOB_NAMESPACE)
    controllerUid = job_def.metadata.labels["controller-uid"]

    core_v1 = client.CoreV1Api()

    pod_label_selector = "controller-uid=" + controllerUid
    pods_list = core_v1.list_namespaced_pod(namespace=JOB_NAMESPACE, label_selector=pod_label_selector, timeout_seconds=10)

    # Notice that:
    # - there are more parameters to limit size, lines, and more - see
    # https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#read_namespaced_pod_log
    # - the logs of the 1st pod are returned (similar to `kubectl logs job/<job-name>`)
    # - assumes 1 container in the pod
    pod_name = pods_list.items[0].metadata.name
    try:
    # For whatever reason the response returns only the first few characters unless
    # the call is for `_return_http_data_only=True, _preload_content=False`
    pod_log_response = core_v1.read_namespaced_pod_log(name=pod_name, namespace=JOB_NAMESPACE, _return_http_data_only=True, _preload_content=False)
    pod_log = pod_log_response.data.decode("utf-8")
    print(pod_log)
    except client.rest.ApiException as e:
    print("Exception when calling CoreV1Api->read_namespaced_pod_log: %s\n" % e)


    if __name__ == '__main__':
    main()

    关于kubernetes - 在Kubernetes中以编程方式运行作业并从container命令获取输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58835060/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com