Skip to content

Instantly share code, notes, and snippets.

@jonaskello
Last active December 15, 2023 05:55
Show Gist options
  • Save jonaskello/138d0b00aec844b045ab8f413fcd3a6e to your computer and use it in GitHub Desktop.
Save jonaskello/138d0b00aec844b045ab8f413fcd3a6e to your computer and use it in GitHub Desktop.
Install k8s windows nodes
# Make sure you have enabled "Expose hardware assisted virtualization to the guest OS" for the VMWare CPU
Write-Host "##############################`nInstalling features: Containers, Hyper-V, Hyper-V-PowerShell`n##############################`n"
Install-WindowsFeature Containers
Install-WindowsFeature Hyper-V
Install-WindowsFeature Hyper-V-PowerShell
Write-Host "##############################`nDisabling firewall`n##############################`n"
Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False
Write-Host "##############################`nRestarting...`n##############################`n"
Restart-Computer -force
Write-Host "##############################`nInstalling ContainerD`n##############################`n"
# Download and extract desired containerd Windows binaries
$ContainerDVersion="1.6.4"
curl.exe -L https://github.com/containerd/containerd/releases/download/v$ContainerDVersion/containerd-$ContainerDVersion-windows-amd64.tar.gz -o containerd-windows-amd64.tar.gz
tar.exe xvf .\containerd-windows-amd64.tar.gz
# Add containerd to path
$global:ContainerDPath = "$env:ProgramFiles\containerd"
$env:Path += ";$global:ContainerDPath"
[Environment]::SetEnvironmentVariable("Path", $env:Path, [System.EnvironmentVariableTarget]::Machine)
# Copy and configure
Copy-Item -Path ".\bin\" -Destination "$global:ContainerDPath" -Recurse -Force
containerd.exe config default | Out-File "$global:ContainerDPath\config.toml" -Encoding ascii
# Review the configuration. Depending on setup you may want to adjust:
# - the sandbox_image (Kubernetes pause image)
# - cni bin_dir and conf_dir locations
Get-Content "$global:ContainerDPath\config.toml"
#config file fixups
$config = Get-Content "$global:ContainerDPath\config.toml"
$config = $config -replace "bin_dir = (.)*$", "bin_dir = `"c:/opt/cni/bin`""
$config = $config -replace "conf_dir = (.)*$", "conf_dir = `"c:/etc/cni/net.d`""
$config | Set-Content "$global:ContainerDPath\config.toml" -Force
# Create dirs for cni
mkdir -Force c:\opt\cni\bin | Out-Null
mkdir -Force c:\etc\cni\net.d | Out-Null
# Register and start service
containerd.exe --register-service
Start-Service containerd
Write-Host "##############################`nInstalling crictl`n##############################`n"
$crictl_version="1.24.1"
# Install crictl from the cri-tools project which is required so that kubeadm can talk to the CRI endpoint.
curl.exe -LO https://github.com/kubernetes-sigs/cri-tools/releases/download/v$crictl_version/crictl-v$crictl_version-windows-amd64.tar.gz
tar xvf crictl-v$crictl_version-windows-amd64.tar.gz
mv crictl.exe "$global:ContainerDPath"
# Configure crictl
mkdir -Force "$home\.crictl"
@"
runtime-endpoint: npipe://./pipe/containerd-containerd
image-endpoint: npipe://./pipe/containerd-containerd
timeout: 10
#debug: true
"@ | Set-Content "$home\.crictl\crictl.yaml" -Force
crictl.exe info
Write-Host "##############################`nInstalling wins, kubelet, and kubeadm`n##############################`n"
$k8s_version="1.23.6"
# Install wins, kubelet, and kubeadm
curl.exe -LO https://raw.githubusercontent.com/kubernetes-sigs/sig-windows-tools/master/kubeadm/scripts/PrepareNode.ps1
.\PrepareNode.ps1 -KubernetesVersion v$k8s_version -ContainerRuntime containerD
# Done - please remember to add '--cri-socket "npipe:////./pipe/containerd-containerd"' to your kubeadm join command
# Join the node (run this on the windows worker node)
kubeadm join mymaster:6443 --token xxxxx --discovery-token-ca-cert-hash sha256:xxxxxx --cri-socket "npipe:////./pipe/containerd-containerd"
#Add a taint to the windows worker so linux daemonsets are not scheduled on it (do it on a linux node)
kubectl taint nodes mynode os=Win1809:NoSchedule

flannel upgrade

Instructions on how to upgrade:

https://github.com/flannel-io/flannel/blob/master/Documentation/upgrade.md

To follow that approach one just needs to have a definition of the current version of flannel and the new one. kubectl delete -f .yaml and kubectl create -f .yaml will do the thing

The instructions on how to install flannel always installs the latest version of the manifests so we need to backtrack and find what we have actuall installed.

Getting new files

We need new versions of

  • kube-flannel.yml (this is the linux flannel)
  • flannel-overlay.yml (this is the windows flannel)

Linux: Use history to find the latest version of this file that has the version of flannel you are installing: https://github.com/flannel-io/flannel/blob/master/Documentation/kube-flannel.yml. For example for 0.14.0 it is this version: https://github.com/flannel-io/flannel/blob/584c19aebe302db6318e2063e1359a78a63a4ecf/Documentation/kube-flannel.yml

Windows: Make sure flannel-overlay.yml is using hostprocess and not the old wins with script version. At the time of writing this was located here: https://github.com/kubernetes-sigs/sig-windows-tools/blob/master/hostprocess/flannel/flanneld/flannel-overlay.yml

IMPORTANT: After we get the new files we need to patch them

Patch linux file

According to instructions here the linux file must be patched:

https://kubernetes.io/docs/tasks/administer-cluster/kubeadm/adding-windows-nodes/#configuring-flannel

Need to change this in kube-flannel.yml (linux):


net-conf.json: |
{
"Network": "10.244.0.0/16",
"Backend": {
"Type": "vxlan",
"VNI": 4096,
"Port": 4789
}
}

Patch windows file

No patch is needed for hostprocess.

Upgrading

So we need the original files that were installed, and the new files with patches from above, and then run this:

kubectl delete -f original/flannel-overlay.yml
kubectl delete -f original/kube-flannel.yml
# wait a bit
kubectl apply -f new/kube-flannel.yml
kubectl delete -f new/flannel-overlay.yml
# https://v1-20.docs.kubernetes.io/docs/tasks/administer-cluster/kubeadm/upgrading-windows-nodes/
### *** NOTE: ***
### Before running this script you should drain the node:
###
### kubectl drain myworkernode --ignore-daemonsets --delete-emptydir-data
###
### And after this script you should uncordon it:
###
### kubectl uncordon myworkernode
###
# POWERSHELL
$env:VERSION = '1.24.1'
# Upgrade kubeadm
curl.exe -Lo C:\k\kubeadm.exe https://dl.k8s.io/v$env:VERSION/bin/windows/amd64/kubeadm.exe
# Upgrade the kubelet configuration
kubeadm upgrade node
# Upgrade kubelet
stop-service kubelet
curl.exe -Lo C:\k\kubelet.exe https://dl.k8s.io/v$env:VERSION/bin/windows/amd64/kubelet.exe
restart-service kubelet
# Upgrade kube-proxy
# This is only needed once for all windows worker nodes
# From a LINUX machine with access to the Kubernetes API, run the following, again replacing v1.20.13 with your desired version:
VERSION=1.24.1 && curl -L https://github.com/kubernetes-sigs/sig-windows-tools/releases/latest/download/kube-proxy.yml | sed "s/VERSION/v$VERSION/g" | kubectl apply -f -
@ghbeta
Copy link

ghbeta commented Oct 17, 2022

Hi, first want to thank you to share these script, it helps a lot. i tried last several days to create a k8s cluster with mixed linux and windows node locally, using the latest k8s 1.25.3. i get the same error as yours from this issue (kubernetes-sigs/sig-windows-tools#128) hcnCreateNetwork failed in Win32: The object already exists.
i tried the scripts here just modify k8s version and containerD version to its latest release. after setting up i am getting another error that kube-proxy-windows can't start due to the cri not initialized error. the crictl info also gives me an error told me there is no configuration found at c:\etc\cni\net.d. this actually should be the key solution to delete this folder?

did i miss someting here? thanks for your work and time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment