@@ -251,11 +251,12 @@ Kubernetes 官方支持 [Go](#go-client)、[Python](#python-client)、[Java](#ja
251
251
#### Go 客户端 {#go-client}
252
252
253
253
<!--
254
- * To get the library, run the following command: `go get k8s.io/client-go/<version number>/kubernetes` See [https://github.com/kubernetes/client-go](https://github.com/kubernetes/client-go) to see which versions are supported.
255
- * Write an application atop of the client-go clients.-->
254
+ * To get the library, run the following command: `go get k8s.io/client-go@kubernetes-<kubernetes-version-number>` See [https://github.com/kubernetes/client-go/releases](https://github.com/kubernetes/client-go/releases) to see which versions are supported.
255
+ * Write an application atop of the client-go clients.
256
+ -->
256
257
257
- * 要获取库,运行下列命令:` go get k8s.io/client-go/< 版本号>/kubernetes ` ,
258
- 参见 [ https://github.com/kubernetes/client-go ] ( https://github.com/kubernetes/client-go ) 查看受支持的版本。
258
+ * 要获取库,运行下列命令:` go get k8s.io/client-go/kubernetes-<kubernetes 版本号> ` ,
259
+ 参见 [ https://github.com/kubernetes/client-go/releases ] ( https://github.com/kubernetes/client-go/releases ) 查看受支持的版本。
259
260
* 基于 client-go 客户端编写应用程序。
260
261
261
262
<!--
@@ -339,9 +340,9 @@ for i in ret.items:
339
340
#### Java 客户端 {#java-client}
340
341
341
342
<!--
342
- * To install the [Java Client](https://github.com/kubernetes-client/java), simply execute :
343
+ To install the [Java Client](https://github.com/kubernetes-client/java), run :
343
344
-->
344
- * 要安装 [ Java 客户端] ( https://github.com/kubernetes-client/java ) ,只需执行 :
345
+ 要安装 [ Java 客户端] ( https://github.com/kubernetes-client/java ) ,运行 :
345
346
346
347
``` shell
347
348
# 克隆 Java 库
@@ -522,168 +523,8 @@ exampleWithKubeConfig = do
522
523
>>= print
523
524
```
524
525
525
-
526
- <!--
527
- ### Accessing the API from within a Pod
528
-
529
- When accessing the API from within a Pod, locating and authenticating
530
- to the API server are slightly different to the external client case described above.
531
- -->
532
- ### 从 Pod 中访问 API
533
-
534
- 从 Pod 内部访问 API 时,定位 API 服务器和向服务器认证身份的操作
535
- 与上面描述的外部客户场景不同。
536
-
537
- <!--
538
- The easiest way to use the Kubernetes API from a Pod is to use
539
- one of the official [client libraries](/docs/reference/using-api/client-libraries/). These
540
- libraries can automatically discover the API server and authenticate.
541
- -->
542
- 从 Pod 使用 Kubernetes API 的最简单的方法就是使用官方的
543
- [ 客户端库] ( /zh/docs/reference/using-api/client-libraries/ ) 。
544
- 这些库可以自动发现 API 服务器并进行身份验证。
545
-
546
- <!--
547
- #### Using Official Client Libraries
548
-
549
- From within a Pod, the recommended ways to connect to the Kubernetes API are:
550
-
551
- - For a Go client, use the official [Go client library](https://github.com/kubernetes/client-go/).
552
- The `rest.InClusterConfig()` function handles API host discovery and authentication automatically.
553
- See [an example here](https://git.k8s.io/client-go/examples/in-cluster-client-configuration/main.go).
554
-
555
- - For a Python client, use the official [Python client library](https://github.com/kubernetes-client/python/).
556
- The `config.load_incluster_config()` function handles API host discovery and authentication automatically.
557
- See [an example here](https://github.com/kubernetes-client/python/blob/master/examples/in_cluster_config.py).
558
-
559
- - There are a number of other libraries available, please refer to the [Client Libraries](/docs/reference/using-api/client-libraries/) page.
560
-
561
- In each case, the service account credentials of the Pod are used to communicate
562
- securely with the API server.
563
- -->
564
- #### 使用官方客户端库
565
-
566
- 从一个 Pod 内部连接到 Kubernetes API 的推荐方式为:
567
-
568
- - 对于 Go 语言客户端,使用官方的 [ Go 客户端库] ( https://github.com/kubernetes/client-go/ ) 。
569
- 函数 ` rest.InClusterConfig() ` 自动处理 API 主机发现和身份认证。
570
- 参见[ 这里的一个例子] ( https://git.k8s.io/client-go/examples/in-cluster-client-configuration/main.go ) 。
571
-
572
- - 对于 Python 客户端,使用官方的 [ Python 客户端库] ( https://github.com/kubernetes-client/python/ ) 。
573
- 函数 ` config.load_incluster_config() ` 自动处理 API 主机的发现和身份认证。
574
- 参见[ 这里的一个例子] ( https://github.com/kubernetes-client/python/blob/master/examples/in_cluster_config.py ) 。
575
-
576
- - 还有一些其他可用的客户端库,请参阅[ 客户端库] ( /zh/docs/reference/using-api/client-libraries/ ) 页面。
577
-
578
- 在以上场景中,客户端库都使用 Pod 的服务账号凭据来与 API 服务器安全地通信。
579
-
580
- <!--
581
- #### Directly accessing the REST API
582
-
583
- While running in a Pod, the Kubernetes apiserver is accessible via a Service named
584
- `kubernetes` in the `default` namespace. Therefore, Pods can use the
585
- `kubernetes.default.svc` hostname to query the API server. Official client libraries
586
- do this automatically.
587
- -->
588
- #### 直接访问 REST API
589
-
590
- 在运行在 Pod 中时,可以通过 ` default ` 命名空间中的名为 ` kubernetes ` 的服务访问
591
- Kubernetes API 服务器。也就是说,Pod 可以使用 ` kubernetes.default.svc ` 主机名
592
- 来查询 API 服务器。官方客户端库自动完成这个工作。
593
-
594
- <!--
595
- The recommended way to authenticate to the API server is with a
596
- [service account](/docs/user-guide/service-accounts) credential. By default, a Pod
597
- is associated with a service account, and a credential (token) for that
598
- service account is placed into the filesystem tree of each container in that Pod,
599
- at `/var/run/secrets/kubernetes.io/serviceaccount/token`.
600
- -->
601
- 向 API 服务器进行身份认证的推荐做法是使用
602
- [ 服务账号] ( /zh/docs/tasks/configure-pod-container/configure-service-account/ ) 凭据。
603
- 默认情况下,每个 Pod 与一个服务账号关联,该服务账户的凭证(令牌)放置在此 Pod 中
604
- 每个容器的文件系统树中的 ` /var/run/secrets/kubernetes.io/serviceaccount/token ` 处。
605
-
606
- <!--
607
- If available, a certificate bundle is placed into the filesystem tree of each
608
- container at `/var/run/secrets/kubernetes.io/serviceaccount/ca.crt`, and should be
609
- used to verify the serving certificate of the API server.
610
- -->
611
- 如果由证书包可用,则凭证包被放入每个容器的文件系统树中的
612
- ` /var/run/secrets/kubernetes.io/serviceaccount/ca.crt ` 处,
613
- 且将被用于验证 API 服务器的服务证书。
614
-
615
- <!--
616
- Finally, the default namespace to be used for namespaced API operations is placed in a file
617
- at `/var/run/secrets/kubernetes.io/serviceaccount/namespace` in each container.
618
- -->
619
- 最后,用于命名空间域 API 操作的默认命名空间放置在每个容器中的
620
- ` /var/run/secrets/kubernetes.io/serviceaccount/namespace ` 文件中。
621
-
622
- <!--
623
- #### Using kubectl proxy
624
-
625
- If you would like to query the API without an official client library, you can run `kubectl proxy`
626
- as the [command](/docs/tasks/inject-data-application/define-command-argument-container/)
627
- of a new sidecar container in the Pod. This way, `kubectl proxy` will authenticate
628
- to the API and expose it on the `localhost` interface of the Pod, so that other containers
629
- in the Pod can use it directly.
630
- -->
631
- #### 使用 kubectl proxy {#use-kubectl-proxy}
632
-
633
- 如果你希望不实用官方客户端库就完成 API 查询,可以将 ` kubectl proxy ` 作为
634
- [ command] ( /zh/docs/tasks/inject-data-application/define-command-argument-container/ )
635
- 在 Pod 启动一个边车(Sidecar)容器。这样,` kubectl proxy ` 自动完成对 API
636
- 的身份认证,并将其暴露到 Pod 的 ` localhost ` 接口,从而 Pod 中的其他容器可以
637
- 直接使用 API。
638
-
639
- <!--
640
- #### Without using a proxy
641
-
642
- It is possible to avoid using the kubectl proxy by passing the authentication token
643
- directly to the API server. The internal certificate secures the connection.
644
- -->
645
- #### 不使用代理 {#without-using-a-proxy}
646
-
647
- 通过将认证令牌直接发送到 API 服务器,也可以避免运行 kubectl proxy 命令。
648
- 内部的证书机制能够为链接提供保护。
649
-
650
- ``` shell
651
- # 指向内部 API 服务器的主机名
652
- APISERVER=https://kubernetes.default.svc
653
-
654
- # 服务账号令牌的路径
655
- SERVICEACCOUNT=/var/run/secrets/kubernetes.io/serviceaccount
656
-
657
- # 读取 Pod 的名字空间
658
- NAMESPACE=$( cat ${SERVICEACCOUNT} /namespace)
659
-
660
- # 读取服务账号的持有者令牌
661
- TOKEN=$( cat ${SERVICEACCOUNT} /token)
662
-
663
- # 引用内部整数机构(CA)
664
- CACERT=${SERVICEACCOUNT} /ca.crt
665
-
666
- # 使用令牌访问 API
667
- curl --cacert ${CACERT} --header " Authorization: Bearer ${TOKEN} " -X GET ${APISERVER} /api
668
- ```
669
-
526
+ ## {{% heading "whatsnext" %}}
670
527
<!--
671
- The output will be similar to this:
528
+ * [Accessing the Kubernetes API from a Pod](/docs/tasks/run-application/access-api-from-pod/)
672
529
-->
673
- 输出类似于:
674
-
675
- ``` json
676
- {
677
- "kind" : " APIVersions" ,
678
- "versions" : [
679
- " v1"
680
- ],
681
- "serverAddressByClientCIDRs" : [
682
- {
683
- "clientCIDR" : " 0.0.0.0/0" ,
684
- "serverAddress" : " 10.0.1.149:443"
685
- }
686
- ]
687
- }
688
- ```
689
-
530
+ * [ 从 Pod 中访问 API] ( /zh/docs/tasks/run-application/access-api-from-pod/ )
0 commit comments