Skip to content

Instantly share code, notes, and snippets.

@ctrahey
Last active April 2, 2021 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctrahey/f9a6a2858db75d49e89eb372bc6f862d to your computer and use it in GitHub Desktop.
Save ctrahey/f9a6a2858db75d49e89eb372bc6f862d to your computer and use it in GitHub Desktop.
Upgrading a Kubernetes Cluster

Upgrade Kubernetes Cluster

A simple script that enumerates nodes, cordons/drains them, upgrades kubelet and kubectl, then uncordons. Roughly follows the official docs and assumes that the control plane is already updated.

Steps I ran on the Control Plane first:

KUBE_VERSION=1.20.5
apt-mark unhold kubeadm && \
apt-get update && apt-get install -y kubeadm=${KUBE_VERSION}-00 && \
apt-mark hold kubeadm

kubeadm upgrade -v5 apply v$KUBE_VERSION # <-- First CP node
kubeadm upgrade node # <-- other CP nodes

Note that the above steps only upgrade control-plane components, and you still need to upgrade kubelet (which is why the control plane nodes are not filtered out in upgrade-cluster.sh).

KUBE_VERSION=1.20.5
for NODE in $(k get nodes -o name);
do
IP=$(kubectl get $NODE -o json | jq '.status.addresses[]|select(.type=="InternalIP").address' -r)
kubectl drain $NODE --ignore-daemonsets --delete-local-data
echo "Node $NODE drained, waiting 20 secs to settle" # This is probably overkill
sleep 20
echo "Updating packages... "
ssh -o "StrictHostKeyChecking=accept-new" -o ConnectTimeout=3 $IP sudo bash <<EOF
apt-mark unhold kubelet kubectl && \
apt-get update && apt-get install -y kubelet=$KUBE_VERSION-00 kubectl=$KUBE_VERSION-00 && \
apt-mark hold kubelet kubectl
sudo systemctl daemon-reload
sudo systemctl restart kubelet
EOF
echo "done on system, uncordoning node $NODE"
kubectl uncordon $NODE
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment