Skip to content

Commit 53cec5e

Browse files
committed
docs: Rsync access-cluster-api.md
1 parent 0ea4666 commit 53cec5e

File tree

1 file changed

+22
-30
lines changed

1 file changed

+22
-30
lines changed

content/zh/docs/tasks/administer-cluster/access-cluster-api.md

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ This page shows how to access clusters using the Kubernetes API.
1818

1919
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
2020

21-
2221
<!-- steps -->
2322

2423
<!--
@@ -56,11 +55,11 @@ kubectl config view
5655

5756
<!--
5857
Many of the [examples](https://github.com/kubernetes/examples/tree/master/) provide an introduction to using
59-
kubectl. Complete documentation is found in the [kubectl manual](/docs/reference/kubectl/overview/).
58+
kubectl. Complete documentation is found in the [kubectl manual](/docs/reference/kubectl/).
6059
-->
6160

6261
许多[样例](https://github.com/kubernetes/examples/tree/master/)
63-
提供了使用 kubectl 的介绍。完整文档请见 [kubectl 手册](/zh/docs/reference/kubectl/overview/)
62+
提供了使用 kubectl 的介绍。完整文档请见 [kubectl 手册](/zh/docs/reference/kubectl/)
6463

6564
<!--
6665
### Directly accessing the REST API
@@ -76,7 +75,7 @@ kubectl 处理对 API 服务器的定位和身份验证。如果你想通过 htt
7675
1. Run kubectl in proxy mode (recommended). This method is recommended, since it uses the stored apiserver location and verifies the identity of the API server using a self-signed cert. No man-in-the-middle (MITM) attack is possible using this method.
7776
1. Alternatively, you can provide the location and credentials directly to the http client. This works with client code that is confused by proxies. To protect against man in the middle attacks, you'll need to import a root cert into your browser.
7877
-->
79-
1. 以代理模式运行 kubectl(推荐)。
78+
1. 以代理模式运行 kubectl(推荐)。
8079
推荐使用此方法,因为它用存储的 apiserver 位置并使用自签名证书验证 API 服务器的标识。
8180
使用这种方法无法进行中间人(MITM)攻击。
8281
2. 另外,你可以直接为 HTTP 客户端提供位置和身份认证。
@@ -160,8 +159,25 @@ export CLUSTER_NAME="some_server_name"
160159
# 指向引用该集群名称的 API 服务器
161160
APISERVER=$(kubectl config view -o jsonpath="{.clusters[?(@.name==\"$CLUSTER_NAME\")].cluster.server}")
162161

163-
# 获得令牌
164-
TOKEN=$(kubectl get secrets -o jsonpath="{.items[?(@.metadata.annotations['kubernetes\.io/service-account\.name']=='default')].data.token}"|base64 -d)
162+
# 创建一个 secret 来保存默认服务账户的令牌
163+
kubectl apply -f - <<EOF
164+
apiVersion: v1
165+
kind: Secret
166+
metadata:
167+
name: default-token
168+
annotations:
169+
kubernetes.io/service-account.name: default
170+
type: kubernetes.io/service-account-token
171+
EOF
172+
173+
# 等待令牌控制器使用令牌填充 secret:
174+
while ! kubectl describe secret default-token | grep -E '^token' >/dev/null; do
175+
echo "waiting for token..." >&2
176+
sleep 1
177+
done
178+
179+
# 获取令牌
180+
TOKEN=$(kubectl get secret default-token -o jsonpath='{.data.token}' | base64 --decode)
165181

166182
# 使用令牌玩转 API
167183
curl -X GET $APISERVER/api --header "Authorization: Bearer $TOKEN" --insecure
@@ -185,30 +201,6 @@ curl -X GET $APISERVER/api --header "Authorization: Bearer $TOKEN" --insecure
185201
}
186202
```
187203

188-
<!-- Using `jsonpath` approach: -->
189-
使用 `jsonpath` 方式:
190-
191-
```shell
192-
APISERVER=$(kubectl config view --minify -o jsonpath='{.clusters[0].cluster.server}')
193-
TOKEN=$(kubectl get secret $(kubectl get serviceaccount default -o jsonpath='{.secrets[0].name}') -o jsonpath='{.data.token}' | base64 --decode )
194-
curl $APISERVER/api --header "Authorization: Bearer $TOKEN" --insecure
195-
```
196-
197-
```json
198-
{
199-
"kind": "APIVersions",
200-
"versions": [
201-
"v1"
202-
],
203-
"serverAddressByClientCIDRs": [
204-
{
205-
"clientCIDR": "0.0.0.0/0",
206-
"serverAddress": "10.0.1.149:443"
207-
}
208-
]
209-
}
210-
```
211-
212204
<!--
213205
The above example uses the `--insecure` flag. This leaves it subject to MITM
214206
attacks. When kubectl accesses the cluster it uses a stored root certificate

0 commit comments

Comments
 (0)