升级思路是,驱逐负载、摘除流量之后,先升级控制节点,后升级工作节点。
1. 查看集群版本
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"}
|
当前版本是 1.22,由于 kubeadm 不允许跨版本升级,这里准备升级到 1.23 。
2. 添加 Kubernetes 安装源
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. 查找目标版本的 kubeadm
1
2
3
4
|
yum list --showduplicates kubeadm --disableexcludes=kubernetes
...
kubeadm.x86_64 1.23.4-0 kubernetes
|
4. 升级 Kubeadm
1
2
|
yum remove -y kubeadm
yum install -y kubeadm-1.23.4-0 --disableexcludes=kubernetes
|
5. 查看升级计划
这里忽略掉了一些错误情况,强行查看升级计划。
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. 拉取依赖的镜像
提前拉取依赖,可以提前发现无法拉取的镜像,还可以加快升级过程。如果拉取不到镜像,可以通过 kubeadm config images list
命令查看镜像列表,然后通过下面的方式拉取镜像:
1
2
|
docker pull k8simage/kube-apiserver:v1.23.4
docker tag k8simage/kube-apiserver:v1.23.4 k8s.gcr.io/kube-apiserver:v1.23.4
|
直接拉取依赖的镜像:
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. 开始升级 Kubernetes 集群
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. 升级 kubectl 和 kubelet
1
|
yum install -y kubelet-1.23.4-0 kubectl-1.23.4-0 --disableexcludes=kubernetes
|