使用 Bridge to KubernetesUse Bridge to Kubernetes

可以使用 Bridge to Kubernetes 来重定向 Kubernetes 群集与开发计算机上运行的代码之间的流量。You can use Bridge to Kubernetes to redirect traffic between your Kubernetes cluster and code running on your development computer. 本指南还提供了一个脚本,用于在 Kubernetes 群集上部署包含多个微服务的大型示例应用程序。This guide also provides a script for deploying a large sample application with multiple microservices on a Kubernetes cluster.

在开始之前Before you begin

本指南使用 TODO App 示例应用程序来演示如何将开发计算机连接到 Kubernetes 群集。This guide uses the TODO App sample application to demonstrate connecting your development computer to a Kubernetes cluster. 如果你已在 Kubernetes 群集上运行自己的应用程序,仍可以执行以下步骤,使用你自己的服务名称。If you already have your own application running on a Kubernetes cluster, you can still follow the steps below and use the names of your own services.

此示例演示如何使用 Bridge to Kubernetes 在任何 Kubernetes 群集上开发简单的 TODO 应用程序的微服务版本。This sample illustrates how Bridge to Kubernetes can be used to develop a microservice version of a simple TODO application on any Kubernetes cluster. 这个使用 Visual Studio 的示例由 TodoMVC 提供的代码改编而来。This sample, using Visual Studio, has been adapted from code provided by TodoMVC. 这些步骤应适用于任何 Kubernetes 群集。These steps should work with any Kubernetes cluster.

TODO 应用程序示例由提供持久存储的前端和后端组成。The TODO application sample is composed of a frontend and a backend that provides persistent storage. 此扩展示例添加了一个统计信息组件,并将应用程序分解为多个微服务,具体如下:This extended sample adds a statistics component and breaks the application into a number of microservices, specifically:

  • 前端调用 database-api 来保留和更新 TODO 项;The frontend calls the database-api to persist and update TODO items;
  • database-api 服务依赖于 Mongo 数据库来保留 TODO 项;The database-api service relies on a Mongo database to persist TODO items;
  • 前端将添加、完成和删除事件写入 RabbitMQ 队列;The frontend writes add, complete, and delete events to a RabbitMQ queue;
  • 统计信息工作者从 RabbitMQ 队列接收事件,并更新 Redis 缓存;A statistics worker receives events from the RabbitMQ queue and updates a Redis cache;
  • 统计信息 API 公开缓存的统计数据供前端显示。A statistics API exposes the cached statistics for the frontend to show.

总之,此扩展 TODO 应用程序由六个相关组件组成。In all, this extended TODO application is composed of six interrelated components.

先决条件Prerequisites

检查群集Check the cluster

打开命令提示符,检查是否已安装 kubectl,以及你要使用的群集是否在路径上可用且就绪,并将上下文设置为该群集。Open a command prompt, and check that the kubectl is installed and on the path, the cluster you want to use is available and ready, and set the context to that cluster.

kubectl cluster-info
kubectl config use-context {context-name}

其中,{context-name} 是要用于 todo-app 示例的群集上下文的名称。where {context-name} is the name of the context for the cluster you want to use for the todo-app sample.

部署应用程序Deploy the application

克隆 mindaro 存储库,并打开一个命令窗口,其中当前工作文件夹指向 samples/todo-app。Clone the mindaro repo and open a command window with the current working folder to samples/todo-app.

为示例创建命名空间。Create a namespace for the sample.

kubectl create namespace todo-app

然后,应用部署清单:Then, apply the deployment manifest:

kubectl apply -n todo-app -f deployment.yaml

这是一个简单的部署,它使用 LoadBalancer 类型的服务公开前端。This is a simple deployment that exposes the frontend using a service of type LoadBalancer. 等待所有 pod 都处于运行状态,以及 frontend 服务的外部 IP变为可用。Wait for all the pods to be running and for the external IP of the frontend service to become available.

如果要使用 MiniKube 进行测试,则需要使用 minikube tunnel 解析外部 IP。If you are testing with MiniKube, you will need to use minikube tunnel to resolve an external IP. 如果你使用 AKS 或其他基于云的 Kubernetes 提供程序,系统会自动分配一个外部 IP。If you're using AKS or another cloud-based Kubernetes provider, an external IP is assigned automatically. 使用以下命令监视 frontend 服务,等待其启动并运行:Use the following command to monitor the frontend service to wait until it's up and running:

kubectl get service -n todo-app frontend --watch

NAME       TYPE           CLUSTER-IP    EXTERNAL-IP     PORT(S)        AGE
frontend   LoadBalancer   10.0.245.78   20.73.226.228   80:31910/TCP   6m26s

使用外部 IP 和本地端口(“端口”列中的第一个数字)浏览到应用程序。Browse to the application using the external IP and local port (the first number in the PORT(S) column).

http://{external-ip}:{local-port}

在浏览器中测试正在运行的应用。Test the running app in the browser. 在添加、完成和删除 todo 项时,请注意,统计信息页将以预期的指标进行更新。As you add, complete and delete todo items, notice that the stats page updates with the expected metrics.

连接到群集并调试服务Connect to your cluster and debug a service

在 Visual Studio 中打开 samples\todo-app\database-api\database-api.csproj。Open samples\todo-app\database-api\database-api.csproj in Visual Studio. 在项目中,从启动设置下拉列表中选择“Bridge to Kubernetes”,如下所示。In the project, select Bridge to Kubernetes from the launch settings dropdown as shown below.

选择 Bridge to Kubernetes

单击 Bridge to Kubernetes 旁边的启动按钮。Click on the start button next to Bridge to Kubernetes. 在“为 Bridge to Kubernetes 创建配置文件”对话框中:In the Create profile for Bridge to Kubernetes dialog:

  • 选择你的群集名称。Select your cluster name.
  • 选择“todo-app”作为命名空间。Select todo-app for your namespace.
  • 选择“database-api”作为要重定向到的服务。Select database-api for the service to redirect.
  • 选择之前用于启动浏览器的同一 URL: http://{external-ip}:{local-port}Select the same URL you used previously to launch your browser, http://{external-ip}:{local-port}

选择 Bridge to Kubernetes 群集

选择是否在隔离模式下运行,这意味着使用该群集的其他用户不会受到更改的影响。Choose whether or not you want to run isolated, meaning that others who are using the cluster won't be affected by your changes. 通过将请求路由到每个受影响的服务的副本,但正常路由所有其他流量,可以实现此隔离模式。This isolation mode is accomplished by routing your requests to your copy of each affected service, but routing all other traffic normally. 有关如何执行此操作的更多说明,请参阅 Bridge to Kubernetes 的工作原理More explanation on how this is done can be found at How Bridge to Kubernetes Works.

单击 “确定”Click OK. 将 Kubernetes 群集中 database-api 服务的所有流量重定向到开发计算机中运行的应用程序版本。All traffic in the Kubernetes cluster is redirected for the database-api service to the version of your application running in your development computer. Bridge to Kubernetes 还会将应用程序的所有出站流量路由回 Kubernetes 群集。Bridge to Kubernetes also routes all outbound traffic from the application back to your Kubernetes cluster.

备注

系统将提示你允许 EndpointManager 在提升的权限下运行并修改主机文件。You will be prompted to allow the EndpointManager to run elevated and modify your hosts file.

当状态栏显示你已连接到 database-api 服务时,你的开发计算机已连接。Your development computer is connected when the status bar shows you are connected to the database-api service.

已连接开发计算机

备注

在后续启动时,系统不会提示“创建 Bridge to Kubernetes 的配置文件”对话框。On subsequent launches, you will not be prompted with the Create profile for Bridge to Kubernetes dialog. 可在项目属性的“调试”中更新这些设置。You update these settings in the Debug in the project properties.

连接开发计算机后,流量将开始重定向到你要替换的服务的开发计算机。Once your development computer is connected, traffic starts redirecting to your development computer for the service you are replacing.

备注

例如,若要在之后编辑调试配置文件,则在希望使用不同的 Kubernetes 服务进行测试时,请选择“调试” > “调试属性”,然后单击“更改”按钮 。To edit the debug profile later, for example, if you want to test with a different Kubernetes service, choose Debug > Debug Properties, and click the Change button.

设置断点Set a break point

打开 MongoHelper.cs,并单击 CreateTask 方法中第 68 行处的某个位置,将光标置于该处。Open MongoHelper.cs and click somewhere on line 68 in the CreateTask method to put your cursor there. 若要设置断点,请按 F9,或选择“调试” > “切换断点” 。Set a breakpoint by hitting F9 or selecting Debug > Toggle Breakpoint.

通过打开公共 URL(前端服务的外部 IP 地址),导航到示例应用程序。Navigate to the sample application by opening the public URL (the external IP address for the frontend service). 若要恢复服务,请按“F5”,或单击“调试” > “继续” 。To resume the service, hit F5 or click Debug > Continue.

将光标置于断点所在的行上,然后按 F9,即可删除该断点。Remove the breakpoint by putting your cursor on the line with the breakpoint and hitting F9.

备注

默认情况下,停止调试任务也会断开开发计算机与 Kubernetes 群集的连接。By default, stopping the debugging task also disconnects your development computer from your Kubernetes cluster. 你可以更改此行为,方法是在“工具” > “选项”对话框的“Kubernetes 调试工具”部分中将“调试后断开连接”更改为 falseYou can change this behavior by changing Disconnect after debugging to false in the Kubernetes Debugging Tools section of the Tools > Options dialog. 更新此设置后,在停止并启动调试时,开发计算机将保持连接状态。After updating this setting, your development computer will remain connected when you stop and start debugging. 若要断开开发计算机与群集的连接,请单击工具栏上的“断开连接”按钮。To disconnect your development computer from you cluster click on the Disconnect button on the toolbar.

Kubernetes 调试选项的屏幕截图

其他配置Additional configuration

Bridge to Kubernetes 可以处理路由流量和复制环境变量,无需任何其他配置。Bridge to Kubernetes can handle routing traffic and replicating environment variables without any additional configuration. 如果需要下载已装载到 Kubernetes 群集中的容器的所有文件(例如 ConfigMap 文件),可以创建 KubernetesLocalProcessConfig.yaml 以将这些文件下载到开发计算机。If you need to download any files that are mounted to the container in your Kubernetes cluster, such as a ConfigMap file, you can create a KubernetesLocalProcessConfig.yaml to download those files to your development computer. 有关详细信息,请参阅将 KubernetesLocalProcessConfig.yaml 用于 Bridge to Kubernetes 的附加配置For more information, see Using KubernetesLocalProcessConfig.yaml for additional configuration with for Bridge to Kubernetes.

使用日志记录和诊断Using logging and diagnostics

可在开发计算机的 TEMP 目录的 Bridge to Kubernetes 目录中查找诊断日志。You can find the diagnostic logs in Bridge to Kubernetes directory in your development computer's TEMP directory.

后续步骤Next steps

了解 Bridge to Kubernetes 的工作原理。Learn how Bridge to Kubernetes works.