访问专用 Azure Kubernetes 服务 (AKS) 群集

在访问专用 AKS 群集时,需要从群集虚拟网络、对等互连网络或通过已配置的专用终结点连接到群集。 这些方法需要配置 VPN、Express Route、在群集虚拟网络中部署 jumpbox,或在另一个虚拟网络中创建专用终结点。

通过 Azure CLI,可以使用 command invoke 访问专用群集,而无需配置 VPN 或快速路由。 使用 command invoke,可以通过 Azure API 在专用群集上远程调用 kubectlhelm 等命令,而无需直接连接到群集。 Microsoft.ContainerService/managedClusters/runcommand/actionMicrosoft.ContainerService/managedclusters/commandResults/read 操作控制使用 command invoke 的权限。

通过 Azure 门户,可以使用 Run command 功能在专用群集上运行命令。 Run command 功能会使用相同的 command invoke 功能在群集上运行命令。

开始之前

在开始之前,请确保拥有以下资源和权限:

  • 现有的专用群集。 如果还没有群集,请参阅创建专用 AKS 群集
  • Azure CLI 2.24.0 或更高版本。 运行 az --version 即可查找版本。 如果需要进行安装或升级,请参阅安装 Azure CLI
  • 对群集上的 Microsoft.ContainerService/managedClusters/runcommand/actionMicrosoft.ContainerService/managedclusters/commandResults/read 角色的访问权限。

限制

通过命令 run 创建的 Pod 可使用 kustomize 为群集提供 helm 和最新兼容版本的 kubectl

command invoke 会从群集运行命令,因此以此方式运行的任何命令会受到所配置的网络限制和其他限制的约束。 确保群集中存在足够的节点和资源来调度此命令 pod。

注意

command invoke 的输出大小限制为 512kB。

在 AKS 群集上运行命令

使用 command invoke 运行单个命令

  • 使用 az aks command invoke --command 命令在群集上运行命令。 以下示例命令在 myResourceGroupmyPrivateCluster 群集上运行 kubectl get pods -n kube-system 命令。

    az aks command invoke \
      --resource-group myResourceGroup \
      --name myPrivateCluster \
      --command "kubectl get pods -n kube-system"
    

使用 command invoke 运行多个命令

  • 使用 az aks command invoke --command 命令在群集上运行多个命令。 以下示例命令在 myResourceGroupmyPrivateCluster 群集上运行三个 helm 命令。

    az aks command invoke \
      --resource-group myResourceGroup \
      --name myPrivateCluster \
      --command "helm repo add bitnami https://charts.bitnami.com/bitnami && helm repo update && helm install my-release bitnami/nginx"
    

使用 command invoke 运行命令并附加文件或目录

  • 使用带 --file 参数的 az aks command invoke --command 命令运行带有附加文件或目录的命令。 以下示例命令在 myResourceGroupmyPrivateCluster 群集上运行 kubectl apply -f deployment.yaml -n defaultdeployment.yaml 文件是从运行 az aks command invoke 的开发计算机上当前目录中附加的。

    az aks command invoke \
      --resource-group myResourceGroup \
      --name myPrivateCluster \
      --command "kubectl apply -f deployment.yaml -n default" \
      --file deployment.yaml
    

使用 command invoke 运行附加了当前目录中所有文件的命令

  • 使用带 --file 参数的 az aks command invoke --command 命令运行附加了当前目录中所有文件的命令。 以下示例命令在 myResourceGroupmyPrivateCluster 群集上运行 kubectl apply -f deployment.yaml configmap.yaml -n defaultdeployment.yamlconfigmap.yaml 文件是运行 az aks command invoke 的开发计算机上当前目录的一部分。

    az aks command invoke \
      --resource-group myResourceGroup \
      --name myPrivateCluster \
      --command "kubectl apply -f deployment.yaml configmap.yaml -n default" \
      --file .