重要
需要 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 可以基于用户的标识或目录组成员身份。
在这篇文章中,你将学会如何:
- 创建 AKS 群集
- 配置 AKS 群集
先决条件
- Azure 订阅:如果没有 Azure 订阅,请在开始之前创建 一个免费帐户 。
- Azure 服务主体: 创建服务主体,记下以下值: appId、 displayName、 密码和 租户。
安装 Ansible:执行以下选项之一:
- 在 Linux 虚拟机上安装和 配置 Ansible
- 配置 Azure Cloud Shell ,如果无权访问 Linux 虚拟机, 请使用 Ansible 创建虚拟机。
创建托管 AKS 群集
示例 playbook 创建一个资源组,并在该资源组中创建一个 AKS 群集。
将以下 playbook 保存为 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
在运行 playbook 之前,请参阅以下注意事项:
- 在
tasks内的第一部分定义一个在eastus位置的名为myResourceGroup的资源组。 - 第二部分
tasks在myResourceGroup资源组中定义了命名为myAKSCluster的 AKS 群集。 -
your_ssh_key对于占位符,以单行格式输入 RSA 公钥 - 以“ssh-rsa”开头(不含引号)。 - 使用 az aks get-versions 命令获取
aks_version占位符版本。
运行 playbook 使用 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 节点
上一节中的示例剧本定义了两个节点。 通过修改块中的count值来调整agent_pool_profiles节点数。
将以下 playbook 保存为 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
在运行 playbook 之前,请参阅以下注意事项:
-
your_ssh_key对于占位符,以单行格式输入 RSA 公钥 - 以“ssh-rsa”开头(不含引号)。
运行 playbook 使用 ansible-playbook
ansible-playbook azure_configure_aks.yml
运行 playbook 会显示类似于以下输出的结果:
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 群集
示例 playbook 将删除 AKS 群集。
将以下 playbook 保存为 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
运行 playbook 使用 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