Partilhar via


Conectar-se a um cluster de Integração VNet do Servidor de API usando o Azure Private Link

A integração de VNet do API Server permite que você coloque o IP do plano de controle dentro de sua própria VNet. O padrão descrito aqui estende essa capacidade a mais VNets ao encadear o Private Link. É útil para topologias hub-and-spoke, redes de compilação dedicadas ou VNets jump-host que devem administrar clusters de produção sem abrir o servidor de API para a Internet pública.

Este artigo aplica-se apenas a clusters criados com a integração de rede virtual do API Server e mostra como:

  • Implante um cluster AKS privado com a integração VNet do API Server.
  • Exponha o servidor de API por meio de um PLS (Serviço de Link Privado) dentro da rede virtual do cluster.
  • Crie um ponto de extremidade privado (PE) noutra rede virtual.
  • Configure o DNS privado para que as ferramentas do Kubernetes resolvam o FQDN privado do cluster dentro da rede remota.

Para clusters privados que não usam a integração de VNet do API Server, consulte Criar um cluster AKS privado.

Disponibilidade da região

A Integração de VNet do Servidor de API está atualmente disponível em um subconjunto de regiões do Azure e está sujeita a limites de capacidade regionais. Antes de começar, confirme se a região de destino é suportada. Para obter mais informações, consulte API Server VNet Integration.

Pré-requisitos

Requisito Mínimo
Azure CLI (Interface de Linha de Comando da Azure) 2.73.0
Permissões Colaborador + Colaborador de Rede em ambas as subscrições

Se você usar servidores DNS personalizados, adicione o IP virtual 168.63.129.16 do Azure como um encaminhador upstream.

Definir variáveis de ambiente

Defina as seguintes variáveis de ambiente para uso ao longo deste artigo. Sinta-se à vontade para substituir os valores de marcadores de posição pelos seus.

LOCATION="westus3"

# Resource groups
AKS_RG="aks-demo-rg"
REMOTE_RG="client-demo-rg"

# AKS cluster
AKS_CLUSTER="aks-private"
AKS_SUBNET="aks-subnet"

# Private Link Service
PLS_NAME="apiserver-pls"
PLS_SUBNET="pls-subnet"
PLS_PREFIX="10.225.0.0/24"

# Remote VNet
REMOTE_VNET="client-vnet"
REMOTE_SUBNET="client-subnet"
REMOTE_VNET_PREFIX="192.168.0.0/16"
REMOTE_SUBNET_PREFIX="192.168.1.0/24"
PE_NAME="aks-pe"
PE_CONN_NAME="aks-pe-conn"

# DNS
DNS_ZONE="private.${LOCATION}.azmk8s.io"
DNS_LINK="dns-link"

Criar grupos de recursos

# Create resource groups for the AKS cluster
az group create --name $AKS_RG --location $LOCATION

# Create a resource group for the remote VNet
az group create --name $REMOTE_RG --location $LOCATION

Implantar um cluster privado com integração de VNet do API Server

Importante

A integração de VNet do API Server requer sua própria sub-rede. Se você não fornecer um, o AKS o criará automaticamente no grupo de recursos do nó.

az aks create \
  --name $AKS_CLUSTER \
  --resource-group $AKS_RG \
  --location $LOCATION \
  --enable-private-cluster \
  --enable-apiserver-vnet-integration

Depois de o cluster concluir o provisionamento, capture o grupo de recursos do nó gerado automaticamente, o nome da rede virtual do cluster e o rótulo FQDN privado.

AKS_NODE_RG=$(az aks show -g $AKS_RG -n $AKS_CLUSTER --query nodeResourceGroup -o tsv)
AKS_VNET=$(az network vnet list --resource-group $AKS_NODE_RG --query '[0].name' -o tsv)
DNS_RECORD=$(az aks show -g $AKS_RG -n $AKS_CLUSTER --query privateFqdn -o tsv | cut -d'.' -f1,2)
FRONTEND_IP_CONFIG_ID=$(az network lb show \
  --name kube-apiserver \
  --resource-group $AKS_NODE_RG \
  --query "frontendIPConfigurations[0].id" \
  -o tsv)

Adicione uma sub-rede dedicada para o PLS e desative as políticas de rede, que não são suportadas em sub-redes de Link Privado.

Crie o PLS e aponte-o para o balanceador de carga interno do kube-apiserver que o AKS criou para o plano de controlo.

# Subnet for the PLS
az network vnet subnet create \
  --name $PLS_SUBNET \
  --vnet-name $AKS_VNET \
  --resource-group $AKS_NODE_RG \
  --address-prefixes $PLS_PREFIX \
  --disable-private-link-service-network-policies

# PLS pointing to the API‑server ILB
az network private-link-service create \
  --name $PLS_NAME \
  --resource-group $AKS_NODE_RG \
  --vnet-name $AKS_VNET \
  --subnet $PLS_SUBNET \
  --lb-frontend-ip-configs $FRONTEND_IP_CONFIG_ID \
  --location $LOCATION

Criar um Private Endpoint (PE) na VNet remota

# Remote VNet and subnet
az network vnet create \
  --name $REMOTE_VNET \
  --resource-group $REMOTE_RG \
  --location $LOCATION \
  --address-prefixes $REMOTE_VNET_PREFIX

az network vnet subnet create \
  --name $REMOTE_SUBNET \
  --vnet-name $REMOTE_VNET \
  --resource-group $REMOTE_RG \
  --address-prefixes $REMOTE_SUBNET_PREFIX

# Private Endpoint
PLS_ID=$(az network private-link-service show \
  --name $PLS_NAME \
  --resource-group $AKS_NODE_RG \
  --query id -o tsv)

az network private-endpoint create \
  --name $PE_NAME \
  --resource-group $REMOTE_RG \
  --vnet-name $REMOTE_VNET \
  --subnet $REMOTE_SUBNET \
  --private-connection-resource-id $PLS_ID \
  --connection-name $PE_CONN_NAME \
  --location $LOCATION

Quando o ponto de extremidade privado concluir o provisionamento, anote sua ID de interface de rede (NIC) para que você possa recuperar o endereço IP privado alocado.

PE_NIC_ID=$(az network private-endpoint show \
  --name $PE_NAME \
  --resource-group $REMOTE_RG \
  --query 'networkInterfaces[0].id' \
  --output tsv)

# Capture the IP from the NIC
PE_IP=$(az network nic show \
  --ids $PE_NIC_ID \
  --query 'ipConfigurations[0].privateIPAddress' \
  --output tsv)

Configurar DNS privado

# Create or reuse the regional DNS zone
az network private-dns zone create \
  --name $DNS_ZONE \
  --resource-group $REMOTE_RG

az network private-dns record-set a create \
  --name $DNS_RECORD \
  --zone-name $DNS_ZONE \
  --resource-group $REMOTE_RG

az network private-dns record-set a add-record \
  --record-set-name $DNS_RECORD \
  --zone-name $DNS_ZONE \
  --resource-group $REMOTE_RG \
  --ipv4-address $PE_IP

# Link zone to the remote VNet
REMOTE_VNET_ID=$(az network vnet show \
  --name $REMOTE_VNET \
  --resource-group $REMOTE_RG \
  --query id -o tsv)

az network private-dns link vnet create \
  --name $DNS_LINK \
  --zone-name $DNS_ZONE \
  --resource-group $REMOTE_RG \
  --virtual-network $REMOTE_VNET_ID \
  --registration-enabled false

Testar a ligação

Se tentar testar a ligação localmente, poderá receber um erro porque a zona DNS não está ligada à sua rede local.

az aks get-credentials --resource-group $AKS_RG --name $AKS_CLUSTER
kubectl get nodes

Implantar uma VM de teste na VNet remota

Para confirmar o caminho do Ponto Final Privado, implante uma VM de teste na VNet remota e use-a para se conectar ao cluster AKS.

# Create Network Security Group that allows inbound SSH (TCP 22)
az network nsg create \
  --name "${REMOTE_VNET}-nsg" \
  --resource-group $REMOTE_RG \
  --location $LOCATION

az network nsg rule create \
  --nsg-name "${REMOTE_VNET}-nsg" \
  --resource-group $REMOTE_RG \
  --name allow-ssh \
  --priority 100 \
  --access Allow \
  --protocol Tcp \
  --direction Inbound \
  --source-address-prefixes '*' \
  --destination-port-ranges 22

# Associate the NSG with the remote subnet
az network vnet subnet update \
  --name $REMOTE_SUBNET \
  --vnet-name $REMOTE_VNET \
  --resource-group $REMOTE_RG \
  --network-security-group "${REMOTE_VNET}-nsg"

# Create a small Ubuntu VM in that subnet (with a public IP for quick SSH)
az vm create \
  --resource-group $REMOTE_RG \
  --name test-vm \
  --image Ubuntu2204 \
  --size Standard_B2s \
  --admin-username azureuser \
  --generate-ssh-keys \
  --vnet-name $REMOTE_VNET \
  --subnet $REMOTE_SUBNET \
  --public-ip-sku Standard

# Capture the public IP address
VM_PUBLIC_IP=$(az vm show -d -g $REMOTE_RG -n test-vm --query publicIps -o tsv)

Conecte-se à VM e teste a conexão

ssh -i ~/.ssh/id_rsa azureuser@$VM_PUBLIC_IP

# Inside the VM
# Install Azure CLI 
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash

# Install kubectl
sudo az aks install-cli

# re-export the aks variables
export AKS_RG="aks-demo-rg"
export AKS_CLUSTER="aks-private"

# login to Azure. Follow the instructions to authenticate
az login

# Get the AKS credentials
az aks get-credentials --resource-group $AKS_RG --name $AKS_CLUSTER

# Test the connection
kubectl get nodes

# You should see the AKS nodes

# Exit the VM
exit

Limpeza de recursos

Para evitar cobranças contínuas do Azure, exclua os grupos de recursos usando o az group delete comando.

az group delete --name $AKS_RG --yes --no-wait
az group delete --name $REMOTE_RG --yes --no-wait

Próximos passos