教程:使用 Ansible 在 Azure Kubernetes 服务 (AKS) 中配置基于角色的访问控制 (RBAC) 角色

重要

运行本文中的示例 playbook 需要 Ansible 2.8(或更高版本)。

可使用 Azure Kubernetes 服务 (AKS) 在 Azure 中轻松地部署托管的 Kubernetes 群集。 AKS 通过将大量管理工作量卸载到 Azure,来降低管理 Kubernetes 所产生的复杂性和操作开销。 作为一个托管 Kubernetes 服务,Azure 可以自动处理运行状况监视和维护等关键任务。 Kubernetes 主节点由 Azure 管理。 用户仅管理和维护代理节点。 作为托管型 Kubernetes 服务,AKS 是免费的 - 只需支付群集中代理节点的费用,不需要支付主节点的费用。

可以将 AKS 配置为使用 Microsoft Entra ID 进行用户身份验证。 配置完成后,即可使用 Microsoft Entra 身份验证令牌登录到 AKS 群集。 RBAC 可基于用户标识或目录组成员身份。

在本文中,学习如何:

  • 创建已启用 Microsoft Entra ID 的 AKS 群集
  • 在群集中配置 RBAC 角色

先决条件

  • Azure 订阅:如果没有 Azure 订阅,请在开始之前创建一个免费帐户。
  • Azure 服务主体创建服务主体,记下以下值:appIddisplayNamepasswordtenant
  • 安装 RedHat OpenShift 库 - pip install openshift

为 AKS 身份验证配置 Microsoft Entra ID

为 AKS 身份验证配置 Microsoft Entra ID 时,将配置两个 Microsoft Entra 应用程序。 此操作必须由 Azure 租户管理员完成。 有关详细信息,请参阅将 Microsoft Entra ID 与 AKS 集成

通过 Azure 租户管理员获取以下内容:

  • 服务器应用密码
  • 服务器应用 ID
  • 客户端应用 ID
  • 租户 ID

需要这些值来运行示例 playbook。

创建 AKS 群集

在本部分中,将使用 Microsoft Entra 应用程序创建 AKS。

下面是使用示例 playbook 时要考虑的部分关键注意事项:

  • playbook 通过 ~/.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') }}"
    

将以下 playbook 保存为 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 对象 ID

若要创建 RBAC 绑定,首先需要获取 Microsoft Entra 对象 ID。

  1. 登录到 Azure 门户

  2. 在页面顶部的搜索字段中,输入 Microsoft Entra ID

  3. 单击 Enter

  4. 在“管理”菜单中选择“用户”

  5. 在名称字段中,搜索帐户。

  6. 在“名称”列中,选择帐户链接

  7. 在“标识”部分,复制“对象 ID”

创建 RBAC 绑定

在本部分中,将在 AKS 中创建角色绑定或群集角色绑定。

将以下 playbook 保存为 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 租户对象 ID

aks-kube-deploy.yml 形式保存下列将新角色部署到 AKS 的 playbook:

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

运行示例 playbook

本部分列出了用于调用本文所创任务的完整示例 playbook。

将以下 playbook 保存为 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 命令运行完整的 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 运行 playbook

ansible-playbook cleanup.yml

后续步骤