快速入门:使用 Helm 在 Azure Kubernetes 服务 (AKS) 上进行开发Quickstart: Develop on Azure Kubernetes Service (AKS) with Helm

Helm 是一种开放源打包工具,有助于安装和管理 Kubernetes 应用程序的生命周期。Helm is an open-source packaging tool that helps you install and manage the lifecycle of Kubernetes applications. 与诸如 APTYum 的 Linux 包管理器类似,Helm 用于管理 Kubernetes 图表,这些图表是预配置的 Kubernetes 资源包。Similar to Linux package managers such as APT and Yum, Helm is used to manage Kubernetes charts, which are packages of preconfigured Kubernetes resources.

本文介绍如何使用 Helm 在 AKS 中打包和运行应用程序。This article shows you how to use Helm to package and run an application on AKS. 有关使用 Helm 安装现有应用程序的详细信息,请参阅在 AKS 中通过 Helm 安装现有应用程序For more details on installing an existing application using Helm, see Install existing applications with Helm in AKS.

先决条件Prerequisites

创建 Azure 容器注册表Create an Azure Container Registry

若要使用 Helm 在 AKS 群集中运行应用程序,需要使用 Azure 容器注册表来存储容器映像。To use Helm to run your application in your AKS cluster, you need an Azure Container Registry to store your container images. 以下示例使用 az acr create 在位于“基本”SKU 的 MyResourceGroup 资源组中创建名为 MyHelmACR 的 ACR 。The below example uses az acr create to create an ACR named MyHelmACR in the MyResourceGroup resource group with the Basic SKU. 你应提供自己的唯一注册表名称。You should provide your own unique registry name. 注册表名称在 Azure 中必须唯一,并且包含 5-50 个字母数字字符。The registry name must be unique within Azure, and contain 5-50 alphanumeric characters. “基本”SKU 是用于开发目的的成本优化入口点,可在存储与吞吐量之间实现平衡。The Basic SKU is a cost-optimized entry point for development purposes that provides a balance of storage and throughput.

az group create --name MyResourceGroup --location chinaeast2
az acr create --resource-group MyResourceGroup --name MyHelmACR --sku Basic

输出类似于以下示例。The output is similar to the following example. 记下 ACR 的 loginServer 值,因为稍后的步骤会用到它。Make a note of the loginServer value for your ACR since it will be used in a later step. 在以下示例中,myhelmacr.azurecr.cn 是 MyHelmACR 的 loginServer 。In the below example, myhelmacr.azurecr.cn is the loginServer for MyHelmACR.

{
  "adminUserEnabled": false,
  "creationDate": "2019-06-11T13:35:17.998425+00:00",
  "id": "/subscriptions/<ID>/resourceGroups/MyResourceGroup/providers/Microsoft.ContainerRegistry/registries/MyHelmACR",
  "location": "chinaeast2",
  "loginServer": "myhelmacr.azurecr.cn",
  "name": "MyHelmACR",
  "networkRuleSet": null,
  "provisioningState": "Succeeded",
  "resourceGroup": "MyResourceGroup",
  "sku": {
    "name": "Basic",
    "tier": "Basic"
  },
  "status": null,
  "storageAccount": null,
  "tags": {},
  "type": "Microsoft.ContainerRegistry/registries"
}

创建 Azure Kubernetes 服务群集Create an Azure Kubernetes Service cluster

创建 AKS 群集。Create an AKS cluster. 以下命令将创建名为 MyAKS 的 AKS 群集并附加 MyHelmACR。The below command creates an AKS cluster called MyAKS and attaches MyHelmACR.

az aks create -g MyResourceGroup -n MyAKS --location chinaeast2  --attach-acr MyHelmACR --generate-ssh-keys

AKS 群集需要访问 ACR 来提取并运行容器映像。Your AKS cluster needs access to your ACR to pull the container images and run them. 上述命令还授予 MyAKS 群集对 MyHelmACR ACR 的访问权 。The above command also grants the MyAKS cluster access to your MyHelmACR ACR.

连接到 AKS 群集Connect to your AKS cluster

若要从本地计算机连接到 Kubernetes 群集,请使用 kubectl(Kubernetes 命令行客户端)。To connect to the Kubernetes cluster from your local computer, you use kubectl, the Kubernetes command-line client.

也可使用 az aks install-cli 命令在本地安装它:You can also install it locally using the az aks install-cli command:

az aks install-cli

若要将 kubectl 配置为连接到 Kubernetes 群集,请使用 az aks get-credentials 命令。To configure kubectl to connect to your Kubernetes cluster, use the az aks get-credentials command. 以下示例获取 MyResourceGroup 中名为 MyAKS 的 AKS 群集的凭据:The following example gets credentials for the AKS cluster named MyAKS in the MyResourceGroup:

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

下载示例应用程序Download the sample application

本快速入门使用来自 Azure Dev Spaces 示例存储库的示例 Node.js 应用程序This quickstart uses an example Node.js application from the Azure Dev Spaces sample repository. 从 GitHub 克隆该应用程序,然后导航到 dev-spaces/samples/nodejs/getting-started/webfrontend 目录。Clone the application from GitHub and navigate to the dev-spaces/samples/nodejs/getting-started/webfrontend directory.

git clone https://github.com/Azure/dev-spaces
cd dev-spaces/samples/nodejs/getting-started/webfrontend

创建 DockerfileCreate a Dockerfile

使用以下命令创建新的 Dockerfile 文件:Create a new Dockerfile file using the following:

FROM node:latest

WORKDIR /webfrontend

COPY package.json ./

RUN npm install

COPY . .

EXPOSE 80
CMD ["node","server.js"]

生成并将示例应用程序推送到 ACRBuild and push the sample application to the ACR

使用前面的 Dockerfile 通过 az acr build 命令生成映像并将其推送到注册表。Use the az acr build command to build and push an image to the registry, using the preceding Dockerfile. 命令末尾处的 . 设置 Dockerfile 的位置(在本例中为当前目录)。The . at the end of the command sets the location of the Dockerfile, in this case the current directory.

az acr build --image webfrontend:v1 \
  --registry MyHelmACR \
  --file Dockerfile .

创建 Helm 图表Create your Helm chart

使用 helm create 命令生成 Helm 图表。Generate your Helm chart using the helm create command.

helm create webfrontend

对 webfrontend/values.yaml 进行以下更新。Make the following updates to webfrontend/values.yaml. 使用前面步骤中记下的注册表的 loginServer(例如 myhelmacr.azurecr.cn)进行替换:Substitute the loginServer of your registry that you noted in an earlier step, such as myhelmacr.azurecr.cn:

  • image.repository 更改为 <loginServer>/webfrontendChange image.repository to <loginServer>/webfrontend
  • service.type 更改为 LoadBalancerChange service.type to LoadBalancer

例如:For example:

# Default values for webfrontend.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1

image:
  repository: myhelmacr.azurecr.cn/webfrontend
  pullPolicy: IfNotPresent
...
service:
  type: LoadBalancer
  port: 80
...

在 webfrontend/Chart.yaml 中将 appVersion 更新为 v1Update appVersion to v1 in webfrontend/Chart.yaml. 例如For example

apiVersion: v2
name: webfrontend
...
# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application.
appVersion: v1

运行 Helm 图表Run your Helm chart

使用 helm install 命令安装使用 Helm 图表的应用程序。Use the helm install command to install your application using your Helm chart.

helm install webfrontend webfrontend/

服务可能需要几分钟才能返回公共 IP 地址。It takes a few minutes for the service to return a public IP address. 若要监视进度,请结合 watch 参数使用 kubectl get service 命令:To monitor the progress, use the kubectl get service command with the watch parameter:

$ kubectl get service --watch

NAME                TYPE          CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
webfrontend         LoadBalancer  10.0.141.72   <pending>     80:32150/TCP   2m
...
webfrontend         LoadBalancer  10.0.141.72   <EXTERNAL-IP> 80:32150/TCP   7m

在浏览器中使用 <EXTERNAL-IP> 导航到应用程序的负载均衡器,以查看示例应用程序。Navigate to the load balancer of your application in a browser using the <EXTERNAL-IP> to see the sample application.

删除群集Delete the cluster

如果不再需要本教程中创建的群集,请使用az group delete 命令删除资源组、AKS 群集、容器注册表和其中存储的容器映像,以及所有相关资源。When the cluster is no longer needed, use the az group delete command to remove the resource group, the AKS cluster, the container registry, the container images stored there, and all related resources.

az group delete --name MyResourceGroup --yes --no-wait

备注

删除群集时,AKS 群集使用的 Azure Active Directory 服务主体不会被删除。When you delete the cluster, the Azure Active Directory service principal used by the AKS cluster is not removed. 有关如何删除服务主体的步骤,请参阅 AKS 服务主体的注意事项和删除For steps on how to remove the service principal, see AKS service principal considerations and deletion. 如果你使用了托管标识,则该标识由平台托管,不需要删除。If you used a managed identity, the identity is managed by the platform and does not require removal.

后续步骤Next steps

有关使用 Helm 的详细信息,请参阅 Helm 文档。For more information about using Helm, see the Helm documentation.