k3s: Update k3s service configuration
PATCH 1/2 k3s: Update k3s service configuration
Update k3s.service with the latest changes from install.sh script. Add k3s-killall.sh script to stop all of the K3s containers and reset the containerd state.
The killall script cleans up containers, K3s directories, and networking components while also removing the iptables chain with all the associated rules. The cluster data will not be deleted.
--- recipes-containers/k3s/k3s/k3s-killall.sh | 82 +++++++++++++++++++++++ recipes-containers/k3s/k3s/k3s.service | 12 +++- recipes-containers/k3s/k3s_git.bb | 2 + 3 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 recipes-containers/k3s/k3s/k3s-killall.sh
recipes-containers/k3s/k3s/k3s-killall.sh new file +82 -0
@@ -0,0 +1,82 @@ +#!/bin/sh + +# Based on: k3s-killall.sh installed when running Rancher Lab's K3S install.sh +# In open-source project: https://github.com/k3s-io/k3s +# +# Original file: Copyright (c) 2021 Rancher Labs and Contributors. +# Modifications: Copyright (c) 2021 Arm Limited and Contributors. All rights reserved. +# +# Modifications: +# - Change systemd service directory location +# - Fix PID parsing to run on core image +# - Remove service stopping code (as this is intended to run as part of service +# stop) +# - Changes to resolve warnings from the ShellCheck static analysis tool +# +# SPDX-License-Identifier: Apache License 2.0 + +[ "$(id -u)" -eq 0 ] || exec sudo "$0" "$@" + +for bin in /var/lib/rancher/k3s/data/**/bin/; do + [ -d "$bin" ] && export PATH=$PATH:$bin:$bin/aux +done + +set -x + +pschildren() { + ps -e -o ppid= -o pid= | sed -e 's/^\s*//g; s/\s\s*/\t/g;' | grep -w "^$1" | cut -f2 +} + +pstree() { + for pid in "$@"; do + echo "$pid" + for child in $(pschildren "$pid"); do + pstree "$child" + done + done +} + +killtree() { + while read -r pid; do + if [ -n "${pid}" ]; then + kill -9 "${pid}" 2>/dev/null + fi + done <<EOF +$({ set +x; } 2>/dev/null; pstree "$@"; set -x;) +EOF +} + +getshims() { + ps -e -o pid= -o args= | sed -e 's/^ *//; s/\s\s*/\t/;' | grep -w '[^/]*/bin/containerd-shim' | cut -f1 +} + +killtree "$({ set +x; } 2>/dev/null; getshims; set -x)" + +# shellcheck disable=SC2016 +do_unmount_and_remove() { + set +x + while read -r _ path _; do + case "$path" in $1*) echo "$path" ;; esac + done < /proc/self/mounts | sort -r | xargs -r -t -n 1 sh -c 'umount "$0" && rm -rf "$0"' + set -x +} + +do_unmount_and_remove '/run/k3s' +do_unmount_and_remove '/var/lib/rancher/k3s' +do_unmount_and_remove '/var/lib/kubelet/pods' +do_unmount_and_remove '/var/lib/kubelet/plugins' +do_unmount_and_remove '/run/netns/cni-' + +# Remove CNI namespaces +ip netns show 2>/dev/null | grep cni- | xargs -r -t -n 1 ip netns delete + +# Delete network interface(s) that match 'master cni0' +ip link show 2>/dev/null | grep 'master cni0' | while read -r _ iface _; do + iface=${iface%%@*} + [ -z "$iface" ] || ip link delete "$iface" +done +ip link delete cni0 +ip link delete flannel.1 +ip link delete flannel-v6.1 +rm -rf /var/lib/cni/ +iptables-save | grep -v KUBE- | grep -v CNI- | iptables-restore
recipes-containers/k3s/k3s/k3s.service +11 -1
@@ -4,12 +4,17 @@ Description=Lightweight Kubernetes Documentation=https://k3s.io Requires=containerd.service After=containerd.service +After=network-online.target +Wants=network-online.target [Install] WantedBy=multi-user.target [Service] Type=notify +EnvironmentFile=-/etc/default/%N +EnvironmentFile=-/etc/sysconfig/%N +EnvironmentFile=-/etc/systemd/system/k3s.service.env KillMode=process Delegate=yes # Having non-zero Limit*s causes performance problems due to accounting overhead
@@ -21,7 +26,12 @@ TasksMax=infinity TimeoutStartSec=0 Restart=always RestartSec=5s +ExecStartPre=/bin/sh -xc '! systemctl is-enabled --quiet nm-cloud-setup.service' ExecStartPre=-/sbin/modprobe br_netfilter ExecStartPre=-/sbin/modprobe overlay ExecStart=/usr/local/bin/k3s server - +# Avoid any delay due to this service when the system is rebooting or shutting +# down by using the k3s-killall.sh script to kill all of the running k3s +# services and containers +ExecStopPost=/bin/sh -c "if systemctl is-system-running | grep -i \ + 'stopping'; then /usr/local/bin/k3s-killall.sh; fi"
recipes-containers/k3s/k3s_git.bb +2 -0
@@ -11,6 +11,7 @@ SRC_URI = "git://github.com/rancher/k3s.git;branch=release-1.22;name=k3s \ file://k3s-clean \ file://cni-containerd-net.conf \ file://0001-Finding-host-local-in-usr-libexec.patch;patchdir=src/import \ + file://k3s-killall.sh \ " SRC_URI[k3s.md5sum] = "363d3a08dc0b72ba6e6577964f6e94a5" SRCREV_k3s = "737f722315b9832e9180fa232253d28ae1f2272f"
@@ -60,6 +61,7 @@ do_install() { # ln -sr "${D}/${BIN_PREFIX}/bin/k3s" "${D}${BIN_PREFIX}/bin/ctr" ln -sr "${D}/${BIN_PREFIX}/bin/k3s" "${D}${BIN_PREFIX}/bin/kubectl" install -m 755 "${WORKDIR}/k3s-clean" "${D}${BIN_PREFIX}/bin" + install -m 755 "${WORKDIR}/k3s-killall.sh" "${D}${BIN_PREFIX}/bin" if ${@bb.utils.contains('DISTRO_FEATURES','systemd','true','false',d)}; then install -D -m 0644 "${WORKDIR}/k3s.service" "${D}${systemd_system_unitdir}/k3s.service"
Signature · 1 line
2.17.1
PATCH 2/2 k3s: Add xt-statistic kernel module to rrecommends
Include the xt-statistic kernel module required by K3S for iptables configuration.
--- recipes-containers/k3s/k3s_git.bb | 1 + 1 file changed, 1 insertion(+)
recipes-containers/k3s/k3s_git.bb +1 -0
@@ -95,6 +95,7 @@ RRECOMMENDS:${PN} = "\ kernel-module-xt-connmark \ kernel-module-vxlan \ kernel-module-xt-masquerade \ + kernel-module-xt-statistic \ " RCONFLICTS:${PN} = "kubectl"
Signature · 1 line
2.17.1
Thanks for the tweaks, they look good to me.
I have some other pending k3s changes I'm working on, but there was no reason to hold these up, so I've pulled them into master.
Bruce
Quoted 168 lines · Kamil Dziezyk
Update k3s.service with the latest changes from
install.shscript. Addk3s-killall.shscript to stop all of the K3s containers and reset the containerd state.The killall script cleans up containers, K3s directories, and networking components while also removing the iptables chain with all the associated rules. The cluster data will not be deleted.
Signed-off-by Kamil Dziezyk <[email protected]>Change-Id If1794367cabfc18fc8e3ecaf26badd4d0bc25114--- recipes-containers/k3s/k3s/k3s-killall.sh | 82 +++++++++++++++++++++++ recipes-containers/k3s/k3s/k3s.service | 12 +++- recipes-containers/k3s/k3s_git.bb | 2 + 3 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 recipes-containers/k3s/k3s/k3s-killall.shrecipes-containers/k3s/k3s/k3s-killall.sh new file +82
-0@@ -0,0 +1,82 @@ +#!/bin/sh + +# Based on: k3s-killall.sh installed when running Rancher Lab's K3S install.sh +# In open-source project: https://github.com/k3s-io/k3s +# +# Original file: Copyright (c) 2021 Rancher Labs and Contributors. +# Modifications: Copyright (c) 2021 Arm Limited and Contributors. All rights reserved. +# +# Modifications: +# - Change systemd service directory location +# - Fix PID parsing to run on core image +# - Remove service stopping code (as this is intended to run as part of service +# stop) +# - Changes to resolve warnings from the ShellCheck static analysis tool +# +# SPDX-License-Identifier: Apache License 2.0 + +[ "$(id -u)" -eq 0 ] || exec sudo "$0" "$@" + +for bin in /var/lib/rancher/k3s/data/**/bin/; do + [ -d "$bin" ] && export PATH=$PATH:$bin:$bin/aux +done + +set -x + +pschildren() { + ps -e -o ppid= -o pid= | sed -e 's/^\s*//g; s/\s\s*/\t/g;' | grep -w "^$1" | cut -f2 +} + +pstree() { + for pid in "$@"; do + echo "$pid" + for child in $(pschildren "$pid"); do + pstree "$child" + done + done +} + +killtree() { + while read -r pid; do + if [ -n "${pid}" ]; then + kill -9 "${pid}" 2>/dev/null + fi + done <<EOF +$({ set +x; } 2>/dev/null; pstree "$@"; set -x;) +EOF +} + +getshims() { + ps -e -o pid= -o args= | sed -e 's/^ *//; s/\s\s*/\t/;' | grep -w '[^/]*/bin/containerd-shim' | cut -f1 +} + +killtree "$({ set +x; } 2>/dev/null; getshims; set -x)" + +# shellcheck disable=SC2016 +do_unmount_and_remove() { + set +x + while read -r _ path _; do + case "$path" in $1*) echo "$path" ;; esac + done < /proc/self/mounts | sort -r | xargs -r -t -n 1 sh -c 'umount "$0" && rm -rf "$0"' + set -x +} + +do_unmount_and_remove '/run/k3s' +do_unmount_and_remove '/var/lib/rancher/k3s' +do_unmount_and_remove '/var/lib/kubelet/pods' +do_unmount_and_remove '/var/lib/kubelet/plugins' +do_unmount_and_remove '/run/netns/cni-' + +# Remove CNI namespaces +ip netns show 2>/dev/null | grep cni- | xargs -r -t -n 1 ip netns delete + +# Delete network interface(s) that match 'master cni0' +ip link show 2>/dev/null | grep 'master cni0' | while read -r _ iface _; do + iface=${iface%%@*} + [ -z "$iface" ] || ip link delete "$iface" +done +ip link delete cni0 +ip link delete flannel.1 +ip link delete flannel-v6.1 +rm -rf /var/lib/cni/ +iptables-save | grep -v KUBE- | grep -v CNI- | iptables-restorerecipes-containers/k3s/k3s/k3s.service +11
-1@@ -4,12 +4,17 @@ Description=Lightweight Kubernetes Documentation=https://k3s.io Requires=containerd.service After=containerd.service +After=network-online.target +Wants=network-online.target [Install] WantedBy=multi-user.target [Service] Type=notify +EnvironmentFile=-/etc/default/%N +EnvironmentFile=-/etc/sysconfig/%N +EnvironmentFile=-/etc/systemd/system/k3s.service.env KillMode=process Delegate=yes # Having non-zero Limit*s causes performance problems due to accounting overhead@@ -21,7 +26,12 @@ TasksMax=infinity TimeoutStartSec=0 Restart=always RestartSec=5s +ExecStartPre=/bin/sh -xc '! systemctl is-enabled --quiet nm-cloud-setup.service' ExecStartPre=-/sbin/modprobe br_netfilter ExecStartPre=-/sbin/modprobe overlay ExecStart=/usr/local/bin/k3s server - +# Avoid any delay due to this service when the system is rebooting or shutting +# down by using the k3s-killall.sh script to kill all of the running k3s +# services and containers +ExecStopPost=/bin/sh -c "if systemctl is-system-running | grep -i \ + 'stopping'; then /usr/local/bin/k3s-killall.sh; fi"recipes-containers/k3s/k3s_git.bb +2
-1@@ -11,6 +11,7 @@ SRC_URI = "git://github.com/rancher/k3s.git;branch=release-1.22;name=k3s \ file://k3s-clean \ file://cni-containerd-net.conf \ file://0001-Finding-host-local-in-usr-libexec.patch;patchdir=src/import \ + file://k3s-killall.sh \ " SRC_URI[k3s.md5sum] = "363d3a08dc0b72ba6e6577964f6e94a5" SRCREV_k3s = "737f722315b9832e9180fa232253d28ae1f2272f"@@ -60,6 +61,7 @@ do_install() { # ln -sr "${D}/${BIN_PREFIX}/bin/k3s" "${D}${BIN_PREFIX}/bin/ctr" ln -sr "${D}/${BIN_PREFIX}/bin/k3s" "${D}${BIN_PREFIX}/bin/kubectl" install -m 755 "${WORKDIR}/k3s-clean" "${D}${BIN_PREFIX}/bin" + install -m 755 "${WORKDIR}/k3s-killall.sh" "${D}${BIN_PREFIX}/bin" if ${@bb.utils.contains('DISTRO_FEATURES','systemd','true','false',d)}; then install -D -m 0644 "${WORKDIR}/k3s.service" "${D}${systemd_system_unitdir}/k3s.service" --2.17.1
Signature · 3 lines
- Thou shalt not follow the NULL pointer, for chaos and madness await thee at its end - "Use the force Harry" - Gandalf, Star Trek II
Hello Bruce,
Can you backport these two k3s patches to honister branch?
Thanks,
Kamil
Hi Bruce,
Any expectations to have this patch chain backported to honister?
If it helps, we can send the patches targeting honister to the mailing list.
Regards,
Diego Sueiro
________________________________
From: [email protected] <[email protected]> on behalf of Kamil Dziezyk via lists.yoctoproject.org <[email protected]>
Sent: 22 October 2021 08:37
To: [email protected] <[email protected]>
Subject: Re: [meta-virtualization] [PATCH 1/2] k3s: Update k3s service configuration
Hello Bruce,
Can you backport these two k3s patches to honister branch?
Thanks,
Kamil
Quoted 6 lines · Diego Sueiro
Hi Bruce,
Any expectations to have this patch chain backported to honister?
I've tagged them for backport, I was just busy trying to figure out why python 3.10 is breaking master.
I expect to get to them shortly.
Bruce
Quoted 19 lines
If it helps, we can send the patches targeting honister to the mailing list.
Regards,
Diego Sueiro________________________________
From: [email protected] <[email protected]> on behalf of Kamil Dziezyk via lists.yoctoproject.org <[email protected]>
Sent: 22 October 2021 08:37
To: [email protected] <[email protected]>
Subject: Re: [meta-virtualization] [PATCH 1/2] k3s: Update k3s service configurationHello Bruce,
Can you backport these two k3s patches to honister branch?
Thanks,
Kamil
Signature · 3 lines
- Thou shalt not follow the NULL pointer, for chaos and madness await thee at its end - "Use the force Harry" - Gandalf, Star Trek II
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 5 lines · Bruce Ashfield via lists.yoctoproject.org
Quoted 4 lines · Diego Sueiro
Hi Bruce,
Quoted 9 lines
Quoted 2 lines
Any expectations to have this patch chain backported to honister?
I've tagged them for backport, I was just busy trying to figure out why python 3.10 is breaking master.
I expect to get to them shortly.
... and they are now on honister as well.
Bruce
Quoted 38 lines
Bruce
Quoted 1 line
If it helps, we can send the patches targeting honister to the mailing
list.
Quoted 6 lines
Regards,
Diego Sueiro________________________________
From: [email protected] <[email protected]> on behalf of Kamil Dziezyk via lists.yoctoproject.org <[email protected]>
Quoted 2 lines
Sent: 22 October 2021 08:37
To: [email protected] <Quoted 1 line
Subject: Re: [meta-virtualization] [PATCH 1/2] k3s: Update k3s service
configuration
Quoted 9 lines
Hello Bruce,
Can you backport these two k3s patches to honister branch?
Thanks,
Kamil
--
- Thou shalt not follow the NULL pointer, for chaos and madness await thee at its end
- "Use the force Harry" - Gandalf, Star Trek II
Signature · 3 lines
- Thou shalt not follow the NULL pointer, for chaos and madness await thee at its end - "Use the force Harry" - Gandalf, Star Trek II
Thanks
Diego Sueiro
________________________________
From: [email protected] <[email protected]> on behalf of Bruce Ashfield via lists.yoctoproject.org <[email protected]>
Sent: 28 October 2021 20:32
To: Bruce Ashfield <[email protected]>
Cc: Diego Sueiro <[email protected]>; [email protected] <[email protected]>; Kamil Dzieżyk <[email protected]>; nd <[email protected]>
Subject: Re: [meta-virtualization] [PATCH 1/2] k3s: Update k3s service configuration
On Thu, Oct 28, 2021 at 8:53 AM Bruce Ashfield via lists.yoctoproject.org<http://lists.yoctoproject.org> <[email protected]<mailto:[email protected]>> wrote:
Quoted 6 lines · On Thu, Oct 28, 2021 at 4:36 AM Diego Sueiro…
Hi Bruce,
Any expectations to have this patch chain backported to honister?
I've tagged them for backport, I was just busy trying to figure out why python 3.10 is breaking master.
I expect to get to them shortly.
... and they are now on honister as well.
Bruce
Bruce
Quoted 12 lines
If it helps, we can send the patches targeting honister to the mailing list.
Regards,
Diego Sueiro________________________________
From: [email protected]<mailto:[email protected]> <[email protected]<mailto:[email protected]>> on behalf of Kamil Dziezyk via lists.yoctoproject.org<http://lists.yoctoproject.org> <[email protected]<mailto:[email protected]>>
Sent: 22 October 2021 08:37
To: [email protected]<mailto:[email protected]> <[email protected]<mailto:[email protected]>>
Subject: Re: [meta-virtualization] [PATCH 1/2] k3s: Update k3s service configuration
Quoted 7 lines
Hello Bruce,
Can you backport these two k3s patches to honister branch?
Thanks,
Kamil
--
- Thou shalt not follow the NULL pointer, for chaos and madness await thee at its end
- "Use the force Harry" - Gandalf, Star Trek II
--
- Thou shalt not follow the NULL pointer, for chaos and madness await thee at its end
- "Use the force Harry" - Gandalf, Star Trek II