Partager via


Script pour configurer la machine pour gérer le cluster Kubernetes avec Azure Arc

Pour communiquer avec et configurer un cluster Kubernetes avec Azure Arc pour Edge RAG, configurez n’importe quelle machine Windows avec des outils tels qu’Azure CLI, kubectl et Helm. La machine doit être en mesure d’atteindre le cluster Kubernetes.

Exemple de script

Exécutez cet exemple de script pour configurer une machine Windows pour effectuer les tâches suivantes :

  1. Créez un dossier.
  2. Téléchargez Azure CLI, Helm, kubectl et les extensions Azure CLI aksarc et Kubernetes-extension.
  3. Connectez-vous à Azure à l’aide d’Azure CLI avec l’abonnement et l’ID de locataire.
  4. Connectez-vous au cluster Kubernetes avec Azure Arc.
  5. Enregistrez les informations d’identification pour kubectl.
# Fill the subscription and resource group information 

$sub = ""  # Add subscription guid 

$tenantid = " " # Add tenant id 

$rg = " " # Add resourcegroup name 

$k8scluster =  "<AKS Arc cluster name>" # Add name of the AKS ARC cluster 

$rootfolder = "C:\microsoft.arc.rag" # Define the root folder 

 
function Trace-Output { param([string]$message) Write-Host "$(Get-Date -Format 'HH:mm:ss') - $message" -ForegroundColor Yellow } 

 
# Create the root folder if it doesn't exist 

if (-Not (Test-Path -Path $rootfolder)) { 

    Trace-Output "Creating root folder at $rootfolder"  

    New-Item -ItemType Directory -Path $rootfolder 

} else { 

    Trace-Output "Root folder already exists at $rootfolder"  

} 

 
cd $rootfolder 

# Set the path to the AZ CLI installer 

$azCliInstaller = "$rootfolder\AzureCLI.msi" 

$logFile = "$rootfolder\AzureCLIInstall.log" 

# Set AZ CLI into path 

# Define the correct installation path 

$azCliPath = "C:\Program Files\Microsoft SDKs\Azure\CLI2\wbin" 

 
# Download and install AZ CLI 64-bit if it doesn't exist 

if (!(Test-Path $azCliPath) ) { 

    Trace-Output "Downloading and installing Azure CLI 64-bit"  

    Invoke-WebRequest -Uri "https://aka.ms/installazurecliwindowsx64" -OutFile $azCliInstaller 

    $argStr = "/I $azCliInstaller /quiet /l*v $logFile" 

    Start-Process msiexec.exe -Wait -ArgumentList $argStr 

    Remove-Item .\AzureCLI.msi -Force 

} else { 

    Trace-Output "Azure CLI is already installed" 

} 

# Check if the path exists, if not add AZ cli into the path. 

if (Test-Path $azCliPath) { 

 

    if ($env:Path -notlike "*$azCliPath*") { 

        Trace-Output "Azure CLI path added to the PATH environment variable." 

        $env:Path += ";$azCliPath;" 

    } else { 

        Trace-Output "Azure CLI path is already in the PATH environment variable." 

    } 

 
    # Get the current PATH environment variable 

    $currentPath = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine) 

 
    # Check if the Azure CLI path is already in the PATH variable 

    if ($currentPath -notlike "*$azCliPath*") {         

        # Add the Azure CLI path to the PATH variable 

        $newPath = "$currentPath;$azCliPath" 

        [System.Environment]::SetEnvironmentVariable("Path", $newPath, [System.EnvironmentVariableTarget]::Machine) 

        Trace-Output "Azure CLI path added to Machine level PATH environment variable." 

    } else { 

        Trace-Output "Azure CLI path is already in Machine level PATH environment variable." 

    } 

} else { 

    Trace-Output "Azure CLI installation path not found." 

    return 

} 

 
# Install AZ CLI extensions if not installed 

$extensions = @("aksarc", "k8s-extension") 

foreach ($extension in $extensions) { 

$extensionInstalled = az extension show --name $extension -o none 2>$null 
 

if ($LASTEXITCODE -ne 0) { 

       Trace-Output "Installing Azure CLI extension: $extension" 

        az extension add --name $extension 

    } else { 

        Trace-Output "Azure CLI extension $extension is already installed"  

    } 

} 

 

# Set the paths for kubectl and helm 

$kubectlPath = "$rootfolder\kubectl.exe" 

$helmPath = "$rootfolder\helm.exe" 

 

# Download kubectl if it doesn't exist 

if (-Not (Test-Path -Path $kubectlPath)) { 

    Trace-Output "Downloading kubectl" 

    Invoke-WebRequest -Uri "https://dl.k8s.io/release/v1.32.0/bin/windows/amd64/kubectl.exe" -OutFile $kubectlPath 

} else { 

    Trace-Output "kubectl is already downloaded" 

} 

 

# Download helm if it doesn't exist 

if (-Not (Test-Path -Path $helmPath)) { 

    Trace-Output "Downloading helm" 

    Invoke-WebRequest -Uri "https://get.helm.sh/helm-v3.17.2-windows-amd64.zip" -OutFile "$rootfolder\helm.zip" 

    Expand-Archive -Path "$rootfolder\helm.zip" -DestinationPath $rootfolder 

    Move-Item -Path "$rootfolder\windows-amd64\helm.exe" -Destination $helmPath 

    Remove-Item -Path "$rootfolder\helm.zip" 

    Remove-Item -Path "$rootfolder\windows-amd64" -Recurse 

} else { 

    Trace-Output "helm is already downloaded" 

} 

 

# Add the root folder to the system PATH if not already added 

$envPath = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine) 

if (-Not $envPath.Contains($rootfolder)) { 

    $env:Path += ";$rootfolder;" 

    Trace-Output "Adding $rootfolder to system PATH" 

    [System.Environment]::SetEnvironmentVariable("Path", "$envPath;$rootfolder", [System.EnvironmentVariableTarget]::Machine) 

} else { 

    Trace-Output "$rootfolder is already in the system PATH" 

} 

 
# login to azure using device code and set subscription 

az login --use-device-code -t $tenantId 

az account set -s $sub 

az account show 
 

# Save aksarc cluster credentials into kubeconfig for kubectl and helm to work 

Trace-Output "aks cluster name = $k8scluster " 

az aksarc get-credentials --name $k8scluster --resource-group $rg --admin  --only-show-errors