从本地机器访问 Kubernetes API
Accessing Kubernetes APIs from local machine
我希望从我的本地计算机访问 Kubernetes API。我正在尝试使用 kubernetes Rest API 获取 pods 的列表。
我在 Google Cloud.
上创建了一个 kubernetes 集群和一些 pods
在我的本地 Windows 机器上,我安装了 gcloud sdk 和 kubectl 组件。
我使用以下方式连接到我的集群:
gcloud container clusters get-credentials my-cluster --region us-central1 --project my-project
我可以使用 kubectl get pods
获取 pods 的列表
不过,我想使用 kubernetes Rest API 获取 pods 列表。
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 中使用请求库(从我的本地机器),我收到错误
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'))
我在这里错过了什么?
使用下面的 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"
}
]
}
端点 https://kubernetes.default
仅在您想从集群内部(即从另一个 pod)访问 Kubernetes REST API 时才有效。要从 kubernetes 集群外部访问 Kubernetes REST API,即从您的本地计算机,您需要使用可从外部访问的 API 服务器 IP 或主机,即 kubeconfig 文件中的主机。
要从 kubernetes cruster 外部访问它,即从您的本地机器访问它,可以参考文档 here
中的三种方法
运行 kubectl 代理模式(推荐)。建议使用此方法,因为它使用存储的 apiserver 位置并使用自签名证书验证 API 服务器的身份。使用此方法不可能进行中间人 (MITM) 攻击。
kubectl proxy --port=8080 &
curl http://localhost:8080/api/v1/namespaces/default/pods
可以通过将身份验证令牌直接传递到 API 服务器来避免使用 kubectl 代理,如下所示:
检查所有可能的集群,因为您的 .KUBECONFIG 可能有多个上下文:
kubectl config view -o jsonpath='{"Cluster name\tServer\n"}{range .clusters[*]}{.name}{"\t"}{.cluster.server}{"\n"}{end}'
Select 上面输出中你想与之交互的集群名称:
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
- 使用客户端库
要使用 Python 客户端,运行 以下命令:pip install kubernetes
有关更多安装选项,请参阅 Python Client Library page。
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 文件而按照您现在的方式进行操作,但它的工作量更大,您需要使用 kubeconfig 文件中的 kubernetes API 服务器 IP 或主机名。
您的 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 中,您可能必须禁用证书验证。
我希望从我的本地计算机访问 Kubernetes API。我正在尝试使用 kubernetes Rest API 获取 pods 的列表。
我在 Google Cloud.
上创建了一个 kubernetes 集群和一些 pods在我的本地 Windows 机器上,我安装了 gcloud sdk 和 kubectl 组件。 我使用以下方式连接到我的集群:
gcloud container clusters get-credentials my-cluster --region us-central1 --project my-project
我可以使用 kubectl get pods
不过,我想使用 kubernetes Rest API 获取 pods 列表。
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 中使用请求库(从我的本地机器),我收到错误
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'))
我在这里错过了什么?
使用下面的 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"
}
]
}
端点 https://kubernetes.default
仅在您想从集群内部(即从另一个 pod)访问 Kubernetes REST API 时才有效。要从 kubernetes 集群外部访问 Kubernetes REST API,即从您的本地计算机,您需要使用可从外部访问的 API 服务器 IP 或主机,即 kubeconfig 文件中的主机。
要从 kubernetes cruster 外部访问它,即从您的本地机器访问它,可以参考文档 here
中的三种方法运行 kubectl 代理模式(推荐)。建议使用此方法,因为它使用存储的 apiserver 位置并使用自签名证书验证 API 服务器的身份。使用此方法不可能进行中间人 (MITM) 攻击。
kubectl proxy --port=8080 &
curl http://localhost:8080/api/v1/namespaces/default/pods
可以通过将身份验证令牌直接传递到 API 服务器来避免使用 kubectl 代理,如下所示:
检查所有可能的集群,因为您的 .KUBECONFIG 可能有多个上下文:
kubectl config view -o jsonpath='{"Cluster name\tServer\n"}{range .clusters[*]}{.name}{"\t"}{.cluster.server}{"\n"}{end}'
Select 上面输出中你想与之交互的集群名称:
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
探索APIcurl -X GET $APISERVER/api/v1/namespaces/default/pods --header "Authorization: Bearer $TOKEN" --insecure
- 使用客户端库
要使用 Python 客户端,运行 以下命令:pip install kubernetes
有关更多安装选项,请参阅 Python Client Library page。
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 文件而按照您现在的方式进行操作,但它的工作量更大,您需要使用 kubeconfig 文件中的 kubernetes API 服务器 IP 或主机名。
您的 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 中,您可能必须禁用证书验证。