如何简单、优雅地升级kubernetes [中国大陆]

发布日期: 2022-07-24     阅读: 6143    点赞: 0

时代在进步、科技在发展、升级kubernetes也不能再用土办法了…

因为网络原因,安装升级kubernetes历来都有些困难,大多都是修改各组件的镜像源。

随着k8s的普及和版本的不断升级,官方已经支持通过kubeadm的配置来修改安装/升级时拉去镜像的注册器地址。

如下是当下最主流,最简单的升级方法了。

修改kube-system空间下名为kubeadm-config的configMap。

修改k8s.gcr.io为阿里源

  imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers

然后按照官方教程升级即可

https://kubernetes.io/docs/tasks/administer-cluster/kubeadm/kubeadm-upgrade/

如下以ubuntu从k8s 1.21.x 升级到1.22.x为例

确保ubuntu镜像源为国内镜像源

如下修改镜像源为阿里的参考链接

https://developer.aliyun.com/mirror/kubernetes?spm=a2c6h.13651102.0.0.70be1b114oiqZm

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
apt-get install -y kubelet kubeadm kubectl

查看可升级的版本

通过执行如下命令可查询出可升级到的准确目标版本号例如 1.22.12.00

apt update
apt-cache madison kubeadm

以升级1.22查询版本为例

apt-cache madison kubeadm | grep 1.22.

PS:k8s升级需要1.21到1.22 然后1.22 到 1.23 不能跨大版本号升级所以 不能直接 apt upgrade升级

升级控制节点

# replace x in 1.22.x-00 with the latest patch version
apt-mark unhold kubeadm && \
apt-get update && apt-get install -y kubeadm=1.22.x-00 && \
apt-mark hold kubeadm

替换上面的版本号为要升级目标版本号

  • 验证kubeadm版本
kubeadm version
  • 验证升级计划
kubeadm upgrade plan
  • 升级到指定版本
# replace x with the patch version you picked for this upgrade
sudo kubeadm upgrade apply v1.22.x

如升级成功则打印如下信息

[upgrade/successful] SUCCESS! Your cluster was upgraded to "v1.22.x". Enjoy!

[upgrade/kubelet] Now that your control plane is upgraded, please proceed with upgrading your kubelets if you haven't already done so.
  • 升级其他控制节点
sudo kubeadm upgrade node
  • 将节点设为不可调度
# replace <node-to-drain> with the name of your node you are draining
kubectl drain <node-to-drain> --ignore-daemonsets
  • 升级kubelet and kubect
# replace x in 1.22.x-00 with the latest patch version
apt-mark unhold kubelet kubectl && \
apt-get update && apt-get install -y kubelet=1.22.x-00 kubectl=1.22.x-00 && \
apt-mark hold kubelet kubectl
  • 将节点恢复调度
  # replace <node-to-drain> with the name of your node
  kubectl uncordon <node-to-drain>

升级worker节点

升级kubeadm

# replace x in 1.22.x-00 with the latest patch version
apt-mark unhold kubeadm && \
apt-get update && apt-get install -y kubeadm=1.22.x-00 && \
apt-mark hold kubeadm

升级节点

kubeadm upgrade node

将节点设为停止调度

# replace <node-to-drain> with the name of your node you are draining
kubectl drain <node-to-drain> --ignore-daemonsets

升级kubelete和kubectl

apt-mark unhold kubelet kubectl && \
apt-get update && apt-get install -y kubelet=1.22.12-00 kubectl=1.22.12-00 --allow-downgrades && \
apt-mark hold kubelet kubectl

重启kubelet

sudo systemctl daemon-reload
sudo systemctl restart kubelet

恢复节点为可调度

# replace <node-to-drain> with the name of your node
kubectl uncordon <node-to-drain>

转载请注明出处,本文采用 CC4.0 协议授权