演習 - AKS 上でクラスターのスケーラビリティを有効にする

完了

AKS クラスターでのクラスター オートスケーラーの有効化

  1. ブラウザーで Azure Cloud Shell を開き、[Bash] を選びます。

  2. az group create コマンドを使用して、Azure リソース グループを作成します。

    az group create --name myResourceGroup --location eastus
    
  3. az aks create コマンドと --enable-cluster-autoscaler フラグを使用して、クラスター オートスケーラーが有効になっている新しい AKS クラスターを作成してください。

    az aks create --resource-group myResourceGroup --name myAKSCluster --enable-addons monitoring --enable-msi-auth-for-monitoring --enable-cluster-autoscaler --min-count 1 --max-count 10 --generate-ssh-keys
    

    クラスターの作成には数分かかります。

  4. az aks get-credentials コマンドを使ってクラスターに接続します。

    az aks get-credentials --resource-group myResourceGroup --name myAKSCluster
    
  5. kubectl get nodes コマンドを使用して、クラスター内のノードを表示してください。

    kubectl get nodes
    

    出力は次の出力例のようになります。

    NAME                                STATUS   ROLES   AGE   VERSION
    aks-nodepool1-12345678-vmss000000   Ready    agent   1m    v1.26.6
    aks-nodepool1-12345678-vmss000001   Ready    agent   1m    v1.26.6
    aks-nodepool1-12345678-vmss000002   Ready    agent   1m    v1.26.6
    

サンプル アプリケーションをデプロイする

  1. Cloud Shell で、touch コマンドを使って、deployment.yml という Kubernetes デプロイ用のマニフェスト ファイルを作成してください。

    touch deployment.yml
    
  2. code コマンドを使用してマニフェスト ファイルを開いてください。

    code deployment.yml
    
  3. マニフェスト ファイルに次のコードを貼り付けてください。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: contoso-website
    spec:
      replicas: 35
      selector:
        matchLabels:
          app: contoso-website
      template:
        metadata:
          labels:
            app: contoso-website
        spec:
          containers:
            - image: mcr.microsoft.com/mslearn/samples/contoso-website
              name: contoso-website
              resources:
                requests:
                  cpu: 100m
                  memory: 128Mi
                limits:
                  cpu: 250m
                  memory: 256Mi
              ports:
                - containerPort: 80
                  name: http
    
  4. ファイルを保存して、エディターを閉じます。

クラスター オートスケーラー プロファイルを更新する

自動スケーラー プロファイルを微調整するには、その構成で一連のフラグを設定します。 使用可能なフラグの一覧は、「クラスター オートスケーラー プロファイルを使用する」を参照してください。 たとえば、オートスケーラーを更新して、保留中のポッドを確認するためのポーリング時間を短縮し、以前の状態からスケールダウンするまでの待ち時間を減らします。

  1. --cluster-autoscaler-profile フラグを指定した az aks update コマンドを使用して、クラスター オートスケーラー プロファイルを更新してください。

    az aks update --resource-group myResourceGroup --name myAKSCluster --cluster-autoscaler-profile scan-interval=5s scale-down-unready-time=5m scale-down-delay-after-add=5m
    
  2. kubectl scale deployment コマンドを使用してデプロイをスケールダウンしてください。

    kubectl scale deployment contoso-website --replicas 5
    
  3. kubectl describe cm コマンドを使用して、cluster-autoscaler-status 構成マップのクラスター オートスケーラー ログとクエリを確認してください。

    kubectl describe cm cluster-autoscaler-status -n kube-system
    

    スケールダウンする前に、出力は次の出力例のようになります。

    Cluster-autoscaler status at 2023-11-09 20:08:14.892961701 +0000 UTC:
    Cluster-wide:
      Health:      Healthy (ready=3 unready=0 notStarted=0 longNotStarted=0 registered=3 longUnregistered=0)
                   LastProbeTime:      2023-11-09 19:56:57.890988498 +0000 UTC m=+1673.465985892
                   LastTransitionTime: 2023-11-09 19:45:09.593593337 +0000 UTC m=+774.168590731
      ScaleUp:     NoActivity (ready=3 registered=3)
                   LastProbeTime:      2023-11-09 19:56:57.890988498 +0000 UTC m=+1673.465985892
                   LastTransitionTime: 2023-11-09 19:45:09.593593337 +0000 UTC m=+774.168590731
      ScaleDown:   CandidatesPresent (candidates=3)
                   LastProbeTime:      2023-11-09 19:56:57.890988498 +0000 UTC m=+1673.465985892
                   LastTransitionTime: 2023-11-09 19:56:52.440038763 +0000 UTC m=+1101.015036157
    
  4. オートスケーラーがスケール ダウンを完了するまで約 5 分待ってから、前の kubectl describe cm を再実行してください。

    スケール ダウン後、出力は次の出力例のようになります。

    Cluster-autoscaler status at 2023-11-09 20:14:39.123206413 +0000 UTC:
    Cluster-wide:
      Health:      Healthy (ready=1 unready=0 (resourceUnready=0) notStarted=0 longNotStarted=0 registered=1 longUnregistered=0)
                   LastProbeTime:      2023-11-09 20:14:39.113206413 +0000 UTC m=+2150.697175601
                   LastTransitionTime: 2023-11-09 19:45:09.593593337 +0000 UTC m=+774.168590731
      ScaleUp:     NoActivity (ready=1 registered=1)
                   LastProbeTime:      2023-11-09 20:14:39.113206413 +0000 UTC m=+2150.697175601
                   LastTransitionTime: 2023-11-09 19:45:09.593593337 +0000 UTC m=+774.168590731
      ScaleDown:   NoCandidates (candidates=0)
                   LastProbeTime:      2023-11-09 20:14:39.113206413 +0000 UTC m=+2150.697175601
                   LastTransitionTime: 2023-11-09 20:07:08.79828656 +0000 UTC m=+1718.924760896
    
  5. kubectl get nodes コマンドを使用して、クラスター内のノードを表示してください。

    kubectl get nodes
    

    ノードの数は 1 に減り、出力は次の出力例のようになります。

    NAME                                STATUS   ROLES   AGE   VERSION
    aks-nodepool1-12345678-vmss000000   Ready    agent   37m    v1.26.6