From 4795d7fe35e39dbeb8b62245f61c3760417a479e Mon Sep 17 00:00:00 2001 From: caodonghui Date: Thu, 13 May 2021 15:21:10 +0800 Subject: [PATCH] [zh]Resync tasks files[9] --- .../access-cluster.md | 19 +- ...port-forward-access-application-cluster.md | 178 ++++++++++-------- 2 files changed, 110 insertions(+), 87 deletions(-) diff --git a/content/zh/docs/tasks/access-application-cluster/access-cluster.md b/content/zh/docs/tasks/access-application-cluster/access-cluster.md index be848efce8..52099d3a2a 100644 --- a/content/zh/docs/tasks/access-application-cluster/access-cluster.md +++ b/content/zh/docs/tasks/access-application-cluster/access-cluster.md @@ -381,7 +381,7 @@ You have several options for connecting to nodes, pods and services from outside - Use a service with type `NodePort` or `LoadBalancer` to make the service reachable outside the cluster. See the [services](/docs/user-guide/services) and [kubectl expose](/docs/reference/generated/kubectl/kubectl-commands/#expose) documentation. - - Depending on your cluster environment, this may just expose the service to your corporate network, + - Depending on your cluster environment, this may only expose the service to your corporate network, or it may expose it to the internet. Think about whether the service being exposed is secure. Does it do its own authentication? - Place pods behind services. To access one specific pod from a set of replicas, such as for debugging, @@ -482,10 +482,10 @@ at `https://104.197.5.247/api/v1/namespaces/kube-system/services/elasticsearch-l #### 手动构建 apiserver 代理 URL {#manually-constructing-apiserver-proxy-urls} 如上所述,你可以使用 `kubectl cluster-info` 命令来获得服务的代理 URL。 -要创建包含服务端点、后缀和参数的代理 URL,只需添加到服务的代理 URL: +要创建包含服务端点、后缀和参数的代理 URL,需添加到服务的代理 URL: `http://`*`kubernetes_master_address`*`/api/v1/namespaces/`*`namespace_name`*`/services/`*`service_name[:port_name]`*`/proxy` 如果尚未为端口指定名称,则不必在 URL 中指定 *port_name*。 +对于已命名和未命名的端口,也可以使用端口号代替 *port_name*。 默认情况下,API server 使用 HTTP 代理你的服务。 要使用 HTTPS,请在服务名称前加上 `https:`: @@ -512,9 +513,9 @@ The supported formats for the name segment of the URL are: URL 名称段支持的格式为: * `` - 使用 http 代理到默认或未命名的端口 -* `:` - 使用 http 代理到指定的端口 +* `:` - 使用 http 代理到指定的端口名称或端口号 * `https::` - 使用 https 代理到默认或未命名的端口(注意后面的冒号) -* `https::` - 使用 https 代理到指定的端口 +* `https::` - 使用 https 代理到指定的端口名称或端口号 3. [kube proxy](/zh/docs/concepts/services-networking/service/#ips-and-vips): diff --git a/content/zh/docs/tasks/access-application-cluster/port-forward-access-application-cluster.md b/content/zh/docs/tasks/access-application-cluster/port-forward-access-application-cluster.md index 4219ca4f20..d52d2e5233 100644 --- a/content/zh/docs/tasks/access-application-cluster/port-forward-access-application-cluster.md +++ b/content/zh/docs/tasks/access-application-cluster/port-forward-access-application-cluster.md @@ -12,35 +12,35 @@ weight: 40 本文展示如何使用 `kubectl port-forward` 连接到在 Kubernetes 集群中 -运行的 Redis 服务。这种类型的连接对数据库调试很有用。 +运行的 MongoDB 服务。这种类型的连接对数据库调试很有用。 ## {{% heading "prerequisites" %}} * {{< include "task-tutorial-prereqs.md" >}} {{< version-check >}} -* 安装 [redis-cli](http://redis.io/topics/rediscli)。 +* 安装 [MongoDB Shell](https://www.mongodb.com/try/download/shell)。 -## 创建 Redis deployment 和服务 +## 创建 MongoDB deployment 和服务 -1. 创建一个 Redis deployment: +1. 创建一个运行 MongoDB 的 deployment: ```shell - kubectl apply -f https://k8s.io/examples/application/guestbook/redis-master-deployment.yaml + kubectl apply -f https://k8s.io/examples/application/guestbook/mongo-deployment.yaml ``` - 查看 deployment 状态: + 查看 Deployment 状态: ```shell kubectl get deployment ``` - 输出显示创建的 deployment: + 输出显示创建的 Deployment: ``` - NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE - redis-master 1 1 1 1 55s + NAME READY UP-TO-DATE AVAILABLE AGE + mongo 1/1 1 1 2m21s ``` - 查看 replicaset 状态: + Deployment 自动管理 ReplicaSet。 + 查看 ReplicaSet 状态: ```shell - kubectl get rs + kubectl get replicaset ``` - 输出显示创建的 replicaset: + 输出显示创建的 ReplicaSet: ``` - NAME DESIRED CURRENT READY AGE - redis-master-765d459796 1 1 1 1m + NAME DESIRED CURRENT READY AGE + mongo-75f59d57f4 1 1 1 3m12s ``` -2. 创建一个 Redis 服务: +2. 创建一个在网络上公开的 MongoDB 服务: ```shell - kubectl apply -f https://k8s.io/examples/application/guestbook/redis-master-service.yaml + kubectl apply -f https://k8s.io/examples/application/guestbook/mongo-service.yaml ``` - 查看输出是否成功,以验证是否成功创建 service: + 查看输出是否成功,以验证是否成功创建 Service: ``` - service/redis-master created + service/mongo created ``` - 检查 service 是否创建: + 检查 Service 是否创建: ```shell - kubectl get svc | grep redis + kubectl get service mongo ``` - 输出显示创建的 service: + 输出显示创建的 Service: ``` - NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE - redis-master ClusterIP 10.0.0.213 6379/TCP 27s + NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE + mongo ClusterIP 10.96.41.183 27017/TCP 11s ``` -3. 验证 Redis 服务是否运行在 pod 中并且监听 6379 端口: +3. 验证 MongoDB 服务是否运行在 Pod 中并且监听 27017 端口: ```shell - kubectl get pods redis-master-765d459796-258hz \ - --template='{{(index (index .spec.containers 0).ports 0).containerPort}}{{"\n"}}' + # Change mongo-75f59d57f4-4nd6q to the name of the Pod + kubectl get pod mongo-75f59d57f4-4nd6q --template='{{(index (index .spec.containers 0).ports 0).containerPort}}{{"\n"}}' ``` + - 输出应该显示端口: + 输出应该显示 Pod 中 MongoDB 的端口: ``` - 6379 + 27017 ``` + + (这是 Internet 分配给 MongoDB 的 TCP 端口)。 + -## 转发一个本地端口到 pod 端口 +## 转发一个本地端口到 Pod 端口 -1. 从 Kubernetes v1.10 开始,`kubectl port-forward` 允许使用资源名称 +1. `kubectl port-forward` 允许使用资源名称 (例如 pod 名称)来选择匹配的 pod 来进行端口转发。 ```shell - kubectl port-forward redis-master-765d459796-258hz 7000:6379 + # Change mongo-75f59d57f4-4nd6q to the name of the Pod + kubectl port-forward mongo-75f59d57f4-4nd6q 28015:27017 ``` + 这相当于 ```shell - kubectl port-forward pods/redis-master-765d459796-258hz 7000:6379 + kubectl port-forward pods/mongo-75f59d57f4-4nd6q 28015:27017 ``` 或者 ```shell - kubectl port-forward deployment/redis-master 7000:6379 + kubectl port-forward deployment/mongo 28015:27017 ``` 或者 ```shell - kubectl port-forward rs/redis-master 7000:6379 + kubectl port-forward replicaset/mongo-75f59d57f4 28015:27017 ``` 或者 - ``` - kubectl port-forward svc/redis-master 7000:redis + ```shell + kubectl port-forward service/mongo 28015:27017 ``` -2. 启动 Redis 命令行接口: +2. 启动 MongoDB 命令行接口: ```shell - redis-cli -p 7000 + mongosh --port 28015 ``` -3. 在 Redis 命令行提示符下,输入 `ping` 命令: +3. 在 MongoDB 命令行提示符下,输入 `ping` 命令: ``` - ping + db.runCommand( { ping: 1 } ) ``` @@ -270,43 +281,54 @@ the slightly simpler syntax: 以便你不需要管理本地端口冲突。该命令使用稍微不同的语法: ```shell -kubectl port-forward deployment/redis-master :6379 +kubectl port-forward deployment/mongo :27017 ``` + + +输出应该类似于: + +``` +Forwarding from 127.0.0.1:63753 -> 27017 +Forwarding from [::1]:63753 -> 27017 +``` + -`kubectl` 工具会找到一个未被使用的本地端口号(避免使用低段位的端口号,因为他们可能会被其他应用程序使用)。输出应该类似于: +`kubectl` 工具会找到一个未被使用的本地端口号(避免使用低段位的端口号,因为他们可能会被其他应用程序使用)。 +输出应该类似于: ``` -Forwarding from 127.0.0.1:62162 -> 6379 -Forwarding from [::1]:62162 -> 6379 +Forwarding from 127.0.0.1:63753 -> 27017 +Forwarding from [::1]:63753 -> 27017 ``` - ## 讨论 {#discussion} -与本地 7000 端口建立的连接将转发到运行 Redis 服务器的 pod 的 6379 端口。 -通过此连接,您可以使用本地工作站来调试在 pod 中运行的数据库。 +与本地 28015 端口建立的连接将转发到运行 MongoDB 服务器的 Pod 的 27017 端口。 +通过此连接,您可以使用本地工作站来调试在 Pod 中运行的数据库。 {{< warning >}} -由于已知的限制,目前的端口转发仅适用于 TCP 协议。 +`kubectl port-forward` 仅适用于 TCP 端口。 在 [issue 47862](https://github.com/kubernetes/kubernetes/issues/47862) -中正在跟踪对 UDP 协议的支持。 +中跟踪了对 UDP 协议的支持。 {{< /warning >}} ## {{% heading "whatsnext" %}}