标签: kubernetes-python-client

气流 k8s 运营商 xcom - 握手状态 403 禁止

当我在 Airflow 1.10 版中使用KubernetesPodOperator运行docker 镜像

一旦 pod 成功完成任务,airflow 会尝试通过 k8s 流客户端连接到 pod 来获取 xcom 值。

以下是我遇到的错误:

[2018-12-18 05:29:02,209] {{models.py:1760}} ERROR - (0)
Reason: Handshake status 403 Forbidden
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/kubernetes/stream/ws_client.py", line 249, in websocket_call
    client = WSClient(configuration, get_websocket_url(url), headers)
  File "/usr/local/lib/python3.6/site-packages/kubernetes/stream/ws_client.py", line 72, in __init__
    self.sock.connect(url, header=header)
  File "/usr/local/lib/python3.6/site-packages/websocket/_core.py", line 223, in connect
    self.handshake_response = handshake(self.sock, *addrs, **options)
  File "/usr/local/lib/python3.6/site-packages/websocket/_handshake.py", line 79, in handshake
    status, resp = _get_resp_headers(sock)
  File "/usr/local/lib/python3.6/site-packages/websocket/_handshake.py", line 152, …
Run Code Online (Sandbox Code Playgroud)

kubernetes airflow kubernetes-python-client apache-airflow-xcom

8
推荐指数
1
解决办法
1685
查看次数

Kubernetes python客户端:身份验证问题

我们正在使用kubernetes python客户端(4.0.0)与google的kubernetes引擎(master + nodepools运行k8s 1.8.4)一起定期调度kubernetes上的工作负载.我们用于创建pod,附加到日志和报告pod的结束状态的脚本的简化版本如下所示:

config.load_kube_config(persist_config=False)
v1 = client.CoreV1Api()
v1.create_namespaced_pod(body=pod_specs_dict, namespace=args.namespace)
logging_response = v1.read_namespaced_pod_log(
    name=pod_name,
    namespace=args.namespace,
    follow=True,
    _preload_content=False
)
for line in logging_response:
    line = line.rstrip()
    logging.info(line)
status_response = v1.read_namespaced_pod_status(pod_name, namespace=args.namespace)
print("Pod ended in status: {}".format(status_response.status.phase))
Run Code Online (Sandbox Code Playgroud)

一切都很好,但是我们遇到了一些身份验证问题.身份验证通过默认的gcp身份验证提供程序进行,我通过kubectl container cluster get-credentials在调度程序上手动运行来获取初始访问令牌.在某些随机时间范围内,某些API调用会导致API服务器发出401响应.我的猜测是,只要访问令牌过期,脚本就会尝试获取新的访问令牌.但是,在调度程序上并发运行多个脚本,导致多次获取新的API密钥,其中只有一个仍然有效.我尝试了多种方法来解决问题(使用persist_config=True,重新加载配置后重试401,...)没有任何成功.由于我不完全清楚gcp身份验证和kubernetes python客户端配置是如何工作的(并且两者的文档都相当稀缺),所以我有点不知所措.

我们应该使用其他身份验证方法而不是gcpauth-provider吗?这是kubernetes python客户端中的错误吗?我们应该使用多个配置文件吗?

kubernetes google-kubernetes-engine kubernetes-python-client

6
推荐指数
2
解决办法
2875
查看次数

我在哪里可以获得Kubernetes安装中的API_KEY

一直在尝试让kubernetes的python客户端正常工作。有好运气与config.load_kube_config() 现在,我想使用Python客户端从既不具有远程机器kubectl,也不~/.kube/config就可以了。试图Python客户端API参考诸如

片段:

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'
Run Code Online (Sandbox Code Playgroud)

乍一看,我似乎需要使用默认的service-account-token,它表示:

configuration.api_key['authorization'] = 'YOUR_API_KEY

很快看到很多 [SSL: CERTIFICATE_VERIFY_FAILED]

有人可以提示我在哪里可以从新安装中获得此文件吗?使用kubeadm来建立该集群。

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.2", GitCommit:"5fa2db2bd46ac79e5e00a4e6ed24191080aa463b", GitTreeState:"clean", BuildDate:"2018-01-18T21:10:44Z", GoVersion:"go1.9.2", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"9", …
Run Code Online (Sandbox Code Playgroud)

kubernetes kubernetes-python-client

6
推荐指数
2
解决办法
1958
查看次数

Kubernetes Python 客户端连接问题

我已经设置了官方 Kubernetes Python 客户端(https://github.com/kubernetes-client/python)。

我正在运行 Python 代码以连接到 Kubernetes 集群,但它抛出了一个错误

urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='****', port=6443): Max retries exceeded with url: /api/v1/namespaces/default/pods (Caused by NewConnectionError(': Failed to build a new连接:[Errno 110] 连接超时',))

安装要求:

cachetools        3.1.0 ,
certifi           2019.3.9 ,
chardet           3.0.4 , 
google-auth       1.6.3 ,
idna              2.8 ,
ipaddress         1.0.22 ,
kubernetes        10.0.0-snapshot ,
oauthlib          3.0.1 ,
pip               19.0.3 ,
pyasn1            0.4.5 ,
pyasn1-modules    0.2.4 ,
python-dateutil   2.8.0 ,
PyYAML            5.1 ,
requests          2.21.0 ,
requests-oauthlib 1.2.0 ,
rsa               4.0 …
Run Code Online (Sandbox Code Playgroud)

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

6
推荐指数
1
解决办法
3754
查看次数

使用 kubernetes python 客户端将文件从 pod 复制到主机

我需要使用 kubernetes python 客户端将文件从 pod 复制到主机。它会像kubectl cp pod:file file.

我正在测试来自:https : //github.com/prafull01/Kubernetes-Utilities/blob/master/kubectl_cp_as_python_client.py的代码。

具体这段代码:

command_copy = ['tar', 'cf', '-', source_path]
with TemporaryFile() as tar_buffer:
    exec_stream = stream(self.coreClient.connect_get_namespaced_pod_exec, pod_name, name_space,
                         command=command_copy, stderr=True, stdin=True, stdout=True, tty=False,
                         _preload_content=False)
    # Copy file to stream

    try:
        while exec_stream.is_open():
            exec_stream.update(timeout=1)
            if exec_stream.peek_stdout():
                out = exec_stream.read_stdout()
                tar_buffer.write(out.encode('utf-8'))
            if exec_stream.peek_stderr():
                logger.debug("STDERR: %s" % exec_stream.read_stderr())
        exec_stream.close()
        tar_buffer.flush()
        tar_buffer.seek(0)
        with tarfile.open(fileobj=tar_buffer, mode='r:') as tar:
            member = tar.getmember(source_path)
            tar.makefile(member, destination_path)
            return True
    except Exception as e:
        raise manage_kubernetes_exception(e) …
Run Code Online (Sandbox Code Playgroud)

python kubernetes kubernetes-python-client

6
推荐指数
2
解决办法
2023
查看次数

如何使用 python kubernetes 客户端读取 Kubernetes 部署

python kubernetes 客户端相当于什么

kubectl get deploy -o yaml 
Run Code Online (Sandbox Code Playgroud)

CRUD python 客户端示例

我引用了这个示例来获取 python 部署,但没有读取部署选项

python python-3.x kubernetes kubernetes-python-client

6
推荐指数
1
解决办法
5516
查看次数

使用 Python 客户端在 Kuberentes 中批准 CSR

我在 Kubernetes 中有以下 CSR 对象:

$ kubectl get csr
NAME                                     AGE       REQUESTOR                                      CONDITION
test-certificate-0.my-namespace          53m       system:serviceaccount:my-namespace:some-user   Pending
Run Code Online (Sandbox Code Playgroud)

我想使用 Python API 客户端批准它:

$ kubectl get csr
NAME                                     AGE       REQUESTOR                                      CONDITION
test-certificate-0.my-namespace          53m       system:serviceaccount:my-namespace:some-user   Pending
Run Code Online (Sandbox Code Playgroud)

现在,csr对象的内容是:

{'api_version': 'certificates.k8s.io/v1beta1',
 'kind': 'CertificateSigningRequest',
 'metadata': {'annotations': None,
              'cluster_name': None,
              'creation_timestamp': datetime.datetime(2019, 3, 15, 14, 36, 28, tzinfo=tzutc()),
              'deletion_grace_period_seconds': None,
              'name': 'test-certificate-0.my-namespace',
              'namespace': None,
              'owner_references': None,
              'resource_version': '4269575',
              'self_link': '/apis/certificates.k8s.io/v1beta1/certificatesigningrequests/test-certificate-0.my-namespace',
              'uid': 'b818fa4e-472f-11e9-a394-124b379b4e12'},
 'spec': {'extra': None,
          'groups': ['system:serviceaccounts',
                     'system:serviceaccounts:cloudp-38483-test01',
                     'system:authenticated'],
          'request': 'redacted',
          'uid': 'd5bfde1b-4036-11e9-a394-124b379b4e12',
          'usages': ['digital …
Run Code Online (Sandbox Code Playgroud)

python kubernetes kubernetes-python-client

5
推荐指数
1
解决办法
580
查看次数

使用 K8s Python 客户端获取服务选择器

label selectors我正在尝试通过Kubernetes Python Client获取服务。我正在使用list_service_for_all_namespaces方法来检索服务,并使用field_selector如下参数对其进行过滤:

...
field_selector="spec.selector={u'app': 'redis'}
...
services = v1.list_service_for_all_namespaces(field_selector=field_selector, watch=False)
for service in services.items:
    print(service)
...
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"\"spec.selector\" is not a known field selector: only \"metadata.name\", \"metadata.namespace\"","reason":"BadRequest","code":400}
Run Code Online (Sandbox Code Playgroud)

因此,似乎只有namenamespace是有效参数,但没有记录:

field_selector = 'field_selector_example' # str | 通过字段限制返回对象列表的选择器。默认为一切。(选修的)

目前我的解决方法是为服务设置与标签选择器相同的标签,然后通过参数检索它,但我希望能够通过.label_selectorlabel selectors

问题是,从一开始我就需要获取服务背后的端点(后端 Pod),但 API 调用甚至没有返回此信息,所以我想我会获取选择器,将它们与 Pod 上的标签进行匹配,就这样,但现在我意识到选择器也不可能获得。

这限制太多了。我想可能我的方法是错误的。有谁知道如何获得label selectors服务?

kubernetes kubernetes-python-client

3
推荐指数
1
解决办法
6435
查看次数

使用python客户端从yaml创建Kubernetes CronJob

我正在尝试使用 K8S python 客户端在我的集群上创建一堆 cronjobs。create_from_yaml除了部署之外,我似乎找不到玉米作业或任何资源。我想做的是:

from kubernetes import client, utils

batchv1beta1 = client.BatchV1beta1Api()
utils.create_from_yaml(batchv1beta1, 'jobs/job-01.yaml')
Run Code Online (Sandbox Code Playgroud)

但这显然不起作用,因为这不是有效的属性。任何指导表示赞赏!

python kubernetes kubernetes-python-client

3
推荐指数
1
解决办法
2072
查看次数

如何使用 python 编辑/修补 kubernetes 部署以添加标签

我对 kubernetes 相当陌生 - 我开发了 Web UI/API,可以使用 Azure 机器学习服务将模型部署自动化到 Azure Kubernetes 服务 (AKS)。作为一项强化措施,我将使用此文档为 AKS 中部署的 Pod 设置托管身份。步骤之一是编辑部署以在/spec/template/metadata/labels部署中添加身份功能标签(请参阅本节Edit the deployment to add ...中开头的段落)。

我希望使用 python kubernetes 客户端(https://github.com/kubernetes-client/python)自动执行此步骤。浏览可用的 API,我想知道也许patch_namespaced_deployment可以让我编辑部署并在/spec/template/metadata/labels. 我正在寻找一些使用 python 客户端的示例代码 - 任何实现上述目标的帮助将不胜感激。

kubernetes kubernetes-python-client azure-aks azure-machine-learning-service aad-pod-identity

3
推荐指数
1
解决办法
9309
查看次数