快速入門:使用 Azure CLI 部署 Azure Container Linux (ACL) Azure Kubernetes Service (AKS) 叢集

在這個快速入門中,你會使用 Azure CLI 建立一個 Azure Kubernetes Service (AKS) 叢集,該叢集以 Azure Container Linux (ACL) 作為節點作業系統(OS)運行。 部署叢集後,你用 ACL kubectl 連線並確認 ACL 節點是否正常運作。

考慮事項與限制條件

在開始之前,請先回顧以下關於ACL的考量與限制:

先決條件

Note

你可以使用 Azure Cloud Shell或本地安裝 Azure CLI 來執行這個快速啟動中的指令。

註冊 Microsoft.ContainerService 資源提供者

您可能需要在 Azure 訂用帳戶中註冊資源提供者。 使用命令 az provider show 檢查註冊狀態。

az provider show --namespace Microsoft.ContainerService --query registrationState

如有需要,請使用指令 az provider register 註冊資源提供者。

az provider register --namespace Microsoft.ContainerService

建立資源群組

Azure 資源群組是部署及管理 Azure 資源所在的邏輯群組。 建立資源群組時,必須指定位置。 這個位置是:

  • 資源群組中繼資料的儲存位置。
  • 如果您在建立資源時未指定其他區域,這就是您的資源將在 Azure 中執行的區域。

使用 az group create 命令建立資源群組。 以下範例設定資源群組名稱、區域及 AKS 叢集名稱的環境變數,然後在指定位置建立資源群組。 你可以用你偏好的名稱和區域替換環境變數的值。

export MY_RESOURCE_GROUP_NAME="myACLResourceGroup"
export REGION="westus"
export MY_AKS_CLUSTER_NAME="myACLCluster"

az group create --name $MY_RESOURCE_GROUP_NAME --location $REGION

輸出範例:

{
  "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myACLResourceGroup",
  "location": "westus",
  "managedBy": null,
  "name": "myACLResourceGroup",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null,
  "type": "Microsoft.Resources/resourceGroups"
}

建立 ACL 叢集

使用 az aks create 帶有 --os-sku AzureContainerLinux 參數的指令建立 AKS 叢集,以設定 ACL 映像檔。

az aks create \
  --resource-group $MY_RESOURCE_GROUP_NAME \
  --name $MY_AKS_CLUSTER_NAME \
  --os-sku AzureContainerLinux \
  --node-count 3 \
  --generate-ssh-keys

幾分鐘後,命令會完成並傳回關於叢集的 JSON 格式資訊。

連線至叢集

若要管理 Kubernetes 叢集,請使用 Kubernetes 命令行用戶端 kubectl 如果您使用 Azure Cloud Shell,則 kubectl 已安裝。 若要在本機安裝 kubectl,請使用 az aks install-cli 命令。

  1. 設定 kubectl 使用 az aks get-credentials 命令連線到 Kubernetes 叢集。 此命令會下載認證,並設定 Kubernetes CLI 來使用這些認證。

    az aks get-credentials --resource-group $MY_RESOURCE_GROUP_NAME --name $MY_AKS_CLUSTER_NAME
    
  2. 使用 kubectl get 命令確認叢集的連線。 此命令會傳回叢集節點的清單。

    kubectl get nodes
    

部署應用程式

要部署應用程式,你使用清單檔案建立執行 AKS Store 應用程式所需的所有物件。 Kube 資訊清單檔會定義叢集所需的狀態,例如要執行哪些容器映像。 清單包含下列 Kubernetes 部署和服務:

  • 市集前端:供客戶檢視產品和下單的 Web 應用程式。
  • 產品服務:顯示產品資訊。
  • 訂單服務:下單。
  • Rabbit MQ:用於訂單佇列的訊息佇列系統。

Note

除非是針對生產環境的永續性儲存體,否則不建議執行具狀態容器,例如 Rabbit MQ。 這裡為了簡化使用這些服務,但我們建議使用管理服務,例如 Azure Cosmos DB 或 Azure 服務匯流排。

  1. 建立名為 aks-store-quickstart.yaml 的檔案,然後將下列資訊清單複製進來:

    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: rabbitmq
    spec:
      serviceName: rabbitmq
      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
    ---
    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
    
  2. 使用 kubectl apply 命令來部署應用程式,並指定 YAML 資訊清單的名稱。

    kubectl apply -f aks-store-quickstart.yaml
    

測試應用程式

您可以造訪公用 IP 位址或應用程式 URL 來驗證應用程式是否正在執行。

使用下列命令取得應用程式 URL:

runtime="5 minutes"
endtime=$(date -ud "$runtime" +%s)
while [[ $(date -u +%s) -le $endtime ]]
do
   STATUS=$(kubectl get pods -l app=store-front -o 'jsonpath={..status.conditions[?(@.type=="Ready")].status}')
   echo "Status: $STATUS"
   if [ "$STATUS" == 'True' ]
   then
      export IP_ADDRESS=$(kubectl get service store-front --output 'jsonpath={..status.loadBalancer.ingress[0].ip}')
      echo "Service IP Address: $IP_ADDRESS"
      break
   else
      sleep 10
   fi
done
echo "http://$IP_ADDRESS"

刪除叢集

如果你不打算看教學,請清理不必要的資源以避免 Azure 費用。

使用 az group delete 命令移除資源群組、容器服務和所有相關資源。

az group delete --name $MY_RESOURCE_GROUP_NAME --yes --no-wait

若要深入了解 AKS 適用的 ACL,請參閱 什麼是 Azure Kubernetes Service (AKS) 適用的 Azure Container Linux (ACL)?