Answers: <p>If your setup does not have systemd or openrc, but for example init (from busybox)...
  You must take a look on <a href="https://get.k3s.io/" rel="nofollow noreferrer">k3s script</a> and adapt this <code>k3s-killall.sh</code> for
  your init system:</p>
  <pre><code># --- create killall script ---
  create_killall() {
      [ &quot;${INSTALL_K3S_BIN_DIR_READ_ONLY}&quot; = true ] &amp;&amp; return
      info &quot;Creating killall script ${KILLALL_K3S_SH}&quot;
      $SUDO tee ${KILLALL_K3S_SH} &gt;/dev/null &lt;&lt; \EOF
  #!/bin/sh
  [ $(id -u) -eq 0 ] || exec sudo $0 $@
  
  K3S_DATA_DIR=${K3S_DATA_DIR:-/var/lib/rancher/k3s}
  
  for bin in ${K3S_DATA_DIR}/data/**/bin/; do
      [ -d $bin ] &amp;&amp; export PATH=$PATH:$bin:$bin/aux
  done
  
  set -x
  
  for service in /etc/systemd/system/k3s*.service; do
      [ -s $service ] &amp;&amp; systemctl stop $(basename $service)
  done
  
  for service in /etc/init.d/k3s*; do
      [ -x $service ] &amp;&amp; $service stop
  done
  
  pschildren() {
      ps -e -o ppid= -o pid= | \
      sed -e 's/^\s*//g; s/\s\s*/\t/g;' | \
      grep -w &quot;^$1&quot; | \
      cut -f2
  }
  
  pstree() {
      for pid in $@; do
          echo $pid
          for child in $(pschildren $pid); do
              pstree $child
          done
      done
  }
  
  killtree() {
      kill -9 $(
          { set +x; } 2&gt;/dev/null;
          pstree $@;
          set -x;
      ) 2&gt;/dev/null
  }
  
  remove_interfaces() {
      # Delete network interface(s) that match 'master cni0'
      ip link show 2&gt;/dev/null | grep 'master cni0' | while read ignore iface ignore; do
          iface=${iface%%@*}
          [ -z &quot;$iface&quot; ] || ip link delete $iface
      done
  
      # Delete cni related interfaces
      ip link delete cni0
      ip link delete flannel.1
      ip link delete flannel-v6.1
      ip link delete kube-ipvs0
      ip link delete flannel-wg
      ip link delete flannel-wg-v6
  
      # Restart tailscale
      if [ -n &quot;$(command -v tailscale)&quot; ]; then
          tailscale set --advertise-routes=
      fi
  }
  
  getshims() {
      ps -e -o pid= -o args= | sed -e 's/^ *//; s/\s\s*/\t/;' | grep -w &quot;${K3S_DATA_DIR}&quot;'/data/[^/]*/bin/containerd-shim' | cut -f1
  }
  
  killtree $({ set +x; } 2&gt;/dev/null; getshims; set -x)
  
  do_unmount_and_remove() {
      set +x
      while read -r _ path _; do
          case &quot;$path&quot; in $1*) echo &quot;$path&quot; ;; esac
      done &lt; /proc/self/mounts | sort -r | xargs -r -t -n 1 sh -c 'umount -f &quot;$0&quot; &amp;&amp; rm -rf &quot;$0&quot;'
      set -x
  }
  
  do_unmount_and_remove '/run/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&gt;/dev/null | grep cni- | xargs -r -t -n 1 ip netns delete
  
  remove_interfaces
  
  rm -rf /var/lib/cni/
  iptables-save | grep -v KUBE- | grep -v CNI- | grep -iv flannel | iptables-restore
  ip6tables-save | grep -v KUBE- | grep -v CNI- | grep -iv flannel | ip6tables-restore
  EOF
      $SUDO chmod 755 ${KILLALL_K3S_SH}
      $SUDO chown root:root ${KILLALL_K3S_SH}
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2024-09-04T10:41:07.033Z
Answers: 2024-09-04T10:41:07.033Z
Answers: PiotrBzdrega
Answers: <p>There is a systemd shutdown task (<code>/etc/systemd/system/k3s.service</code>) which stops the server, but not the k8s resources (pods, networks, etc). For this there is a utility script <code>k3s-killall.sh</code> which should be at <code>/usr/local/bin/k3s-killall.sh</code></p>
  
Answers: CC BY-SA 4.0
Answers: 2021-09-30T09:49:38.727Z
Answers: On Ubuntu you can stop the k3s service with `sudo systemctl stop k3s`, and stop it restarting when you reboot with `sudo systemctl disable k3s`.
Answers: Richard Whitehead
Answers: CC BY-SA 4.0
Answers: 2020-09-14T06:02:54.997Z
Answers: 2020-09-14T06:02:54.997Z
Answers: Bruce Becker
Body: <p>I've just installed k3s on my notebook (Ubuntu). Works fine. Great project. However, i do not need it to run 24/7 so, how do I properly shutdown the k3s cluster. Oddly, I cannot find anything about this in the documentation. Thanks</p>
  
Tags: <kubernetes>
Title: Properly shutdown k3s on local machine
UserDisplayName: Moonlit
ThreadText: # Properly shutdown k3s on local machine
  
  (Asked by: username_0 on 2020-09-13)
  
  I've just installed k3s on my notebook (Ubuntu). Works fine. Great project. However, i do not need it to run 24/7 so, how do I properly shutdown the k3s cluster. Oddly, I cannot find anything about this in the documentation. Thanks
  
  **Tags:** kubernetes
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2020-09-14. Score: 19 | Accepted answer
  
  There is a systemd shutdown task (`/etc/systemd/system/k3s.service`) which stops the server, but not the k8s resources (pods, networks, etc). For this there is a utility script `k3s-killall.sh` which should be at `/usr/local/bin/k3s-killall.sh`
  
  > **Comments:**
  > **username_3**: On Ubuntu you can stop the k3s service with `sudo systemctl stop k3s`, and stop it restarting when you reboot with `sudo systemctl disable k3s`.  
  
  ---
  
  ### Answer by username_1 on 2024-09-04. Score: 0
  
  If your setup does not have systemd or openrc, but for example init (from busybox)...
  You must take a look on [k3s script](https://get.k3s.io/) and adapt this `k3s-killall.sh` for
  your init system:
  
  ```
  # --- create killall script ---
  create_killall() {
      [ "${INSTALL_K3S_BIN_DIR_READ_ONLY}" = true ] && return
      info "Creating killall script ${KILLALL_K3S_SH}"
      $SUDO tee ${KILLALL_K3S_SH} >/dev/null << \EOF
  #!/bin/sh
  [ $(id -u) -eq 0 ] || exec sudo $0 $@
  
  K3S_DATA_DIR=${K3S_DATA_DIR:-/var/lib/rancher/k3s}
  
  for bin in ${K3S_DATA_DIR}/data/**/bin/; do
      [ -d $bin ] && export PATH=$PATH:$bin:$bin/aux
  done
  
  set -x
  
  for service in /etc/systemd/system/k3s*.service; do
      [ -s $service ] && systemctl stop $(basename $service)
  done
  
  for service in /etc/init.d/k3s*; do
      [ -x $service ] && $service stop
  done
  
  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() {
      kill -9 $(
          { set +x; } 2>/dev/null;
          pstree $@;
          set -x;
      ) 2>/dev/null
  }
  
  remove_interfaces() {
      # Delete network interface(s) that match 'master cni0'
      ip link show 2>/dev/null | grep 'master cni0' | while read ignore iface ignore; do
          iface=${iface%%@*}
          [ -z "$iface" ] || ip link delete $iface
      done
  
      # Delete cni related interfaces
      ip link delete cni0
      ip link delete flannel.1
      ip link delete flannel-v6.1
      ip link delete kube-ipvs0
      ip link delete flannel-wg
      ip link delete flannel-wg-v6
  
      # Restart tailscale
      if [ -n "$(command -v tailscale)" ]; then
          tailscale set --advertise-routes=
      fi
  }
  
  getshims() {
      ps -e -o pid= -o args= | sed -e 's/^ *//; s/\s\s*/\t/;' | grep -w "${K3S_DATA_DIR}"'/data/[^/]*/bin/containerd-shim' | cut -f1
  }
  
  killtree $({ set +x; } 2>/dev/null; getshims; set -x)
  
  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 -f "$0" && rm -rf "$0"'
      set -x
  }
  
  do_unmount_and_remove '/run/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
  
  remove_interfaces
  
  rm -rf /var/lib/cni/
  iptables-save | grep -v KUBE- | grep -v CNI- | grep -iv flannel | iptables-restore
  ip6tables-save | grep -v KUBE- | grep -v CNI- | grep -iv flannel | ip6tables-restore
  EOF
      $SUDO chmod 755 ${KILLALL_K3S_SH}
      $SUDO chown root:root ${KILLALL_K3S_SH}
  }
  
  ```
  
  
  ---
  
  