20

仅需4步,轻松升级K3s集群!

 3 years ago
source link: http://dockone.io/article/10533
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Rancher 2.4是Rancher目前最新的版本,在这一版本中你可以通过Rancher UI对K3s集群进行升级管理。

K3s是一个轻量级Kubernetes发行版,借助它你可以几分钟之内设置你的开发Kubernetes环境。它非常适用于生产用例,主要用于IoT和Edge设备。

EzUJJjU.png!web

在Rancher 2.4中,你可以导入K3s集群并且可以通过Rancher本身对其进行升级管理。在本篇教程中,我们将了解如何在一个正在运行的K3s集群上执行实时升级。

Step1:创建K3s集群

在执行升级之前,我们需要启动一个K3s集群。在本文中我是在一个CentOS VM上启动的,你可以在任意云上执行这一操作。

登录到VM,并运行以下命令:

**[root@demo3 ~]# curl -sfL [https://get.k3s.io](https://get.k3s.io) | INSTALL_K3S_VERSION="v1.0.1" sh -s -**



[INFO]  Using v1.0.1 as release

[INFO]  Downloading hash [https://github.com/rancher/k3s/releases/download/v1.0.1/sha256sum-amd64.txt](https://github.com/rancher/k3s/releases/download/v1.0.1/sha256sum-amd64.txt)

[INFO]  Downloading binary [https://github.com/rancher/k3s/releases/download/v1.0.1/k3s](https://github.com/rancher/k3s/releases/download/v1.0.1/k3s)

[INFO]  Verifying binary download

[INFO]  Installing k3s to /usr/local/bin/k3s

[INFO]  Creating /usr/local/bin/kubectl symlink to k3s

[INFO]  Creating /usr/local/bin/crictl symlink to k3s

[INFO]  Creating /usr/local/bin/ctr symlink to k3s

[INFO]  Creating killall script /usr/local/bin/k3s-killall.sh

[INFO]  Creating uninstall script /usr/local/bin/k3s-uninstall.sh

[INFO]  env: Creating environment file /etc/systemd/system/k3s.service.env

[INFO]  systemd: Creating service file /etc/systemd/system/k3s.service

[INFO]  systemd: Enabling k3s unit

Created symlink from /etc/systemd/system/multi-user.target.wants/k3s.service to /etc/systemd/system/k3s.service.

[INFO]  systemd: Starting k3sAbove will install k3s version



**[root@demo3 ~]# kubectl get  nodes**



NAME    STATUS   ROLES    AGE     VERSION

demo3   Ready    master   5m55s   **v1.16.3-k3s.2**

你可以看到我们在VM上已经安装了Kubernetes v1.16.3。

Step2:安装Rancher

为了从Rancher UI中执行升级,必须通过以下命令安装Rancher:

**[root@demo3 ~]# sudo docker run -d --restart=unless-stopped -p 80:80 -p 443:443 rancher/rancher

**Unable to find image 'rancher/rancher:latest' locally

Trying to pull repository docker.io/rancher/rancher ...

latest: Pulling from docker.io/rancher/rancher

5bed26d33875: Pull complete

f11b29a9c730: Pull complete

930bda195c84: Pull complete

78bf9a5ad49e: Pull complete

fdb587a45dfe: Pull complete

0c3f7647a659: Pull complete

02c5407ca821: Pull complete

ebc009758b56: Pull complete

57a6e40f0f6e: Pull complete

30a88d01aca6: Pull complete

76505b705d6f: Pull complete

e585ea16af8d: Pull complete

6b069694034e: Pull complete

8c5d309f94d1: Pull complete

8ec56f5fa6c2: Pull complete

75872eb736b7: Pull complete

e774fd043162: Pull complete

c76d81e7a658: Pull complete

49bf6b83af1c: Pull complete

Digest: sha256:248ddca1169e8a4e06babd50e8105cbba0a326f86ec4de3e38d61e8909ffdb4e

Status: Downloaded newer image for docker.io/rancher/rancher:latest

Step3:在Rancher中导入集群

打开Rancher UI并点击以下按钮导入K3s集群:

6BBbMjZ.png!webvAvY73i.png!webrI32Yvb.png!web

复制最后一条命令并在安装了K3s的VM上运行该命令:

**[root@demo3 ~]# curl --insecure -sfL [https://185.136.232.236/v3/import/pxv5g7xswnnqkhl4dx7nrn8swndwshk9g8426lnldrvt7vjvfc2fgr.yaml](https://185.136.232.236/v3/import/pxv5g7xswnnqkhl4dx7nrn8swndwshk9g8426lnldrvt7vjvfc2fgr.yaml) | kubectl apply -f -**

clusterrole.rbac.authorization.k8s.io/proxy-clusterrole-kubeapiserver created

clusterrolebinding.rbac.authorization.k8s.io/proxy-role-binding-kubernetes-master created

namespace/cattle-system created

serviceaccount/cattle created

clusterrolebinding.rbac.authorization.k8s.io/cattle-admin-binding created

secret/cattle-credentials-d6e5b6d created

clusterrole.rbac.authorization.k8s.io/cattle-admin created

deployment.apps/cattle-cluster-agent created

daemonset.apps/cattle-node-agent created
bUFRRjM.png!web

Step4:执行升级

点击三个小点点,并选择【Edit】。选择最新的Kubernetes版本并点击保存。

uiqm2ue.png!webfQVrIby.png!web

点击保存之后,你可以看到集群状态从Active变成了Upgrading。

z2IfYbM.png!webrIjqYz3.png!web

一会儿之后,状态会重新变为Active,你将看到K3s集群升级到了最新版本。

uENBBjf.png!web

仅需几次点击,我们已经通过Rancher UI直接将导入的K3s集群升级到了v1.17.4版本。

Troubleshooting

社区用户经过实践之后发现K3s集群处于Upgrading的状态会持续很长一段时间。经过Rancher中国的社区技术经理本地操作复现后,给出如下解决方案:

K3s 分为master和agent角色。K3s集群启动成功后,可以导入到Rancher server去纳管。如果Rancher server和K3s master在同一个节点上,在Rancher UI上升级K3s 版本就会出现失败的情况。

此外,由于 https://github.com/rancher/k3s ... 23L15 的逻辑,升级时会 grep 两个进程,分别是K3s master和Rancher server中的K3s,所以会报错。

如要解决上述问题,需要先将Rancher server 停掉,等待K3s升级成功后,再启动Rancher server。

结 论

Rancher推出的K3s轻巧易用,加上Rancher 2.4对其的支持,升级集群更是易如反掌。当你通过Rancher UI导入K3s集群,Rancher会自动检测并启用带有可用版本列表的升级集群选项。通过这一功能,你可以升级任意K3s集群,无论该集群是自管理还是托管在云上。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK