我希望从我的本地机器上访问Kubernetes API。我正在尝试使用kubernetes Rest获取豆荚列表。
我在Google 上创建了一个kubernetes集群和一些吊舱。
在我的本地Windows机器上,我已经安装了gcloud sdk和kubectl组件。我使用以下方法连接到群集:
gcloud container clusters get-credentials my-cluster --region us-central1 --project my-project我可以使用kubectl get pods获得豆荚的列表
不过,我希望使用kubernetes Rest获得豆荚列表。
GET https://kubernetes.default/api/v1/namespaces/default/pods
Authorization: Bearer my_access_token但我认为这个请求没有通过。
在邮递员里,我得到了一个错误:
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'))
我在这里错过了什么?
发布于 2020-07-04 07:13:20
端点https://kubernetes.default只在您想要从集群内部访问Kubernetes REST时才能工作,即从另一个pod中访问。为了从Kubernetes集群之外即从本地计算机访问kubernetes REST,您需要使用外部可访问的API服务器IP或主机,即kubeconfig文件中的服务器IP或主机。
对于从kubernetes结壳外部访问它,即从本地机器访问它,有三种方法引用docs 这里
kubectl proxy --port=8080 &
curl http://localhost:8080/api/v1/namespaces/default/pods检查所有可能的集群,因为您的.KUBECONFIG可能有多个上下文:
kubectl config view -o jsonpath='{"Cluster name\tServer\n"}{range .clusters[*]}{.name}{"\t"}{.cluster.server}{"\n"}{end}'从上面的输出中选择要与之交互的集群名称:
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)使用令牌探索API
curl -X GET $APISERVER/api/v1/namespaces/default/pods --header "Authorization: Bearer $TOKEN" --insecure要使用Python,请运行以下命令:pip install kubernetes,有关更多安装选项,请参见Python客户端库页面。
Python可以使用与kubectl CLI相同的kubectl文件来定位和验证到kubectl服务器。参见此示例:
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或主机名。
发布于 2020-07-04 06:51:32
使用下面的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"
}
]
}发布于 2020-07-04 07:28:13
对于外部REST访问,您的api服务器地址不正确。
像这样得到地址。
kubectl config view在列表中找到您的集群名称并获取APi。
这是在我的本地pc上工作的cURL (没有真正的IP或令牌)。
curl --location --request GET 'https://nnn.nnn.nnnn.nnn/api/v1/namespaces/develop/pods' \
--header 'Authorization: bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'如果您在邮递员中运行,则可能必须禁用证书验证。
https://stackoverflow.com/questions/62726057
复制相似问题