Azure Kubernetes Service (AKS) 是受控 Kubernetes 服務,可讓您快速部署及管理叢集。 在本快速入門中,您將:
- 使用 Bicep 部署 AKS 叢集。
- 使用一組微服務和 Web 前端模擬零售情節,執行範例多容器應用程式。
注意
若要開始快速佈建 AKS 叢集,本文包含僅針對評估目的部署具有預設設定值之叢集的步驟。 在部署生產就緒叢集之前,建議您先熟悉我們的基準參考架構,考慮其如何符合您的業務需求。
開始之前
- 本快速入門假設您已有 Kubernetes 概念的基本知識。 如需詳細資訊,請參閱 Azure Kubernetes Services (AKS) 的 Kubernetes 核心概念。
- 您需要具有有效訂用帳戶的 Azure 帳戶。 如果您沒有帳戶,可免費建立帳戶。
- 若要深入了解如何建立 Windows Server 節點集區,請參閱建立支援 Windows Server 容器的 AKS 叢集。
- Bicep 是使用宣告式語法來部署 Azure 資源的特定領域語言 (DSL)。 其提供簡潔的語法、可靠的類型安全,並支援程式碼重複使用。 Bicep 能夠為您在 Azure 中的基礎結構即程式碼解決方案,提供最佳的製作體驗。
在 Azure Cloud Shell 中使用 Bash 環境。 如需詳細資訊,請參閱開始使用 Azure Cloud Shell。
若要在本地執行 CLI 參考命令,請安裝 Azure CLI。 若您在 Windows 或 macOS 上執行,請考慮在 Docker 容器中執行 Azure CLI。 如需詳細資訊,請參閱〈如何在 Docker 容器中執行 Azure CLI〉。
如果您使用的是本機安裝,請使用 az login 命令,透過 Azure CLI 來登入。 請遵循您終端機上顯示的步驟,完成驗證程序。 如需其他登入選項,請參閱 使用 Azure CLI 向 Azure 進行驗證。
出現提示時,請在第一次使用時安裝 Azure CLI 延伸模組。 如需擴充功能的詳細資訊,請參閱 使用和管理 Azure CLI 的擴充功能。
執行 az version 以尋找已安裝的版本和相依程式庫。 若要升級至最新版本,請執行 az upgrade。
- 本文需要使用 Azure CLI 2.0.64 版或更新版本。 若您使用的是 Azure Cloud Shell,即已安裝最新版本。
- 這篇文章需要現有的 Azure 資源群組。 如果您需要建立一個,您可以使用 az group create 命令。
- 若要使用 Bicep 檔案建立 AKS 叢集,您必須提供 SSH 公開金鑰。 如果您需要此資源,請參閱下一節。 否則,請跳至檢閱 Bicep 檔案。
- 確定您用來建立叢集的身分識別擁有適當的最低權限。 如需 AKS 存取和身分識別的詳細資訊,請參閱 Azure Kubernetes Service (AKS) 的存取與身分識別選項。
- 若要部署 Bicep 檔案,您需要對即將建立的資源具備寫入權限,並可存取
Microsoft.Resources/deployments
資源類型上的所有作業。 例如,若要建立虛擬機器,您需要Microsoft.Compute/virtualMachines/write
和Microsoft.Resources/deployments/*
權限。 如需角色與權限的清單,請參閱 Azure 內建角色。
建立 SSH 金鑰組
移至 https://shell.azure.com,並在您的瀏覽器中開啟 Cloud Shell。
使用 az sshkey create Azure CLI 命令或
ssh-keygen
命令建立 SSH 金鑰組。# Create an SSH key pair using Azure CLI az sshkey create --name "mySSHKey" --resource-group "myResourceGroup" # Create an SSH key pair using ssh-keygen ssh-keygen -t rsa -b 4096
如需建立 SSH 金鑰的詳細資訊,請參閱在 Azure 中建立及管理驗證的 SSH 金鑰。
檢閱 Bicep 檔案
此快速入門中使用的 Bicep 檔案是來自 Azure 快速入門範本。
@description('The name of the Managed Cluster resource.')
param clusterName string = 'aks101cluster'
@description('The location of the Managed Cluster resource.')
param location string = resourceGroup().location
@description('Optional DNS prefix to use with hosted Kubernetes API server FQDN.')
param dnsPrefix string
@description('Disk size (in GB) to provision for each of the agent pool nodes. This value ranges from 0 to 1023. Specifying 0 will apply the default disk size for that agentVMSize.')
@minValue(0)
@maxValue(1023)
param osDiskSizeGB int = 0
@description('The number of nodes for the cluster.')
@minValue(1)
@maxValue(50)
param agentCount int = 3
@description('The size of the Virtual Machine.')
param agentVMSize string = 'standard_d2s_v3'
@description('User name for the Linux Virtual Machines.')
param linuxAdminUsername string
@description('Configure all linux machines with the SSH RSA public key string. Your key should include three parts, for example \'ssh-rsa AAAAB...snip...UcyupgH azureuser@linuxvm\'')
param sshRSAPublicKey string
resource aks 'Microsoft.ContainerService/managedClusters@2024-02-01' = {
name: clusterName
location: location
identity: {
type: 'SystemAssigned'
}
properties: {
dnsPrefix: dnsPrefix
agentPoolProfiles: [
{
name: 'agentpool'
osDiskSizeGB: osDiskSizeGB
count: agentCount
vmSize: agentVMSize
osType: 'Linux'
mode: 'System'
}
]
linuxProfile: {
adminUsername: linuxAdminUsername
ssh: {
publicKeys: [
{
keyData: sshRSAPublicKey
}
]
}
}
}
}
output controlPlaneFQDN string = aks.properties.fqdn
Bicep 檔案中定義的資源:
如需更多 AKS 範例,請參閱 AKS 快速入門範本站台。
部署 Bicep 檔案
- 將 Bicep 檔案以 main.bicep 儲存至本機電腦。
重要
Bicep 檔案會將 clusterName
參數設定為 aks101cluster 字串。 如果您想要使用不同的叢集名稱,請務必先將字串更新為您慣用的叢集名稱,再將檔案儲存到您的電腦。
使用 Azure CLI 或 Azure PowerShell 部署 Bicep 檔案。
az deployment group create --resource-group myResourceGroup --template-file main.bicep --parameters dnsPrefix=<dns-prefix> linuxAdminUsername=<linux-admin-username> sshRSAPublicKey='<ssh-key>'
在命令中提供下列值:
- DNS 前置詞:為您的叢集輸入唯一的 DNS 前置詞,例如 myakscluster。
- Linux 管理員使用者名稱:輸入使用 SSH 連線的使用者名稱,例如 azureuser。
- SSH RSA 公開金鑰:複製並貼上 SSH 金鑰組的公開部分 (根據預設,為~/.ssh/id_rsa.pub內容)。
建立 AKS 叢集需要幾分鐘的時間。 請等到叢集成功部署後,再移至下一個步驟。
驗證 Bicep 部署
連線至叢集
若要管理 Kubernetes 叢集,請使用 Kubernetes 命令列用戶端 kubectl。 如果您使用 Azure Cloud Shell,則 kubectl
已安裝。
-
az aks install-cli
使用
kubectl
命令,設定 連線到 Kubernetes 叢集。 此命令會下載憑證並設定 Kubernetes CLI 以供使用。az aks get-credentials --resource-group myResourceGroup --name myAKSCluster
使用 kubectl get nodes 命令來確認與叢集的連線。 此命令會傳回叢集節點的清單。
kubectl get nodes
下列輸出範例會顯示上一個步驟中建立的單一節點。 確定節點的狀態為就緒。
NAME STATUS ROLES AGE VERSION aks-agentpool-41324942-0 Ready agent 6m44s v1.12.6 aks-agentpool-41324942-1 Ready agent 6m46s v1.12.6 aks-agentpool-41324942-2 Ready agent 6m45s v1.12.6
部署應用程式
若要部署應用程式,您可以使用資訊清單檔來建立執行 AKS 市集應用程式所需的所有物件。 Kubernetes 資訊清單檔會定義叢集所需的狀態,例如要執行哪些容器映像。 資訊清單包含下列 Kubernetes 部署和服務:
- 市集前端:供客戶檢視產品和下單的 Web 應用程式。
- 產品服務:顯示產品資訊。
- 訂單服務:下單。
- Rabbit MQ:訂單佇列的訊息佇列。
注意
除非是針對生產環境的永續性儲存,否則不建議執行具狀態容器,例如 Rabbit MQ。 這裡使用具狀態容器是為了簡單起見,但我們建議使用受管理的服務,例如 Azure CosmosDB 或 Azure 服務匯流排。
建立名為
aks-store-quickstart.yaml
的檔案,然後將下列資訊清單複製進來:apiVersion: apps/v1 kind: Deployment metadata: name: rabbitmq spec: replicas: 1 selector: matchLabels: app: rabbitmq template: metadata: labels: app: rabbitmq spec: nodeSelector: "kubernetes.io/os": linux containers: - name: rabbitmq image: mcr.microsoft.com/mirror/docker/library/rabbitmq:3.10-management-alpine ports: - containerPort: 5672 name: rabbitmq-amqp - containerPort: 15672 name: rabbitmq-http env: - name: RABBITMQ_DEFAULT_USER value: "username" - name: RABBITMQ_DEFAULT_PASS value: "password" resources: requests: cpu: 10m memory: 128Mi limits: cpu: 250m memory: 256Mi volumeMounts: - name: rabbitmq-enabled-plugins mountPath: /etc/rabbitmq/enabled_plugins subPath: enabled_plugins volumes: - name: rabbitmq-enabled-plugins configMap: name: rabbitmq-enabled-plugins items: - key: rabbitmq_enabled_plugins path: enabled_plugins --- apiVersion: v1 data: rabbitmq_enabled_plugins: | [rabbitmq_management,rabbitmq_prometheus,rabbitmq_amqp1_0]. kind: ConfigMap metadata: name: rabbitmq-enabled-plugins --- apiVersion: v1 kind: Service metadata: name: rabbitmq spec: selector: app: rabbitmq ports: - name: rabbitmq-amqp port: 5672 targetPort: 5672 - name: rabbitmq-http port: 15672 targetPort: 15672 type: ClusterIP --- apiVersion: apps/v1 kind: Deployment metadata: name: order-service spec: replicas: 1 selector: matchLabels: app: order-service template: metadata: labels: app: order-service spec: nodeSelector: "kubernetes.io/os": linux containers: - name: order-service image: ghcr.io/azure-samples/aks-store-demo/order-service:latest ports: - containerPort: 3000 env: - name: ORDER_QUEUE_HOSTNAME value: "rabbitmq" - name: ORDER_QUEUE_PORT value: "5672" - name: ORDER_QUEUE_USERNAME value: "username" - name: ORDER_QUEUE_PASSWORD value: "password" - name: ORDER_QUEUE_NAME value: "orders" - name: FASTIFY_ADDRESS value: "0.0.0.0" resources: requests: cpu: 1m memory: 50Mi limits: cpu: 75m memory: 128Mi initContainers: - name: wait-for-rabbitmq image: busybox command: ['sh', '-c', 'until nc -zv rabbitmq 5672; do echo waiting for rabbitmq; sleep 2; done;'] resources: requests: cpu: 1m memory: 50Mi limits: cpu: 75m memory: 128Mi --- apiVersion: v1 kind: Service metadata: name: order-service spec: type: ClusterIP ports: - name: http port: 3000 targetPort: 3000 selector: app: order-service --- apiVersion: apps/v1 kind: Deployment metadata: name: product-service spec: replicas: 1 selector: matchLabels: app: product-service template: metadata: labels: app: product-service spec: nodeSelector: "kubernetes.io/os": linux containers: - name: product-service image: ghcr.io/azure-samples/aks-store-demo/product-service:latest ports: - containerPort: 3002 resources: requests: cpu: 1m memory: 1Mi limits: cpu: 1m memory: 7Mi --- apiVersion: v1 kind: Service metadata: name: product-service spec: type: ClusterIP ports: - name: http port: 3002 targetPort: 3002 selector: app: product-service --- apiVersion: apps/v1 kind: Deployment metadata: name: store-front spec: replicas: 1 selector: matchLabels: app: store-front template: metadata: labels: app: store-front spec: nodeSelector: "kubernetes.io/os": linux containers: - name: store-front image: ghcr.io/azure-samples/aks-store-demo/store-front:latest ports: - containerPort: 8080 name: store-front env: - name: VUE_APP_ORDER_SERVICE_URL value: "http://order-service:3000/" - name: VUE_APP_PRODUCT_SERVICE_URL value: "http://product-service:3002/" resources: requests: cpu: 1m memory: 200Mi limits: cpu: 1000m memory: 512Mi --- apiVersion: v1 kind: Service metadata: name: store-front spec: ports: - port: 80 targetPort: 8080 selector: app: store-front type: LoadBalancer
如需 YAML 資訊清單檔案的詳細資訊,請參閱部署和 YAML 資訊清單。
如果您在本地建立並儲存 YAML 檔案,則可以選取上傳/下載檔案按鈕,然後從本地文件系統選取檔案,將資訊清單檔上傳至 CloudShell 裡的預設目錄。
使用 kubectl apply 命令來部署應用程式並指定 YAML 資訊清單的名稱。
kubectl apply -f aks-store-quickstart.yaml
下列範例輸出會顯示部署和服務:
deployment.apps/rabbitmq created service/rabbitmq created deployment.apps/order-service created service/order-service created deployment.apps/product-service created service/product-service created deployment.apps/store-front created service/store-front created
測試應用程式
當應用程式執行時,Kubernetes 服務會將應用程式前端公開至網際網路。 此程序可能需要幾分鐘才能完成。
使用 kubectl get pods 命令署檢視已部署 Pod 的狀態。 讓全部 Pod 都是
Running
,再繼續。kubectl get pods
檢查市集前端應用程式的公用 IP 位址。 使用 kubectl get service 命令搭配
--watch
引數來監視進度。kubectl get service store-front --watch
服務的
store-front
輸出一開始會顯示為擱置:NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE store-front LoadBalancer 10.0.100.10 <pending> 80:30025/TCP 4h4m
當 EXTERNAL-IP 位址從暫止變成實際的公用 IP 位址時,請使用
CTRL-C
停止kubectl
監看式流程。下列範例輸出顯示指派給服務的有效公用 IP 位址:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE store-front LoadBalancer 10.0.100.10 20.62.159.19 80:30025/TCP 4h5m
若要查看 Azure 市集應用程式的實際運作情況,請開啟網頁瀏覽器並瀏覽至服務的外部 IP 位址。
選取叢集
如果您不打算進行後續的 AKS 教學課程,請清除不必要資源以避免 Azure 費用。
請使用 az group delete 命令來移除資源群組、容器服務以及所有相關資源。
az group delete --name myResourceGroup --yes --no-wait
注意
在本快速入門中,是以系統指派的受控識別 (預設身分識別選項) 來建立 AKS 叢集。 平台會管理這個身分識別,您不需要手動移除它。
下一步
在本快速入門中,您已部署 Kubernetes 叢集,接著將簡單多容器應用程式部署到此叢集。 這個範例應用程式僅供示範之用,並不代表 Kube 應用程式的全部最佳做法。 如需針對生產使用 AKS 建立完整解決方案的指引,請參閱 AKS 解決方案指引。
若要深入了解 AKS,並逐步完成部署範例的完整程式碼,請繼續 Kube 叢集教學課程。