簡體   English   中英

從本地機器訪問 Kubernetes API

[英]Accessing Kubernetes APIs from local machine

我希望從我的本地機器訪問 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

我可以使用kubectl get pods列表

雖然,我想使用 kubernetes Rest API 獲取 pod 列表。

GET https://kubernetes.default/api/v1/namespaces/default/pods
Authorization: Bearer my_access_token

但我認為請求沒有通過。

在 Postman 中,我收到錯誤:

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'))

我在這里想念什么?

The endpoint https://kubernetes.default only works if you want to access Kubernetes REST API from inside the cluster ie from another pod. For accessing Kubernetes REST API from outside the kubernetes cluster ie from your local machine you need to use the API server IP or host which is externally accessible ie the one which is there in kubeconfig file.

要從 kubernetes 外殼(即從您的本地計算機)外部訪問它, 這里的文檔中有三種方法可供參考

  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}'

Select 您想要從上面 output 交互的集群名稱:

export CLUSTER_NAME="some_server_name"

指向引用集群名稱的 API 服務器

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

獲取令牌值

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

使用 TOKEN 探索 API

curl -X GET $APISERVER/api/v1/namespaces/default/pods --header "Authorization: Bearer $TOKEN" --insecure
  1. 使用客戶端庫

要使用 Python 客戶端,請運行以下命令: pip install kubernetes有關更多安裝選項,請參見ZA7F5F35426B5274173Z 客戶端庫頁面 1B68安裝選項2274173Z 客戶端庫頁面。

Python 客戶端可以使用與kubectl CLI 相同的kubeconfig文件來定位和驗證 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))

您也可以在不使用 kubeconfig 文件的情況下按照您的方式進行操作,但它需要更多的工作,您需要使用 kubernetes API 服務器 IP 或 kubeconfig.

使用以下 kubectl 命令啟動 Kubernetes API 服務器的代理:

kubectl proxy --port=8080

Get the API versions:

curl http://localhost:8080/api/
The output should look similar to this:

{
  "kind": "APIVersions",
  "versions": [
    "v1"
  ],
  "serverAddressByClientCIDRs": [
    {
      "clientCIDR": "0.0.0.0/0",
      "serverAddress": "10.0.2.15:8443"
    }
  ]
}

您的 api 服務器地址對於外部 REST 訪問不正確。

獲取這樣的地址。

kubectl config view

在列表中找到您的集群名稱並獲取 APi。

這是在我的本地電腦上工作的 cURL(沒有真正的 IP 或令牌)。

curl --location --request GET 'https://nnn.nnn.nnnn.nnn/api/v1/namespaces/develop/pods' \
--header 'Authorization: bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

如果您在 POSTMAN 中運行,您可能必須禁用證書驗證。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM