Kube-#02.0.1 Windows上的一些Minikube脚本

脚本安装Minikube

function Install-Minikube {
    if(Get-CommandExists("winget")) {
        Write-Host "Found winget. Using winget for installation."
        if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
            Write-Host "You are not running in an elevated shell and winget need admin rights."
            Write-Host "Please complete installation in the new window"
            Start-Process "cmd" -ArgumentList @("/c winget install minikube -y & pause") -Wait -PassThru -Verb RunAs
        } else {
            Start-Process "cmd" -ArgumentList @("/c winget install minikube -y & pause") -Wait -PassThru -NoNewWindow
        }
        Write-Host "Minikube has been installed."
    } elseif (Get-CommandExists("choco")) {
        Write-Host "Found Chocolatey. Using chocolatey for installation."
        if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
            Write-Host "You are not running in an elevated shell and chocolatey need admin rights."
            Write-Host "Please complete installation in the new window"
            Start-Process "cmd" -ArgumentList @("/c choco install minikube -y & pause") -Wait -PassThru -Verb RunAs
        } else {
            Start-Process "cmd" -ArgumentList @("/c choco install minikube -y & pause") -Wait -PassThru -NoNewWindow
        }
        Write-Host "Minikube has been installed."
    } else {
        Write-Host "No winget or chocolatey. Downloading installer for manual install."
        Start-BitsTransfer -Source "https://storage.googleapis.com/minikube/releases/latest/minikube-installer.exe" -Destination "$env:TEMP\minikube-install.exe"
        Write-Host "Starting installer and waiting for it to finish..."
        cmd /c "$env:TEMP\minikube-install.exe"
        Write-Host "Removing installer..."
        Remove-Item "$env:TEMP\minikube-install.exe"
    }
}

启动minikube集群。Windows可以使用HyperV,比docker快一些

function Start-MinikubeCluster {
    Param (
        [Parameter()]
        [string]$Memory = "4g",

        [Parameter()]
        [string]$CPU = "4",

        [Parameter()]
        [string]$Nodes = "1",

        [Parameter()]
        [Switch]$HyperV = $false
    )
    
    if($HyperV) {
        if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
            Write-Host "You are not running in an elevated shell and using HyperV need admin rights."
            Write-Host "Please complete startup in the new window"
            Start-Process "cmd" -ArgumentList @("/c", "minikube start --cpus=$CPU --memory=$memory --driver=hyperv --nodes=$Nodes && pause") -Verb RunAs -Wait
        } else {
            cmd /c "minikube start --cpus=$CPU --memory=$memory --driver=docker --nodes=$Nodes --driver=hyperv"
        }
    } else {
        cmd /c "minikube start --cpus=$CPU --memory=$memory --driver=docker --nodes=$Nodes"
    }
}

关闭并清理Minikube集群

function Stop-MinikubeCluster {
    minikube stop
    minikube delete --all --purge
}

在本地用Docker Compose构建镜像,并复制到Minikube环境中

function Install-BuildAndLoadMinikubeImages {
    $ImageList =
        "list",
        "your", "images",
        "here"
    
    Write-Host "Building Docker images with docker-compose..."
    docker compose build

    Write-Host "Loading image into Minikube"
    ForEach($image in $ImageList) {
        minikube image load $image
        Write-Host "$image loaded."
    }

    Write-Host "Minikube now has the following images"
    minikube image list
}

Leave A Comment