教學課程:使用 Ansible 在 Azure Kubernetes Service 中設定 Azure CNI 網路功能

重要

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

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

使用 AKS,您可以使用下列網路模型來部署叢集:

  • Kubenet 網路 - 網路資源通常會建立並設定為部署 AKS 叢集。
  • Azure CNI 網路 - AKS 叢集已連線到現有的虛擬網路 (VNET) 資源和組態。

如需 AKS 中應用程式網路功能的詳細資訊,請參閱 AKS 中應用程式的網路概念。

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

  • 建立 AKS 叢集
  • 設定 Azure CNI 網路功能

必要條件

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

建立虛擬網路和子網路

本節中的範例劇本程式代碼可用來:

  • 建立虛擬網路
  • 在虛擬網路內建立子網

將下列劇本儲存為 vnet.yml

- name: Create vnet
  azure_rm_virtualnetwork:
      resource_group: "{{ resource_group }}"
      name: "{{ name }}"
      address_prefixes_cidr:
          - 10.0.0.0/8

- name: Create subnet
  azure_rm_subnet:
      resource_group: "{{ resource_group }}"
      name: "{{ name }}"
      address_prefix_cidr: 10.240.0.0/16
      virtual_network_name: "{{ name }}"
  register: subnet

在虛擬網路中建立 AKS 叢集

本節中的範例劇本程式代碼可用來:

  • 在虛擬網路內建立 AKS 叢集。

將下列劇本儲存為 aks.yml

- name: List supported kubernetes version from Azure
  azure_rm_aks_version:
      location: "{{ location }}"
  register: versions

- name: Create AKS cluster within a VNet
  azure_rm_aks:
      resource_group: "{{ resource_group }}"
      name: "{{ name }}"
      dns_prefix: "{{ name }}"
      kubernetes_version: "{{ versions.azure_aks_versions[-1] }}"
      agent_pool_profiles:
        - count: 3
          name: nodepool1
          vm_size: Standard_D2_v2
          vnet_subnet_id: "{{ vnet_subnet_id }}"
      linux_profile:
          admin_username: azureuser
          ssh_key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
      service_principal:
          client_id: "{{ lookup('ini', 'client_id section=default file=~/.azure/credentials') }}"
          client_secret: "{{ lookup('ini', 'secret section=default file=~/.azure/credentials') }}"
      network_profile:
          network_plugin: azure
          docker_bridge_cidr: 172.17.0.1/16
          dns_service_ip: 10.2.0.10
          service_cidr: 10.2.0.0/24
  register: aks

以下是使用範例劇本時要考慮的一些重要注意事項:

  • 使用 模組 azure_rm_aks_version 來尋找支援的版本。

  • vnet_subnet_id是上一節中建立的子網。

  • 劇本會從~/.ssh/id_rsa.pub載入ssh_key。 如果您修改它,請使用單行格式 - 從 「ssh-rsa」 開始 (不含引號)。

  • client_idclient_secret 值會從~/.azure/credentials載入,這是預設認證檔。 您可以將這些值設定為服務主體,或從環境變數載入這些值:

    client_id: "{{ lookup('env', 'AZURE_CLIENT_ID') }}"
    client_secret: "{{ lookup('env', 'AZURE_SECRET') }}"
    

執行範例劇本

本節中的範例劇本程式代碼可用來測試本教學課程中顯示的各種功能。

將下列劇本儲存為 aks-azure-cni.yml

---
- hosts: localhost
  vars:
      resource_group: aksansibletest
      name: aksansibletest
      location: eastus
  tasks:
     - name: Ensure resource group exists
       azure_rm_resourcegroup:
           name: "{{ resource_group }}"
           location: "{{ location }}"

     - name: Create vnet
       include_tasks: vnet.yml

     - name: Create AKS
       vars:
           vnet_subnet_id: "{{ subnet.state.id }}"
       include_tasks: aks.yml

     - name: Show AKS cluster detail
       debug:
           var: aks

以下是使用範例劇本時要考慮的一些重要注意事項:

  • aksansibletest 值變更為您的資源組名。
  • aksansibletest 值變更為 AKS 名稱。
  • eastus 值變更為您的資源群組位置。

使用 ansible-playbook 命令執行劇本:

ansible-playbook aks-azure-cni.yml

執行劇本之後,您會看到類似下列結果的輸出:

PLAY [localhost] 

TASK [Gathering Facts] 
ok: [localhost]

TASK [Ensure resource group exists] 
changed: [localhost]

TASK [Create vnet] 
included: /home/devops/aks-cni/vnet.yml for localhost

TASK [Create vnet] 
changed: [localhost]

TASK [Create subnet] 
changed: [localhost]

TASK [Create AKS] 
included: /home/devops/aks-cni/aks.yml for localhost

TASK [List supported kubernetes version from Azure] 
 [WARNING]: Azure API profile latest does not define an entry for
ContainerServiceClient

ok: [localhost]

TASK [Create AKS cluster with vnet] 
changed: [localhost]

TASK [Show AKS cluster detail] 
ok: [localhost] => {
    "aks": {
        "aad_profile": {},
        "addon": {},
        "agent_pool_profiles": [
            {
                "count": 3,
                "name": "nodepool1",
                "os_disk_size_gb": 100,
                "os_type": "Linux",
                "storage_profile": "ManagedDisks",
                "vm_size": "Standard_D2_v2",
                "vnet_subnet_id": "/subscriptions/BBBBBBBB-BBBB-BBBB-BBBB-BBBBBBBBBBBB/resourceGroups/aksansibletest/providers/Microsoft.Network/virtualNetworks/aksansibletest/subnets/aksansibletest"
            }
        ],
        "changed": true,
        "dns_prefix": "aksansibletest",
        "enable_rbac": false,
        "failed": false,
        "fqdn": "aksansibletest-0272707d.hcp.eastus.azmk8s.io",
        "id": "/subscriptions/BBBBBBBB-BBBB-BBBB-BBBB-BBBBBBBBBBBB/resourcegroups/aksansibletest/providers/Microsoft.ContainerService/managedClusters/aksansibletest",
        "kube_config": "..."
        },
        "location": "eastus",
        "name": "aksansibletest",
        "network_profile": {
            "dns_service_ip": "10.2.0.10",
            "docker_bridge_cidr": "172.17.0.1/16",
            "network_plugin": "azure",
            "network_policy": null,
            "pod_cidr": null,
            "service_cidr": "10.2.0.0/24"
        },
        "node_resource_group": "MC_aksansibletest_aksansibletest_eastus",
        "provisioning_state": "Succeeded",
        "service_principal_profile": {
            "client_id": "AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA"
        },
        "tags": null,
        "type": "Microsoft.ContainerService/ManagedClusters",
        "warnings": [
            "Azure API profile latest does not define an entry for ContainerServiceClient",
            "Azure API profile latest does not define an entry for ContainerServiceClient"
        ]
    }
}

PLAY RECAP 
localhost                  : ok=9    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

清除資源

  1. 將下列程式代碼儲存為 delete_rg.yml

    ---
    - hosts: localhost
      tasks:
        - name: Deleting resource group - "{{ name }}"
          azure_rm_resourcegroup:
            name: "{{ name }}"
            state: absent
          register: rg
        - debug:
            var: rg
    
  2. 使用 ansible-playbook 命令執行劇本 。 將佔位元取代為要刪除的資源群組名稱。 將會刪除資源群組中的所有資源。

    ansible-playbook delete_rg.yml --extra-vars "name=<resource_group>"
    

    重點︰

    • register由於劇本的變數和debug區段,因此命令完成時會顯示結果。

下一步