教學課程:使用 Ansible 在 Azure Kubernetes Service 中設定角色型訪問控制 (RBAC) 角色

重要

本文中需要 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 可以根據使用者的身分識別或目錄群組成員資格。

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

  • 建立已啟用 Microsoft Entra 識別碼的 AKS 叢集
  • 在叢集中設定 RBAC 角色

必要條件

  • Azure 訂用帳戶:如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶
  • Azure 服務主體:建立服務主體,並記下下列值:appIddisplayName密碼租使用者
  • 安裝 RedHat OpenShift 連結庫 - pip install openshift

設定 AKS 驗證的 Microsoft Entra 識別碼

設定 AKS 驗證的 Microsoft Entra 識別符時,會設定兩個 Microsoft Entra 應用程式。 此作業必須由 Azure 租用戶系統管理員完成。 如需詳細資訊,請參閱 整合 Microsoft Entra ID 與 AKS

從 Azure 租使用者管理員取得下列值:

  • 伺服器應用程式秘密
  • 伺服器應用程式識別碼
  • 用戶端應用程式識別碼
  • 租用戶識別碼

執行範例劇本需要這些值。

建立 AKS 叢集

在本節中,您會使用 Microsoft Entra 應用程式建立 AKS。

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

  • 劇本會從~/.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-create.yml

- name: Create resource group
  azure_rm_resourcegroup:
      name: "{{ resource_group }}"
      location: "{{ location }}"

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

- name: Create AKS cluster with RBAC enabled
  azure_rm_aks:
      resource_group: "{{ resource_group }}"
      name: "{{ name }}"
      dns_prefix: "{{ name }}"
      enable_rbac: yes
      kubernetes_version: "{{ versions.azure_aks_versions[-1] }}"
      agent_pool_profiles:
        - count: 3
          name: nodepool1
          vm_size: Standard_D2_v2
      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') }}"
      aad_profile:
          client_app_id: "{{ client_app_id }}"
          server_app_id: "{{ server_app_id }}"
          server_app_secret: "{{ server_app_secret }}"
          tenant_id: "{{ app_tenant_id }}"
  register: aks

- name: Save cluster user config
  copy:
      content: "{{ aks.kube_config }}"
      dest: "aks-{{ name }}-kubeconfig-user"

- name: Get admin config of AKS
  azure_rm_aks_facts:
      resource_group: "{{ resource_group }}"
      name: "{{ name }}"
      show_kubeconfig: admin
  register: aks

- name: Save the kubeconfig
  copy:
      content: "{{ aks.aks[0].kube_config }}"
      dest: "aks-{{ name }}-kubeconfig"

取得 Microsoft Entra 物件識別碼

若要建立 RBAC 系結,您必須先取得 Microsoft Entra 物件識別碼。

  1. 登入 Azure 入口網站

  2. 在頁面頂端的搜尋字段中,輸入 Microsoft Entra ID

  3. 按一下 Enter

  4. 在 [ 管理] 功能表中,選取 [ 使用者]。

  5. 在 [名稱] 欄位中,搜尋您的帳戶。

  6. 在 [ 名稱] 資料行中,選取您帳戶的連結。

  7. 在 [ 身分 識別] 區段中,複製 [ 對象標識符]。

    Copy the Microsoft Entra Object ID.

建立 RBAC 系結

在本節中,您會在 AKS 中建立角色系結或叢集角色系結。

將下列劇本儲存為 kube-role.yml

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: cluster-admins
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- apiGroup: rbac.authorization.k8s.io
  kind: User
  name: <your-aad-account>

<your-aad-account> 您的 Microsoft Entra 租使用者 物件識別碼取代佔位符。

儲存下列劇本 - 將新角色部署至 AKS - 為 aks-kube-deploy.yml

- name: Apply role to AKS
  k8s:
      src: kube-role.yml
      kubeconfig: "aks-{{ name }}-kubeconfig"

執行範例劇本

本節列出呼叫本文中建立之工作的完整範例劇本。

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

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

     - name: Create AKS
       vars:
           client_app_id: <client id>
           server_app_id: <server id>
           server_app_secret: <server secret>
           app_tenant_id: <tenant id>
       include_tasks: aks-create.yml

     - name: Enable RBAC
       include_tasks: aks-kube-deploy.yml

在 區 vars 段中,以您的 Microsoft Entra 資訊取代下列佔位元:

  • <client id>
  • <server id>
  • <server secret>
  • <tenant id>

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

ansible-playbook aks-rbac.yml

驗證結果

在本節中,您會使用 kubectl 列出本文中建立的節點。

在終端機提示字元中輸入下列命令:

kubectl --kubeconfig aks-aksansibletest-kubeconfig-user get nodes

此命令會將您導向至驗證頁面。 使用您的 Azure 帳戶進行登入。

驗證之後,kubectl 會以類似下列結果的方式列出節點:

To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code XXXXXXXX to authenticate.
NAME                       STATUS   ROLES   AGE   VERSION
aks-nodepool1-33413200-0   Ready    agent   49m   v1.12.6
aks-nodepool1-33413200-1   Ready    agent   49m   v1.12.6
aks-nodepool1-33413200-2   Ready    agent   49m   v1.12.6

清除資源

不再需要時,請刪除本文中建立的資源。

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

---
- hosts: localhost
  vars:
      name: aksansibletest
      resource_group: aksansibletest
  tasks:
      - name: Clean up resource group
        azure_rm_resourcegroup:
            name: "{{ resource_group }}"
            state: absent
            force: yes
      - name: Remove kubeconfig
        file:
            state: absent
            path: "aks-{{ name }}-kubeconfig"

使用 ansible-playbook 執行劇本

ansible-playbook cleanup.yml

下一步