Skip to content

Instantly share code, notes, and snippets.

@Codehunter-py
Last active October 14, 2024 21:36
Show Gist options
  • Select an option

  • Save Codehunter-py/a6f5633df0a1002578adc460d5bb32fe to your computer and use it in GitHub Desktop.

Select an option

Save Codehunter-py/a6f5633df0a1002578adc460d5bb32fe to your computer and use it in GitHub Desktop.
Script to stop Kubernetes and container services on the control plane node and worker nodes
#!/bin/bash
# Script to stop Kubernetes and container services on any node (control or worker)
echo "This script is running on node: $(hostname)"
# Drain the node if it's a worker (assuming hostnames like 'worker1', 'worker2', etc.)
kubectl drain $(hostname) --ignore-daemonsets
# Stopping all running containers
echo "Stopping containers on $(hostname)..."
sudo crictl stopp $(sudo crictl ps -q)
# Stopping Kubernetes services
echo "Stopping kubelet service on $(hostname)..."
sudo systemctl stop kubelet
echo "Stopping containerd service on $(hostname)..."
sudo systemctl stop containerd
# Delaying poweroff by 30 seconds
echo "$(hostname) will power off in 30 seconds..."
sleep 30
# Power off the machine
sudo poweroff
# chmod +x shutdown_k8s_node.sh && ./shutdown_k8s_node.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment