python - 即使使用流 api 后,也无法在 Python 中使用 kubernetes-cli 在 pod 中执行

标签 python python-3.x kubernetes kubectl kubernetes-python-client

我正在关注official documentation令我惊讶的是,这个请求根本行不通。

我尝试阅读多个问题和答案,但没有成功。我已设置要使用的 stream api,但错误并未消失。

我的代码是:

from __future__ import print_function
import time
import kubernetes.client
from kubernetes.client.rest import ApiException
from pprint import pprint
from kubernetes import client, config, stream
stream = stream.stream

# Configure API key authorization: BearerToken
configuration = kubernetes.client.Configuration()
configuration.host = "https://my_aws_server.amazonaws.com"
configuration.verify_ssl = False
configuration.api_key['authorization'] = "some_token"
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
configuration.api_key_prefix['authorization'] = 'Bearer'
configuration.debug = True

api_instance = kubernetes.client.CoreV1Api(
    kubernetes.client.ApiClient(configuration))
name = 'jupyter-ankita'  # str | name of the PodExecOptions
namespace = 'jhub'  # str | object name and auth scope, such as for teams and projects
# str | Command is the remote command to execute. argv array. Not executed within a shell. (optional)
command = 'echo "hail aviral"'
# bool | Redirect the standard error stream of the pod for this call. Defaults to true. (optional)
stderr = True
# bool | Redirect the standard input stream of the pod for this call. Defaults to false. (optional)
stdin = True
# bool | Redirect the standard output stream of the pod for this call. Defaults to true. (optional)
stdout = True
# bool | TTY if true indicates that a tty will be allocated for the exec call. Defaults to false. (optional)
tty = True

try:
    api_response = stream(api_instance.connect_post_namespaced_pod_exec(
        name, namespace, command=command, stderr=stderr, stdin=stdin, stdout=stdout)
    )
    pprint(api_response)
except ApiException as e:
    print("Exception when calling CoreV1Api->connect_post_namespaced_pod_exec: %s\n" % e)

我希望运行该命令,但我遇到了错误:

Exception when calling CoreV1Api->connect_post_namespaced_pod_exec: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Audit-Id': '88df8863-61b1-4fe7-9d39-d0e6059ea993', 'Content-Type': 'application/json', 'Date': 'Tue, 28 May 2019 14:04:38 GMT', 'Content-Length': '139'})
HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Upgrade request required","reason":"BadRequest","code":400}

我的 SSL 客户端已更新,我在 MacOS 上使用 brew 中的 Python3.7。

也在 Ubuntu 上使用,同样的错误。

版本是:

Client Version: version.Info{Major:"1", Minor:"12", GitVersion:"v1.12.3", GitCommit:"some_no", GitTreeState:"clean", BuildDate:"2018-11-27T01:14:37Z", GoVersion:"go1.11.2", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"12+", GitVersion:"v1.12.6-eks-d69f1b", GitCommit:"some_no", GitTreeState:"clean", BuildDate:"2019-02-28T20:26:10Z", GoVersion:"go1.10.8", Compiler:"gc", Platform:"linux/amd64"}
  • 更新: 我将我的功能更改为:
api_response = stream( api_instance.connect_post_namespaced_pod_exec, name, namespace, command=command, stderr=stderr, stdin=stdin, stdout=stdout )

并且它能够执行。但是,现在我面临以下错误:

('rpc error: code = 2 desc = oci runtime error: exec failed: '
 'container_linux.go:262: starting container process caused "exec: \\"echo '
 '\\\\\\"hail aviral\\\\\\"\\": executable file not found in $PATH"\n'
 '\r\n')

它表示 exec 失败。

最佳答案

缺少远程命令执行的入口点。 试试这个:

command = [
    '/bin/sh',
    '-c',
    'echo hail aviral']

关于python - 即使使用流 api 后,也无法在 Python 中使用 kubernetes-cli 在 pod 中执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56344970/

相关文章:

regex - 我如何在Python中使用F字符串和正则表达式

python : How to find block comments string in Python

python - 如何显示 1 列的值相对于 Python 中 Excel 工作表中某些其他列中存在的特定值?

python - 如何查找列表中嵌套列表的数量?

python - python 中与本地和全局库的相对导入

kubernetes - 我可以使用什么 kubectl 命令来获取按特定字段排序的事件并仅打印事件的特定详细信息?

Kubernetes 入口 Controller 暴露到特定端口

kubernetes - 如何找到用于 gitlab 集成的 gcloud Kubernetes api url

python - 在 macOS 上编译时输入字段不可编辑 + 不稳定的箭头

python - bisect_left 在列表中列表的第一项上,Python 3