从本地计算机访问 Kubernetes API

use*_*835 1 google-cloud-platform kubernetes google-kubernetes-engine kubectl

我希望从本地计算机访问 Kubernetes API。我正在尝试使用 kubernetes Rest API 获取 pod 列表。

我在Google Cloud上创建了一个 kubernetes 集群和一些 pod 。

在我的本地 Windows 计算机上,我安装了 gcloud sdk 和 kubectl 组件。我使用以下方式连接到我的集群:

gcloud container clusters get-credentials my-cluster --region us-central1 --project my-project
Run Code Online (Sandbox Code Playgroud)

我可以使用以下命令获取 pod 列表kubectl get pods

不过,我想使用 kubernetes Rest API 获取 pod 列表。

GET https://kubernetes.default/api/v1/namespaces/default/pods
Authorization: Bearer my_access_token
Run Code Online (Sandbox Code Playgroud)

但我认为该请求没有得到通过。

在邮递员中,我收到错误:

Error: tunneling socket could not be established, cause=socket hang up

或者在 Python 中使用 requests 库(来自我的本地计算机),我收到错误

HTTPSConnectionPool(host='kubernetes.default', port=443): Max retries exceeded with url: /api/v1/namespaces/default/pods (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x00000277DCD04D90>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))

我在这里缺少什么?

Arg*_*dhu 5

https://kubernetes.default仅当您想从集群内部(即从另一个 Pod)访问 Kubernetes REST API 时,该端点才有效。要从 kubernetes 集群外部(即从本地计算机)访问 Kubernetes REST API,您需要使用可从外部访问的 API 服务器 IP 或主机,即 kubeconfig 文件中的主机。

要从 kubernetes Crusher 外部(即从本地计算机)访问它,可以使用三种方法参考此处的文档

  1. 在代理模式下运行 kubectl(推荐)。建议使用此方法,因为它使用存储的 apiserver 位置并使用自签名证书验证 API 服务器的身份。使用此方法不可能进行中间人 (MITM) 攻击。

    kubectl proxy --port=8080 &

    curl http://localhost:8080/api/v1/namespaces/default/pods

  2. 可以通过将身份验证令牌直接传递到 API 服务器来避免使用 kubectl 代理,如下所示:

检查所有可能的集群,因为您的 .KUBECONFIG 可能有多个上下文:

kubectl config view -o jsonpath='{"Cluster name\tServer\n"}{range .clusters[*]}{.name}{"\t"}{.cluster.server}{"\n"}{end}'
Run Code Online (Sandbox Code Playgroud)

从上面的输出中选择您想要与之交互的集群的名称:

export CLUSTER_NAME="some_server_name"
Run Code Online (Sandbox Code Playgroud)

指向引用集群名称的 API 服务器

APISERVER=$(kubectl config view -o jsonpath="{.clusters[?(@.name==\"$CLUSTER_NAME\")].cluster.server}")
Run Code Online (Sandbox Code Playgroud)

获取代币值

TOKEN=$(kubectl get secrets -o jsonpath="{.items[?(@.metadata.annotations['kubernetes\.io/service-account\.name']=='default')].data.token}"|base64 --decode)
Run Code Online (Sandbox Code Playgroud)

使用 TOKEN 探索 API

curl -X GET $APISERVER/api/v1/namespaces/default/pods --header "Authorization: Bearer $TOKEN" --insecure
Run Code Online (Sandbox Code Playgroud)
  1. 使用客户端库

要使用 Python 客户端,请运行以下命令:有关更多安装选项,pip install kubernetes请参阅Python 客户端库页面

Python 客户端可以使用与 CLI 相同的kubeconfig文件来kubectl定位 API 服务器并对其进行身份验证。看这个例子:

from kubernetes import client, config

config.load_kube_config()

v1=client.CoreV1Api()
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
    print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
Run Code Online (Sandbox Code Playgroud)

您也可以按照不使用 kubeconfig 文件的方式进行操作,但这需要更多工作,并且您需要使用 kubeconfig 文件中的 kubernetes API 服务器 IP 或主机名。