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集群,無論該集群是自管理還是托管在雲上。