从googlecloud函数在GKE上运行容器中执行shell命令

2024-05-19 10:22:06 发布

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

试图使用Google Cloud函数在Google Kubernetes引擎(GKE)上运行的容器中执行shell命令

本质上是希望通过云函数中的Python SDK重现“kubectl exec-it”wordpress cluster xyz“-/bin/bash”的功能

我尝试了下面的代码,但是出现了连接被拒绝错误

from kubernetes import client
from kubernetes.stream import stream
from kubernetes.client import configuration                                              


def testing(event):
    v1=client.CoreV1Api()
    exec_command = [
    '/bin/sh',
    '-c',
    'touch ~/testing']
    resp = stream(v1.connect_get_namespaced_pod_exec,"wordpress-cluster-xyz", 'default',
              command=exec_command,
              stderr=True, stdin=False,
              stdout=True, tty=False)
    print("Response: " + resp)

我收到的错误信息:

Error: function terminated. Recommended action: inspect logs for termination reason. refused Details: (0) Reason: [Errno 111] Connection

完整错误日志:

Traceback (most recent call last): File "/env/local/lib/python3.7/site-packages/kubernetes/stream/ws_client.py", line 296, in websocket_call client = WSClient(configuration, get_websocket_url(url), headers, capture_all) File "/env/local/lib/python3.7/site-packages/kubernetes/stream/ws_client.py", line 94, in init self.sock.connect(url, header=header) File "/env/local/lib/python3.7/site-packages/websocket/_core.py", line 223, in connect options.pop('socket', None)) File "/env/local/lib/python3.7/site-packages/websocket/_http.py", line 121, in connect sock = _open_socket(addrinfo_list, options.sockopt, options.timeout) File "/env/local/lib/python3.7/site-packages/websocket/_http.py", line 201, in _open_socket raise err File "/env/local/lib/python3.7/site-packages/websocket/_http.py", line 176, in _open_socket sock.connect(address) ConnectionRefusedError: [Errno 111] Connection refused During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 346, in run_http_function result = _function_handler.invoke_user_function(flask.request) File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 217, in invoke_user_function return call_user_function(request_or_event) File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 210, in call_user_function return self._user_function(request_or_event) File "/user_code/main.py", line 15, in testing stdout=True, tty=False) File "/env/local/lib/python3.7/site-packages/kubernetes/stream/stream.py", line 35, in stream return func(*args, **kwargs) File "/env/local/lib/python3.7/site-packages/kubernetes/client/api/core_v1_api.py", line 841, in connect_get_namespaced_pod_exec (data) = self.connect_get_namespaced_pod_exec_with_http_info(name, namespace, **kwargs) # noqa: E501 File "/env/local/lib/python3.7/site-packages/kubernetes/client/api/core_v1_api.py", line 941, in connect_get_namespaced_pod_exec_with_http_info collection_formats=collection_formats) File "/env/local/lib/python3.7/site-packages/kubernetes/client/api_client.py", line 345, in call_api _preload_content, _request_timeout) File "/env/local/lib/python3.7/site-packages/kubernetes/client/api_client.py", line 176, in __call_api _request_timeout=_request_timeout) File "/env/local/lib/python3.7/site-packages/kubernetes/stream/stream.py", line 30, in _intercept_request_call return ws_client.websocket_call(config, *args, **kwargs) File "/env/local/lib/python3.7/site-packages/kubernetes/stream/ws_client.py", line 302, in websocket_call raise ApiException(status=0, reason=str(e)) kubernetes.client.rest.ApiException: (0) Reason: [Errno 111] Connection refused

提前谢谢


Tags: inpyenvclientstreamlibpackageslocal

热门问题