使用 Bridge KubernetesUse Bridge to Kubernetes

您可以使用 Bridge 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

本指南使用 自行車共用範例應用程式 ,示範如何將您的開發電腦連接到 Kubernetes 叢集。This guide uses the Bike Sharing 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.

必要條件Prerequisites

此外,針對 .NET 主控台應用程式,請安裝 VisualStudio NuGet 套件。Also, for .NET console applications, install the Microsoft.VisualStudio.Azure.Kubernetes.Tools.Targets NuGet Package.

建立 Kubernetes 叢集Create a Kubernetes cluster

支援的區域中建立 AKS 叢集。Create an AKS cluster in a supported region. 下列命令會建立名為 MyResourceGroup 的資源群組與名為 MyAKS 的 AKS 叢集。The below commands create a resource group called MyResourceGroup and an AKS cluster called MyAKS.

az group create \
    --name MyResourceGroup \
    --location eastus

az aks create \
    --resource-group MyResourceGroup \
    --name MyAKS \
    --location eastus \
    --node-count 3 \
    --generate-ssh-keys

安裝範例應用程式Install the sample application

使用提供的腳本,在您的叢集上安裝範例應用程式。Install the sample application on your cluster using the provided script. 您可以使用 Azure Cloud Shell來執行此腳本。You can run this script using the Azure Cloud Shell.

git clone https://github.com/Microsoft/mindaro
cd mindaro
chmod +x ./bridge-quickstart.sh
./bridge-quickstart.sh -g MyResourceGroup -n MyAKS

開啟應用程式的公用 URL (顯示在安裝腳本的輸出中),以流覽至執行叢集的範例應用程式。Navigate to the sample application running your cluster by opening its public URL, which is displayed in the output of the installation script.

$ ./bridge-quickstart.sh -g MyResourceGroup -n MyAKS
Defaulting Dev spaces repository root to current directory : ~/mindaro
Setting the Kube context
...
To try out the app, open the url:
bikeapp.bikesharingweb.EXTERNAL_IP.nip.io

在上述範例中,公用 URL 是 bikeapp.bikesharingweb.EXTERNAL_IP.nip.ioIn the above sample, the public URL is bikeapp.bikesharingweb.EXTERNAL_IP.nip.io.

連線至您的叢集並將服務進行偵錯工具Connect to your cluster and debug a service

在您的開發電腦上,下載並設定 Kubernetes CLI 以使用 az aks get 認證連接到您的 Kubernetes 叢集。On your development computer, download and configure the Kubernetes CLI to connect to your Kubernetes cluster using az aks get-credentials.

az aks get-credentials --resource-group MyResourceGroup --name MyAKS

從 GitHub 中的 自行車共用範例應用程式 存放庫,使用 [綠色程式 代碼 ] 按鈕上的下拉式清單,然後選擇 [ 在 Visual Studio 中開啟 ],在本機複製存放庫,並在 Visual Studio 中開啟資料夾。From the Bike Sharing sample application repo in GitHub, use the dropdown on the green Code button and choose Open in Visual Studio to clone the repo locally and open the folder in Visual Studio. 然後 ,使用 > [檔案開啟專案] 來開啟samples/BikeSharingApp/ReservationEngine資料夾中的app.config專案。Then, use File > Open Project to open the app.csproj project in the samples/BikeSharingApp/ReservationEngine folder.

在您的專案中,選取 [啟動設定] 下拉式清單中的 [ 要 Kubernetes 的橋接器 ],如下所示。In your project, select Bridge to Kubernetes from the launch settings dropdown as shown below.

選擇橋接器至 Kubernetes

按一下 [ 橋接器至 Kubernetes] 旁邊的 [開始] 按鈕。Click on the start button next to Bridge to Kubernetes. 在 [ 建立橋接器至 Kubernetes 的設定檔 ] 對話方塊中:In the Create profile for Bridge to Kubernetes dialog:

  • 選取您的訂用帳戶。Select your subscription.
  • 為您的叢集選取 [ MyAKS ]。Select MyAKS for your cluster.
  • 針對您的命名空間選取 [ bikeapp ]。Select bikeapp for your namespace.
  • 選取服務要重新導向的 reservationengineSelect reservationengine for the service to redirect.
  • 選取啟動設定檔的 應用程式Select app for the launch profile.
  • 選取以 http://bikeapp.bikesharingweb.EXTERNAL_IP.nip.io 啟動瀏覽器的 URL。Select http://bikeapp.bikesharingweb.EXTERNAL_IP.nip.io for the URL to launch your browser.

選擇橋接器至 Kubernetes 叢集

重要

您只能重新導向具有單一 pod 的服務。You can only redirect services that have a single pod.

選擇您是否要執行隔離,這表示使用叢集的其他人不會受到您的變更影響。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. 如需如何完成這項作業的詳細說明,請參閱 Kubernetes 橋接器的運作方式。More explanation on how this is done can be found at How Bridge to Kubernetes Works.

按一下 [ 儲存並開始調試]。Click Save and start debugging.

Kubernetes 叢集中的所有流量都會重新導向至您的開發電腦上執行的應用程式版本, reservationengine 服務。All traffic in the Kubernetes cluster is redirected for the reservationengine service to the version of your application running in your development computer. 橋接器至 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.

當狀態列顯示您已連線到服務時,您的開發電腦就會連線 reservationengineYour development computer is connected when the status bar shows you are connected to the reservationengine service.

開發電腦已連線

注意

後續啟動時,系統不會提示您提供 [ Bridge 到 Kubernetes 的建立設定檔 ] 對話方塊。On subsequent launches, you will not be prompted with the Create profile for Bridge to Kubernetes dialog. 您可以在 [專案屬性] 的 [ Debug ] 中更新這些設定。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.

設定中斷點Set a break point

開啟 BikesHelper.cs ,並在第26行的某處按一下,將游標放在該處。Open BikesHelper.cs and click somewhere on line 26 to put your cursor there. F9或選取 [ Debug > 切換中斷點],以設定中斷點。Set a breakpoint by hitting F9 or selecting Debug > Toggle Breakpoint.

開啟公用 URL 以流覽至範例應用程式。Navigate to the sample application by opening the public URL. 選取 **Aurelia Briggs (客戶) ** 作為使用者,然後選取要租借的自行車。Select Aurelia Briggs (customer) as the user, then select a bike to rent. 選擇 出租自行車Choose Rent Bike. 返回 Visual Studio,觀察行26已反白顯示。Return to Visual Studio and observe line 26 is highlighted. 您所設定的中斷點已在第26行暫停服務。The breakpoint you set has paused the service at line 26. 若要繼續服務,請按F5或按一下 [ Debug > Continue]。To resume the service, hit F5 or click Debug > Continue. 返回您的瀏覽器,確認頁面顯示您出租了自行車。Return to your browser and verify the page shows you have rented the bike.

將游標放在第26行 BikesHelper.cs ,然後按 F9 鍵,以移除中斷點。Remove the breakpoint by putting your cursor on line 26 in BikesHelper.cs and hitting F9.

注意

根據預設,停止偵錯工具也會中斷您的開發電腦與 Kubernetes 叢集的連線。By default, stopping the debugging task also disconnects your development computer from your Kubernetes cluster. 您可以變更此行為,方法是在偵錯工具的 [Kubernetes 偵錯工具] 區段中,變更 [在偵錯工具後中斷連接] falseKubernetes Debugging ToolsYou can change this behavior by changing Disconnect after debugging to false in the Kubernetes Debugging Tools section of the debugging options. 更新此設定之後,當您停止並開始進行偵錯工具時,您的開發電腦將保持連線。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.

其他設定Additional configuration

橋接器至 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 到 Kubernetes 的其他設定。For more information, see Using KubernetesLocalProcessConfig.yaml for additional configuration with for Bridge to Kubernetes.

使用記錄和診斷Using logging and diagnostics

您可以在 Bridge to Kubernetes 開發電腦的 暫存 目錄中尋找目錄中的診斷記錄。You can find the diagnostic logs in Bridge to Kubernetes directory in your development computer's TEMP directory.

從您的叢集中移除範例應用程式Remove the sample application from your cluster

使用提供的腳本,從您的叢集中移除範例應用程式。Use the provided script to remove the sample application from your cluster.

./bridge-quickstart.sh -c -g MyResourceGroup -n MyAKS

後續步驟Next steps

瞭解 Kubernetes 的橋樑如何運作。Learn how Bridge to Kubernetes works.