繁体   English   中英

使用 kubernetes 客户端在 pod 上运行脚本并退出

[英]Using kubernetes client to run a script on pod and get outout

我有一个 python 脚本,我需要使用它在容器上运行并获取 output。 现在我可以得到容器名称如下: -

config.load_kube_config()
configuration.assert_hostname = False
# config.load_incluster_config()

contexts, active_context = config.list_kube_config_contexts()
contexts = [context["name"] for context in contexts]
cluster1 = contexts[0]
cluster2 = contexts[1]

print(f"First cluster is: {cluster1}")
print(f"Second cluster is: {cluster2}")

client1 = client.CoreV1Api(
    api_client=config.new_client_from_config(context=cluster1)
)
client2 = client.CoreV1Api(
    api_client=config.new_client_from_config(context=cluster2)
)

for i in client1.list_namespaced_pod(namespace).items:
    if "cassandra-0" in i.metadata.name:
        pod1_name = f"{i.metadata.name}"
        print(F"Cassandra Pod Name in NA: {pod1_name}")

现在我得到的 output 是:

Cassandra Pod Name in NA: cassandra-0

但是现在当我添加一个命令时:

        # Calling exec and waiting for response
        exec_command = ['nodetool status']
        resp = stream(client2.connect_get_namespaced_pod_exec,
                  pod1_name,
                  namespace,
                  command=exec_command,
                  stderr=True, stdin=False,
                  stdout=True, tty=False)
        print("Response: " + resp)
        resp.close()

它现在给我以下错误:

Traceback (most recent call last):
  File "C:\Python310\lib\site-packages\kubernetes\stream\ws_client.py", line 512, in websocket_call
    client = WSClient(configuration, url, headers, capture_all)
  File "C:\Python310\lib\site-packages\kubernetes\stream\ws_client.py", line 65, in __init__
    self.sock = create_websocket(configuration, url, headers)
  File "C:\Python310\lib\site-packages\kubernetes\stream\ws_client.py", line 478, in create_websocket
    websocket.connect(url, **connect_opt)
  File "C:\Python310\lib\site-packages\websocket\_core.py", line 248, in connect
    self.handshake_response = handshake(self.sock, url, *addrs, **options)
  File "C:\Python310\lib\site-packages\websocket\_handshake.py", line 57, in handshake
    status, resp = _get_resp_headers(sock)
  File "C:\Python310\lib\site-packages\websocket\_handshake.py", line 148, in _get_resp_headers
    raise WebSocketBadStatusException("Handshake status %d %s", status, status_message, resp_headers)
websocket._exceptions.WebSocketBadStatusException: Handshake status 400 Bad Request

知道做错了什么吗?

我注意到的代码中唯一的问题是代码在cluster0中查找 pod 并在cluster1中执行命令,即client2.connect_get_namespaced_pod_exec,

这是工作示例

from click import command
from kubernetes import client, config
from kubernetes.stream import stream
config.load_kube_config()

contexts, active_context = config.list_kube_config_contexts()
contexts = [context["name"] for context in contexts]
print (contexts)
cluster1 = contexts[4]
cluster2 = contexts[1]

print(f"First cluster is: {cluster1}")
print(f"Second cluster is: {cluster2}")

client1 = client.CoreV1Api(
    api_client=config.new_client_from_config(context=cluster1)
)
client2 = client.CoreV1Api(
    api_client=config.new_client_from_config(context=cluster2)
)

for i in client1.list_namespaced_pod("vault").items:
    print(i.metadata.name)
    if "vault-1" in i.metadata.name:
        pod1_name = f"{i.metadata.name}"
        print(F"Cassandra Pod Name in NA: {pod1_name}")
        # Calling exec and waiting for response
        exec_command = ["/bin/sh", "-c", "echo $HOSTNAME && vault status"]
        resp = stream(client1.connect_get_namespaced_pod_exec,
                  pod1_name,
                  "vault",
                  command=exec_command,
                  stderr=True, stdin=False,
                  stdout=True, tty=False)
        print("Response: " + resp)

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM