【问题标题】:how to connect kubernetes pods (terminal) interactively through api or other?如何通过api或其他交互连接kubernetes pods(终端)?
【发布时间】:2021-11-10 12:25:27
【问题描述】:

如何通过 API 或其他方式交互连接 Kubernetes pods(终端)?

我们可以使用服务公开 Pod,但我们需要如何使用 API 或其他方式以交互方式连接 Pod。

【问题讨论】:

标签: kubernetes google-kubernetes-engine kubernetes-ingress kubectl kubernetes-pod


【解决方案1】:

也许您正在寻找可以在不暴露 pod 的情况下使用的 kubectl port-forward。

https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/

【讨论】:

    【解决方案2】:

    您可以按照here 的描述使用exec --stdin

    类似这样的:

    kubectl exec --stdin --tty [POD ID] -- /bin/bash
    

    【讨论】:

      【解决方案3】:

      如果您想通过 API 调用来实现,最简单的方法是使用其中一个 api client libraries e.g. kubernetes python client.

      python 客户端 中可以使用api_instance.connect_get_namespaced_pod_exec 方法完成。 documentation 甚至为您提供了一个现成的工作示例:

      from __future__ import print_function
      import time
      import kubernetes.client
      from kubernetes.client.rest import ApiException
      from pprint import pprint
      configuration = kubernetes.client.Configuration()
      # Configure API key authorization: BearerToken
      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'
      
      # Defining host is optional and default to http://localhost
      configuration.host = "http://localhost"
      
      # Enter a context with an instance of the API kubernetes.client
      with kubernetes.client.ApiClient(configuration) as api_client:
          # Create an instance of the API class
          api_instance = kubernetes.client.CoreV1Api(api_client)
          name = 'name_example' # str | name of the PodExecOptions
      namespace = 'namespace_example' # str | object name and auth scope, such as for teams and projects
      command = 'command_example' # str | Command is the remote command to execute. argv array. Not executed within a shell. (optional)
      container = 'container_example' # str | Container in which to execute the command. Defaults to only container if there is only one container in the pod. (optional)
      stderr = True # bool | Redirect the standard error stream of the pod for this call. Defaults to true. (optional)
      stdin = True # bool | Redirect the standard input stream of the pod for this call. Defaults to false. (optional)
      stdout = True # bool | Redirect the standard output stream of the pod for this call. Defaults to true. (optional)
      tty = True # bool | TTY if true indicates that a tty will be allocated for the exec call. Defaults to false. (optional)
      
          try:
              api_response = api_instance.connect_get_namespaced_pod_exec(name, namespace, command=command, container=container, stderr=stderr, stdin=stdin, stdout=stdout, tty=tty)
              pprint(api_response)
          except ApiException as e:
              print("Exception when calling CoreV1Api->connect_get_namespaced_pod_exec: %s\n" % e)
      

      作为command,您需要使用bash/bin/bashsh/bin/sh(这取决于您的 pod 中可用的 shell)。

      也将其与this 答案进行比较。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-05
        • 2020-12-23
        • 2023-03-06
        • 1970-01-01
        相关资源
        最近更新 更多