From e5b9708d004092ac384cc73d33c479af2786ff39 Mon Sep 17 00:00:00 2001 From: Naman Agarwal Date: Thu, 1 Jun 2023 16:18:58 +0530 Subject: [PATCH] Add containerd support to PrepareNode script Signed-off-by: Naman Agarwal --- docs/windows.md | 7 +++-- hack/windows/Prepare-Node.ps1 | 51 ++++++++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/docs/windows.md b/docs/windows.md index 2bfd7f92eea..1ecb092f044 100644 --- a/docs/windows.md +++ b/docs/windows.md @@ -313,12 +313,15 @@ Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False Firstly, install wins, kubelet, kubeadm using script `PrepareNode.ps1` provided by kubernetes. The third component [`wins`](https://github.com/rancher/wins) is used to run kube-proxy and antrea-agent on Windows host inside the Windows -container. +container.Specify the Node IP , Kubernetes Version and Container Runtime while +running the script.You can also specify whether you want to install OVS on the +node. ```powershell # Example: curl.exe -LO "https://github.com/kubernetes-sigs/sig-windows-tools/releases/download/v0.1.5/PrepareNode.ps1" -.\PrepareNode.ps1 -KubernetesVersion v1.23.5 +.\Prepare-Node.ps1 -KubernetesVersion v1.25.0 -InstallOVS -NodeIP 192.168.1.10 -ContainerRuntime ContainerD + ``` #### 4. Prepare Node environment needed by antrea-agent diff --git a/hack/windows/Prepare-Node.ps1 b/hack/windows/Prepare-Node.ps1 index 92ffcb07bd4..4a2ff9f6430 100644 --- a/hack/windows/Prepare-Node.ps1 +++ b/hack/windows/Prepare-Node.ps1 @@ -22,8 +22,11 @@ Install OVS .PARAMETER NodeIP The node ip used by kubelet +.PARAMETER ContainerRuntime +The Container runtime that Kubernets will use + .EXAMPLE -PS> .\Prepare-Node.ps1 -KubernetesVersion v1.18.0 -InstallOVS -NodeIP 192.168.1.10 +PS> .\Prepare-Node.ps1 -KubernetesVersion v1.18.0 -InstallOVS -NodeIP 192.168.1.10 -ContainerRuntime ContainerD #> @@ -32,6 +35,9 @@ Param( [parameter(Mandatory = $true, HelpMessage="Node IP")] [string] $NodeIP, [parameter(Mandatory = $false)] [switch] $InstallOVS = $false, [parameter(Mandatory = $false, HelpMessage="Kubernetes download")] [string] $KubernetesURL="dl.k8s.io" + [parameter(HelpMessage="Container runtime that Kubernets will use")] + [ValidateSet("ContainerD", "Docker")] + [string] $ContainerRuntime = "Docker" ) $ErrorActionPreference = 'Stop' @@ -50,6 +56,20 @@ If (Get-Service kubelet -ErrorAction SilentlyContinue) { exit 0 } +if ($ContainerRuntime -eq "Docker") { + if (-not(Test-Path "//./pipe/docker_engine")) { + Write-Error "Docker service was not detected - please install start Docker before calling +PrepareNode.ps1 with -ContainerRuntime Docker" + exit 1 + } +} elseif ($ContainerRuntime -eq "ContainerD") { + if (-not(Test-Path "//./pipe/containerd-containerd")) { + Write-Error "ContainerD service was not detected - please install and start ContainerD +before calling PrepareNode.ps1 with -ContainerRuntime ContainerD" + exit 1 + } +} + if (!$KubernetesVersion.StartsWith("v")) { $KubernetesVersion = "v" + $KubernetesVersion } @@ -71,8 +91,15 @@ DownloadFile "$global:KubernetesPath\kubeadm.exe" "https://$KubernetesURL/$Kuber DownloadFile "$global:KubernetesPath\wins.exe" https://github.com/rancher/wins/releases/download/v0.0.4/wins.exe # Create host network to allow kubelet to schedule hostNetwork pods -Write-Host "Creating Docker host network" -docker network create -d nat host +if ($ContainerRuntime -eq "Docker") { + Write-Host "Creating Docker host network" + docker network create -d nat host +} elseif ($ContainerRuntime -eq "ContainerD") { + Write-Host "Creating ContainerD host network" + DownloadFile "c:\k\hns.psm1" https://github.com/Microsoft/SDN/raw/master/Kubernetes/windows/hns.psm1 + Import-Module "c:\k\hns.psm1" + New-HnsNetwork -Type NAT -Name nat +} Write-Host "Registering wins service" wins.exe srv app run --register @@ -86,17 +113,21 @@ New-Item -path C:\var\lib\kubelet\etc\kubernetes\pki -type SymbolicLink -value C $StartKubeletFileContent = '$FileContent = Get-Content -Path "/var/lib/kubelet/kubeadm-flags.env" $global:KubeletArgs = $FileContent.Trim("KUBELET_KUBEADM_ARGS=`"") -$netId = docker network ls -f name=host --format "{{ .ID }}" +$global:containerRuntime = {{CONTAINER_RUNTIME}} + +if ($global:containerRuntime -eq "Docker") { + $netId = docker network ls -f name=host --format "{{ .ID }}" -if ($netId.Length -lt 1) { + if ($netId.Length -lt 1) { docker network create -d nat host + } } -& C:\k\Prepare-ServiceInterface.ps1 -InterfaceAlias "HNS Internal NIC" - -$cmd = "C:\k\kubelet.exe $global:KubeletArgs --cert-dir=$env:SYSTEMDRIVE\var\lib\kubelet\pki --config=/var/lib/kubelet/config.yaml --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf --hostname-override=$(hostname) --pod-infra-container-image=`"mcr.microsoft.com/oss/kubernetes/pause:1.3.0`" --enable-debugging-handlers --cgroups-per-qos=false --enforce-node-allocatable=`"`" --network-plugin=cni --resolv-conf=`"`" --log-dir=/var/log/kubelet --logtostderr=false --image-pull-progress-deadline=20m --node-ip=$env:NODE_IP" +$cmd = "C:\k\kubelet.exe $global:KubeletArgs --cert-dir=$env:SYSTEMDRIVE\var\lib\kubelet\pki --config=/var/lib/kubelet/config.yaml --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf --hostname-override=$(hostname) --pod-infra-container-image=`"mcr.microsoft.com/oss/kubernetes/pause:1.4.1`" --enable-debugging-handlers --cgroups-per-qos=false --enforce-node-allocatable=`"`" --resolv-conf=`"`" --log-dir=/var/log/kubelet --logtostderr=false --container-runtime=remote --container-runtime-endpoint=npipe:\\\\.\\pipe\\containerd-containerd --node-ip=$env:NODE_IP --feature-gates=WindowsHostProcessContainers=true" Invoke-Expression $cmd' +$StartKubeletFileContent = $StartKubeletFileContent -replace "{{CONTAINER_RUNTIME}}", +"`"$ContainerRuntime`"" Set-Content -Path $global:StartKubeletScript -Value $StartKubeletFileContent Write-Host "Installing nssm" @@ -123,9 +154,9 @@ nssm set kubelet DependOnService docker New-NetFirewallRule -Name kubelet -DisplayName 'kubelet' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 10250 # Create netadapter for kube-proxy, the default full name of the adapter is "vEthernet (HNS Internal NIC)" -& ./Prepare-ServiceInterface.ps1 +& .\Prepare-ServiceInterface.ps1 -InterfaceAlias "HNS Internal NIC" if ($InstallOVS) { Write-Host "Installing OVS" & .\Install-OVS.ps1 -} +} \ No newline at end of file