Certified Kubernetes Administrator (CKA) - Phần 9: Task

Upgrade the current version of kubernetes from 1.30.0 to 1.31.0 exactly using the kubeadm utility.

Upgrading kubeadm clusters

Changing the package repository (Master & Worker node):

echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] \
https://pkgs.k8s.io/core:/stable:/v1.31/deb/ /" | \
sudo tee /etc/apt/sources.list.d/kubernetes.list
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.31/deb/Release.key | \
sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
sudo apt-get update

Determine which version to upgrade to:

$ sudo apt update
$ sudo apt-cache madison kubeadm
   kubeadm | 1.31.3-1.1 | https://pkgs.k8s.io/core:/stable:/v1.31/deb  Packages
   kubeadm | 1.31.2-1.1 | https://pkgs.k8s.io/core:/stable:/v1.31/deb  Packages
   kubeadm | 1.31.1-1.1 | https://pkgs.k8s.io/core:/stable:/v1.31/deb  Packages
   kubeadm | 1.31.0-1.1 | https://pkgs.k8s.io/core:/stable:/v1.31/deb  Packages

Upgrading control plane nodes:

sudo apt-mark unhold kubeadm && \
sudo apt-get update && sudo apt-get install -y kubeadm='1.31.0-1.1' && \
sudo apt-mark hold kubeadm
$ kubeadm version
kubeadm version: &version.Info{
Major:"1", 
Minor:"31", 
GitVersion:"v1.31.0", 
GitCommit:"9edcffcde5595e8a5b1a35f88c421764e575afce", 
GitTreeState:"clean", 
BuildDate:"2024-08-13T07:35:57Z", 
GoVersion:"go1.22.5", 
Compiler:"gc", 
Platform:"linux/amd64"
}
$ sudo kubeadm upgrade plan
Upgrade to the latest stable version:

COMPONENT                 NODE           CURRENT    TARGET
kube-apiserver            controlplane   v1.30.0    v1.31.3
kube-controller-manager   controlplane   v1.30.0    v1.31.3
kube-scheduler            controlplane   v1.30.0    v1.31.3
kube-proxy                               1.30.0     v1.31.3
CoreDNS                                  v1.10.1    v1.11.1
etcd                      controlplane   3.5.12-0   3.5.15-0

You can now apply the upgrade by executing the following command:

        kubeadm upgrade apply v1.31.3
 $ kubeadm upgrade apply v1.31.0
[upgrade/successful] SUCCESS! Your cluster was upgraded to "v1.31.0". Enjoy!

[upgrade/kubelet] Now that your control plane is upgraded, 
please proceed with upgrading your kubelets if you haven't already done so.

Drain the node:

$ k drain controlplane --ignore-daemonsets
node/controlplane cordoned
Warning: ignoring DaemonSet-managed Pods: kube-system/kube-proxy-6crxv,kube-system/weave-net-r7wvc
node/controlplane drained

Upgrade kubelet and kubectl:

sudo apt-mark unhold kubelet kubectl && \
sudo apt-get update && \
sudo apt-get install -y kubelet='1.31.0-1.1' kubectl='1.31.0-1.1' && \
sudo apt-mark hold kubelet kubectl

Restart the kubelet:

sudo systemctl daemon-reload
sudo systemctl restart kubelet
$ k get node -o wide
NAME           STATUS                     ROLES           AGE   VERSION
controlplane   Ready,SchedulingDisabled   control-plane   72m   v1.31.0  
node01         Ready                      <none>          71m   v1.30.0

Uncordon the node:

kubectl uncordon controlplane

Upgrading worker nodes:

sudo apt-mark unhold kubeadm && \
sudo apt-get update && sudo apt-get install -y kubeadm='1.31.0-1.1' && \
sudo apt-mark hold kubeadm

Call "kubeadm upgrade":

sudo kubeadm upgrade node

Drain the node:

kubectl drain node01 --ignore-daemonsets

Upgrade kubelet and kubectl:

sudo apt-mark unhold kubelet kubectl && \
sudo apt-get update && \
sudo apt-get install -y kubelet='1.31.0-1.1' kubectl='1.31.0-1.1' && \
sudo apt-mark hold kubelet kubectl

Restart the kubelet:

sudo systemctl daemon-reload
sudo systemctl restart kubelet

Uncordon the node:

kubectl uncordon node01

Json path result kubectl

Range and end:

-o=jsonpath=
'{range.items[*]}
    {.metadata.name} {"\t"} {.status.capacity.cpu} {"\n"}
{end}'

Custom columns:

-o=custom-columns=
DEPLOYMENT:.metadata.name,
CONTAINER_IMAGE:.spec.template.spec.containers[0].image,
READY_REPLICAS:.status.readyReplicas,
NAMESPACE:.metadata.namespace

DEPLOYMENT   CONTAINER_IMAGE   READY_REPLICAS   NAMESPACE
deploy1      nginx             1                admin2406
deploy2      nginx:alpine      1                admin2406
deploy3      nginx:1.16        1                admin2406
deploy4      nginx:1.17        1                admin2406
deploy5      nginx:latest      1                admin2406

Take the backup of ETCD at the location /opt/etcd-backup.db on the controlplane node.

export ETCDCTL_API=3
etcdctl snapshot save --cacert=/etc/kubernetes/pki/etcd/ca.crt \
--cert=/etc/kubernetes/pki/etcd/server.crt \
--key=/etc/kubernetes/pki/etcd/server.key \
--endpoints=127.0.0.1:2379 /opt/etcd-backup.db