重要
需要 Ansible 2.8(或更高版本)才能运行本文中示例的 playbooks。
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 服务主体: 创建服务主体,记下以下值: appId、 displayName、 密码和 租户。
安装 Ansible:执行以下选项之一:
- 在 Linux 虚拟机上安装和 配置 Ansible
- 配置 Azure Cloud Shell ,如果无权访问 Linux 虚拟机, 请使用 Ansible 创建虚拟机。
-
安装 RedHat OpenShift 库 -
pip install openshift
为 AKS 身份验证配置Microsoft Entra ID
为 AKS 身份验证配置Microsoft Entra ID 时,将配置两个Microsoft Entra 应用程序。 此作必须由 Azure 租户管理员完成。 有关详细信息,请参阅 将 Microsoft Entra ID 与 AKS 集成。
从 Azure 租户管理员获取以下值:
- 服务器应用机密
- 服务器应用 ID
- 客户端应用 ID
- 租户 ID
运行示例手册需要这些值。
创建 AKS 群集
在本部分中,你将使用 Microsoft Entra 应用程序创建 AKS。
下面是使用示例 playbook 时需要考虑的一些注意事项:
该 playbook 从
~/.ssh/id_rsa.pub加载ssh_key。 如果对其进行修改,请使用单行格式 - 以“ssh-rsa”开头(不含引号)。从
~/.azure/credentials加载client_id值和client_secret值,这是默认凭据文件。 可以将这些值设置为服务主体,或从环境变量加载这些值: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。
登录到 Azure 门户。
在页面顶部的搜索字段中,输入 Microsoft Entra ID。
单击
Enter。在 “管理 ”菜单中,选择“ 用户”。
在名称字段中,搜索您的帐户。
在 “名称 ”列中,选择帐户的链接。
在 “标识 ”部分中,复制 对象 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 的 playbook 保存为 aks-kube-deploy.yml:
- name: Apply role to AKS
k8s:
src: kube-role.yml
kubeconfig: "aks-{{ name }}-kubeconfig"
运行示例剧本
本部分列出了完整的示例手册,用于调用本文中创建的任务。
将以下 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"
运行 playbook 使用 ansible-playbook
ansible-playbook cleanup.yml