1
- ---
1
+ ---
2
2
title : 访问集群
3
3
weight : 20
4
4
content_type : concept
@@ -50,10 +50,10 @@ kubectl config view
50
50
51
51
<!--
52
52
Many of the [examples](/docs/user-guide/kubectl-cheatsheet) provide an introduction to using
53
- kubectl and complete documentation is found in the [kubectl manual ](/docs/user-guide /kubectl-overview ).
53
+ ` kubectl` and complete documentation is found in the [kubectl reference ](/docs/reference /kubectl/ ).
54
54
-->
55
55
有许多 [ 例子] ( /zh/docs/reference/kubectl/cheatsheet/ ) 介绍了如何使用 kubectl,
56
- 可以在 [ kubectl手册 ] ( /zh/docs/reference/kubectl/overview/ ) 中找到更完整的文档。
56
+ 可以在 [ kubectl 参考 ] ( /zh/docs/reference/kubectl/overview/ ) 中找到更完整的文档。
57
57
58
58
<!--
59
59
## Directly accessing the REST API
@@ -139,18 +139,47 @@ curl http://localhost:8080/api/
139
139
<!--
140
140
### Without kubectl proxy
141
141
142
- In Kubernetes version 1.3 or later, `kubectl config view` no longer displays the token. Use `kubectl describe secret...` to get the token for the default service account, like this:
142
+ In Kubernetes version 1.3 or later, `kubectl config view` no longer displays the token. Use `kubectl apply` and `kubectl describe secret...` to create a token for the default service account with grep/cut:
143
+
144
+ First, create the Secret, requesting a token for the default ServiceAccount:
145
+
143
146
-->
147
+
144
148
### 不使用 kubectl proxy
145
149
146
150
在 Kubernetes 1.3 或更高版本中,` kubectl config view ` 不再显示 token。
147
- 使用 ` kubectl describe secret ... ` 来获取默认服务帐户的 token,如下所示:
148
-
151
+ 使用 ` kubectl apply ` 和 ` kubectl describe secret ... ` 及 grep 和剪切操作来为 default 服务帐户创建令牌,如下所示:
149
152
` grep/cut ` 方法实现:
153
+ 首先,创建 Secret,请求默认 ServiceAccount 的令牌:
154
+ ``` shell
155
+ kubectl apply -f - << EOF
156
+ apiVersion: v1
157
+ kind: Secret
158
+ metadata:
159
+ name: default-token
160
+ annotations:
161
+ kubernetes.io/service-account.name: default
162
+ type: kubernetes.io/service-account-token
163
+ EOF
164
+ ```
165
+
166
+ <!--
167
+ Next, wait for the token controller to populate the Secret with a token:
168
+
169
+ Capture and use the generated token:
170
+ -->
171
+ 接下来,等待令牌控制器使用令牌填充 Secret:
172
+ ``` shell
173
+ while ! kubectl describe secret default-token | grep -E ' ^token' > /dev/null; do
174
+ echo " waiting for token..." >&2
175
+ sleep 1
176
+ done
177
+ ```
150
178
179
+ 捕获并使用生成的令牌:
151
180
``` shell
152
181
APISERVER=$( kubectl config view | grep server | cut -f 2- -d " :" | tr -d " " )
153
- TOKEN=$( kubectl describe secret $( kubectl get secrets | grep default | cut -f1 -d ' ' ) | grep -E ' ^token' | cut -f2 -d' :' | tr -d ' ' )
182
+ TOKEN=$( kubectl describe secret default-token | grep -E ' ^token' | cut -f2 -d' :' | tr -d ' ' )
154
183
curl $APISERVER /api --header " Authorization: Bearer $TOKEN " --insecure
155
184
```
156
185
``` json
@@ -172,7 +201,7 @@ curl $APISERVER/api --header "Authorization: Bearer $TOKEN" --insecure
172
201
173
202
``` shell
174
203
APISERVER=$( kubectl config view --minify -o jsonpath=' {.clusters[0].cluster.server}' )
175
- TOKEN=$( kubectl get secret $( kubectl get serviceaccount default -o jsonpath= ' {.secrets[0].name} ' ) -o jsonpath=' {.data.token}' | base64 --decode )
204
+ TOKEN=$( kubectl get secret default-token -o jsonpath=' {.data.token}' | base64 --decode )
176
205
curl $APISERVER /api --header " Authorization: Bearer $TOKEN " --insecure
177
206
```
178
207
0 commit comments