僅需4步,輕鬆升級K3s集羣!

Rancher 2.4是Rancher目前最新的版本,在這一版本中你可以通過Rancher UI對K3s集羣進行升級管理。

K3s是一個輕量級Kubernetes發行版,藉助它你可以幾分鐘之內設置你的開發Kubernetes環境。它非常適用於生產用例,主要用於IoT和Edge設備。
在這裏插入圖片描述

在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
ef7f9a0d5bca831af19900a6ceafd30daca51e4f951295762cf7516e48d3d742

Step3:在Rancher中導入集羣

打開Rancher UI並點擊以下按鈕導入K3s集羣:

在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述

複製最後一條命令並在安裝了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

在這裏插入圖片描述

Step4:執行升級

點擊三個小點點,並選擇【Edit】。選擇最新的Kubernetes版本並點擊保存。

在這裏插入圖片描述

在這裏插入圖片描述

點擊保存之後,你可以看到集羣狀態從Active變成了Upgrading。

在這裏插入圖片描述
在這裏插入圖片描述

一會兒之後,狀態會重新變爲Active,你將看到K3s集羣升級到了最新版本。

在這裏插入圖片描述

僅需幾次點擊,我們已經通過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-upgrade/blob/v1.18.4+k3s1/scripts/upgrade.sh#L15的邏輯,升級時會grep兩個進程,分別是K3s master和Rancher server中的K3s,所以會報錯。

如要解決上述問題,需要先將Rancher server 停掉,等待K3s升級成功後,再啓動Rancher server。

結 論

Rancher推出的K3s輕巧易用,加上Rancher 2.4對其的支持,升級集羣更是易如反掌。當你通過Rancher UI導入K3s集羣,Rancher會自動檢測並啓用帶有可用版本列表的升級集羣選項。通過這一功能,你可以升級任意K3s集羣,無論該集羣是自管理還是託管在雲上。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章