教學課程:使用 Ansible 在 Azure 中設定 Azure Kubernetes Service (AKS) 叢集

重要

本文中需要 Ansible 2.8 (或更新版本)才能執行範例劇本。

Azure Kubernetes Service (AKS) 可讓您輕鬆地在 Azure 中部署受控 Kubernetes 叢集。 AKS 可降低管理 Kubernetes 的複雜性和作業負荷,因為是由 Azure 負責大部分的工作。 以主控的 Kubernetes 服務形式,Azure 會為您處理像是健康狀態監視和維護等重要工作。 Kubernetes 主機是由 Azure 管理。 您只需要管理及維護代理程式節點。 作為受控 Kubernetes 服務,AKS 是免費的 - 您只需支付叢集中的代理程式節點費用;不適用於主圖形。

AKS 可以設定為使用 Microsoft Entra ID 進行用戶驗證。 設定之後,您可以使用 Microsoft Entra 驗證令牌登入 AKS 叢集。 RBAC 可以根據使用者的身分識別或目錄群組成員資格。

在本文中,您將學會如何:

  • 建立 AKS 叢集
  • 設定 AKS 叢集

必要條件

  • Azure 訂用帳戶:如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶
  • Azure 服務主體:建立服務主體,並記下下列值:appIddisplayName密碼租使用者

建立受控 AKS 叢集

範例劇本會在資源群組內建立資源群組和 AKS 叢集。

將下列劇本儲存為 azure_create_aks.yml

- name: Create Azure Kubernetes Service
  hosts: localhost
  connection: local
  vars:
    resource_group: myResourceGroup
    location: eastus
    aks_name: myAKSCluster
    username: azureuser
    ssh_key: "your_ssh_key"
    client_id: "your_client_id"
    client_secret: "your_client_secret"
    aks_version: aks_version
  tasks:
  - name: Create resource group
    azure_rm_resourcegroup:
      name: "{{ resource_group }}"
      location: "{{ location }}"
  - name: Create a managed Azure Container Services (AKS) cluster
    azure_rm_aks:
      name: "{{ aks_name }}"
      location: "{{ location }}"
      resource_group: "{{ resource_group }}"
      dns_prefix: "{{ aks_name }}"
      kubernetes_version: "{{aks_version}}"
      linux_profile:
        admin_username: "{{ username }}"
        ssh_key: "{{ ssh_key }}"
      service_principal:
        client_id: "{{ client_id }}"
        client_secret: "{{ client_secret }}"
      agent_pool_profiles:
        - name: default
          count: 2
          vm_size: Standard_D2_v2
      tags:
        Environment: Production

在執行劇本之前,請參閱下列附註:

  • 內的tasks第一個區段會定義位置內eastus名為 myResourceGroup 的資源群組。
  • 內的 tasks 第二個區段會定義資源群組內名為 myAKSClustermyResourceGroup AKS 叢集。
  • your_ssh_key針對佔位元元,以單行格式輸入您的 RSA 公鑰 - 從 “ssh-rsa” 開始(不含引號)。
  • aks_version針對佔位元元,請使用 az aks get-versions 命令。

使用 ansible-playbook 執行劇本

ansible-playbook azure_create_aks.yml

執行劇本會顯示類似下列輸出的結果:

PLAY [Create AKS] 

TASK [Gathering Facts] 
ok: [localhost]

TASK [Create resource group] 
changed: [localhost]

TASK [Create an Azure Container Services (AKS) cluster] 
changed: [localhost]

PLAY RECAP 
localhost                  : ok=3    changed=2    unreachable=0    failed=0

調整 AKS 節點

上一節中的範例劇本會定義兩個節點。 您可以藉由修改 區塊中的agent_pool_profiles值來調整count節點數目。

將下列劇本儲存為 azure_configure_aks.yml

- name: Scale AKS cluster
  hosts: localhost
  connection: local
  vars:
    resource_group: myResourceGroup
    location: eastus
    aks_name: myAKSCluster
    username: azureuser
    ssh_key: "your_ssh_key"
    client_id: "your_client_id"
    client_secret: "your_client_secret"
  tasks:
  - name: Scaling an existed AKS cluster
    azure_rm_aks:
        name: "{{ aks_name }}"
        location: "{{ location }}"
        resource_group: "{{ resource_group }}"
        dns_prefix: "{{ aks_name }}"
        linux_profile:
          admin_username: "{{ username }}"
          ssh_key: "{{ ssh_key }}"
        service_principal:
          client_id: "{{ client_id }}"
          client_secret: "{{ client_secret }}"
        agent_pool_profiles:
          - name: default
            count: 3
            vm_size: Standard_D2_v2

在執行劇本之前,請參閱下列附註:

  • your_ssh_key針對佔位元元,以單行格式輸入您的 RSA 公鑰 - 從 “ssh-rsa” 開始(不含引號)。

使用 ansible-playbook 執行劇本

ansible-playbook azure_configure_aks.yml

執行劇本會顯示類似下列輸出的結果:

PLAY [Scale AKS cluster] 

TASK [Gathering Facts] 
ok: [localhost]

TASK [Scaling an existed AKS cluster] 
changed: [localhost]

PLAY RECAP 
localhost                  : ok=2    changed=1    unreachable=0    failed=0

刪除受控 AKS 叢集

範例劇本會刪除 AKS 叢集。

將下列劇本儲存為 azure_delete_aks.yml

- name: Delete a managed Azure Container Services (AKS) cluster
  hosts: localhost
  connection: local
  vars:
    resource_group: myResourceGroup
    aks_name: myAKSCluster
  tasks:
  - name:
    azure_rm_aks:
      name: "{{ aks_name }}"
      resource_group: "{{ resource_group }}"
      state: absent

使用 ansible-playbook 執行劇本

ansible-playbook azure_delete_aks.yml

執行劇本會顯示類似下列輸出的結果:

PLAY [Delete a managed Azure Container Services (AKS) cluster] 

TASK [Gathering Facts] 
ok: [localhost]

TASK [azure_rm_aks] 

PLAY RECAP 
localhost                  : ok=2    changed=1    unreachable=0    failed=0

下一步