This page looks best with JavaScript enabled

How to Upgrade a Kubernetes Cluster

 ·  ☕ 2 min read

The idea of the upgrade is: drain the workloads and take the traffic off, then upgrade the control plane nodes first and the worker nodes afterwards.

1. Check the cluster version

1
2
3
4
kubectl version

Client Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.4", GitCommit:"e87da0bd6e03ec3fea7933c4b5263d151aafd07c", GitTreeState:"clean", BuildDate:"2021-02-18T16:12:00Z", GoVersion:"go1.15.8", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.0", GitCommit:"c2b5237ccd9c0f1d600d3072634ca66cefdf272f", GitTreeState:"clean", BuildDate:"2021-08-04T17:57:25Z", GoVersion:"go1.16.6", Compiler:"gc", Platform:"linux/amd64"}

The current version is 1.22. Since kubeadm does not allow upgrading across versions, here we prepare to upgrade to 1.23.

2. Add the Kubernetes installation source

CentOS:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
epo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

Ubuntu:

1
2
3
4
5
6
apt-get update && apt-get install -y apt-transport-https
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
apt-get update

3. Find the kubeadm package for the target version

1
2
3
4
yum list --showduplicates kubeadm --disableexcludes=kubernetes

...
kubeadm.x86_64              1.23.4-0             kubernetes

4. Upgrade kubeadm

1
2
yum remove -y kubeadm
yum install -y kubeadm-1.23.4-0 --disableexcludes=kubernetes

5. Check the upgrade plan

Some error conditions are ignored here to force the upgrade plan to be displayed.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
kubeadm upgrade plan --ignore-preflight-errors=ControlPlaneNodesReady,CoreDNSUnsupportedPlugins,CoreDNSMigration

...
Upgrade to the latest stable version:

COMPONENT                 CURRENT   TARGET
kube-apiserver            v1.22.0   v1.23.4
kube-controller-manager   v1.22.0   v1.23.4
kube-scheduler            v1.22.0   v1.23.4
kube-proxy                v1.22.0   v1.23.4
CoreDNS                   v1.8.4    v1.8.6
etcd                      3.5.0-0   3.5.1-0

6. Pull the dependent images

Pulling the dependencies in advance lets you spot images that cannot be pulled early, and also speeds up the upgrade process. If an image cannot be pulled, you can list the images with the kubeadm config images list command and then pull them as follows:

1
2
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.23.4
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.23.4 k8s.gcr.io/kube-apiserver:v1.23.4

Pulling the dependent images directly:

1
2
3
4
5
6
7
8
9
kubeadm config images pull

[config/images] Pulled k8s.gcr.io/kube-apiserver:v1.23.4
[config/images] Pulled k8s.gcr.io/kube-controller-manager:v1.23.4
[config/images] Pulled k8s.gcr.io/kube-scheduler:v1.23.4
[config/images] Pulled k8s.gcr.io/kube-proxy:v1.23.4
[config/images] Pulled k8s.gcr.io/pause:3.6
[config/images] Pulled k8s.gcr.io/etcd:3.5.1-0
[config/images] Pulled k8s.gcr.io/coredns/coredns:v1.8.6

7. Start upgrading the Kubernetes cluster

1
2
3
4
kubeadm upgrade apply v1.23.4 --ignore-preflight-errors=ControlPlaneNodesReady,CoreDNSUnsupportedPlugins,CoreDNSMigration

[upgrade/successful] SUCCESS! Your cluster was upgraded to "v1.23.4". Enjoy!
[upgrade/kubelet] Now that your control plane is upgraded, please proceed with upgrading your kubelets if you haven't already done so.

8. Upgrade kubectl and kubelet

1
yum install -y kubelet-1.23.4-0 kubectl-1.23.4-0 --disableexcludes=kubernetes

WeChat Official Account
WRITTEN BY
WeChat Official Account