Skip to content

Make kubelet starting as a windows service #1752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ kubernetes_bins:
- kubeadm
- kubectl
- kubelet
- kube-log-runner
18 changes: 12 additions & 6 deletions images/capi/ansible/windows/roles/kubernetes/tasks/sc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@
- name: Install kubelet as service
ansible.windows.win_service:
name: kubelet
start_mode: manual
start_mode: auto
path: >
{{ kubernetes_install_path }}\kubelet.exe --windows-service
"{{ kubernetes_install_path }}\kube-log-runner.exe" --log-file={{ systemdrive.stdout | trim }}/var/log/kubelet/kubelet.log
{{kubernetes_install_path }}\kubelet.exe --windows-service
--cert-dir={{ systemdrive.stdout | trim }}/var/lib/kubelet/pki
--config={{ systemdrive.stdout | trim }}/var/lib/kubelet/config.yaml
--bootstrap-kubeconfig={{ systemdrive.stdout | trim }}/etc/kubernetes/bootstrap-kubelet.conf
--kubeconfig={{ systemdrive.stdout | trim }}/etc/kubernetes/kubelet.conf
--hostname-override=$(hostname) --pod-infra-container-image=`"{{ pause_image }}`"
--enable-debugging-handlers --cgroups-per-qos=false --enforce-node-allocatable=`"`"
--resolv-conf=`"`" --log-dir={{ systemdrive.stdout | trim }}/var/log/kubelet
--logtostderr=false {{ additional_kubelet_args if additional_kubelet_args is defined }}
--pod-infra-container-image="{{ pause_image }}"
--enable-debugging-handlers --cgroups-per-qos=false --enforce-node-allocatable=""
--container-runtime-endpoint="npipe:////./pipe/containerd-containerd"
--resolv-conf=""

- name: Create file to restart kubelet as a windows service

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add this file in both sc.yml and nssm.yml? Only one is needed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will revert the change in sc.yaml as this is not the one we will use

ansible.windows.win_template:
src: templates/RestartKubelet.ps1
dest: "{{ kubernetes_install_path }}\\RestartKubelet.ps1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2025 The Kubernetes Authors.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Need to keep sync with StartKubelet.ps1
$FileContent = Get-Content -Path "$env:SYSTEMDRIVE/var/lib/kubelet/kubeadm-flags.env"
$kubeAdmArgs = $FileContent.TrimStart('KUBELET_KUBEADM_ARGS=').Trim('"')

$args = "--cert-dir=$env:SYSTEMDRIVE/var/lib/kubelet/pki",
"--config=$env:SYSTEMDRIVE/var/lib/kubelet/config.yaml",
"--bootstrap-kubeconfig=$env:SYSTEMDRIVE/etc/kubernetes/bootstrap-kubelet.conf",
"--kubeconfig=$env:SYSTEMDRIVE/etc/kubernetes/kubelet.conf",
"--hostname-override=$(hostname)",
"--pod-infra-container-image=`"{{ pause_image }}`"",
"--enable-debugging-handlers",
"--cgroups-per-qos=false",
"--enforce-node-allocatable=`"`"",
"--resolv-conf=`"`"",
"--windows-service"

$KubeletArgListStr = ($args -join " ") + " $kubeAdmArgs"
$KubeletArgListStr = $KubeletArgListStr.Replace("`"", "\`"")
# Used by sc.exe to create the service
$KubeletCommandLine = "`"" + "\`"" + "$env:SYSTEMDRIVE\k\kube-log-runner.exe" + "\`" " + "--log-file=/var/log/kubelet/kubelet.log " + "$env:SYSTEMDRIVE\k\kubelet.exe " + $KubeletArgListStr + "`""

# Write-Output $kubeletCommandLine
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Write-Output $kubeletCommandLine

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer to keep it here as the parameters (kubeletcommdnline) send to sc.exe tend to be tricky, and we can use this to check that kind of commandline is used to start the kubelet service, when we need to do some debugging

$null = sc.exe stop kubelet
$null = sc.exe delete kubelet
for ($i = 0; $i -lt 10; $i++) {
$service = Get-Service -Name kubelet -ErrorAction SilentlyContinue
if ($null -eq $service) {
Write-Host "kubelet service deleted successfully, restarting"
sc.exe create kubelet binPath= $KubeletCommandLine start= auto depend= containerd
sc.exe start kubelet
return
}
else {
Write-Host "Waiting for service to be fully deleted... (attempt $($i + 1)/10)"
}
Start-Sleep -Seconds 3
}


Write-Host "kubelet service failed to restart."
2 changes: 1 addition & 1 deletion images/capi/packer/config/windows/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"prepull": "true",
"runtime": "containerd",
"ssh_source_url": "",
"windows_service_manager": "nssm",
"windows_service_manager": "windows_service",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or maybe I should make it applied to azure only in the begining?

"windows_updates_categories": "",
"windows_updates_kbs": ""
}