1. 程式人生 > >僅需4步,輕鬆升級K3s叢集!

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

Rancher 2.4是Rancher目前最新的版本,在這一版本中你可以通過Rancher UI對K3s叢集進行升級管理。 K3s是一個輕量級Kubernetes發行版,藉助它你可以幾分鐘之內設定你的開發Kubernetes環境。它非常適用於生產用例,主要用於IoT和Edge裝置。 ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200708101329947.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyMjA2ODEz,size_16,color_FFFFFF,t_70#pic_center) 在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叢集: ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/2020070810170781.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyMjA2ODEz,size_16,color_FFFFFF,t_70#pic_center) ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200708101715415.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyMjA2ODEz,size_16,color_FFFFFF,t_70#pic_center) ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200708101729785.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyMjA2ODEz,size_16,color_FFFFFF,t_70#pic_center) 複製最後一條命令並在安裝了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 ``` ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200708101940174.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyMjA2ODEz,size_16,color_FFFFFF,t_70#pic_center) ### Step4:執行升級 點選三個小點點,並選擇【Edit】。選擇最新的Kubernetes版本並點選儲存。 ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200708101957184.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyMjA2ODEz,size_16,color_FFFFFF,t_70#pic_center) ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200708102009919.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyMjA2ODEz,size_16,color_FFFFFF,t_70#pic_center) 點選儲存之後,你可以看到叢集狀態從Active變成了Upgrading。 ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/2020070810203068.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyMjA2ODEz,size_16,color_FFFFFF,t_70#pic_center) ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200708102045966.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyMjA2ODEz,size_16,color_FFFFFF,t_70#pic_center) 一會兒之後,狀態會重新變為Active,你將看到K3s叢集升級到了最新版本。 ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200708102112718.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyMjA2ODEz,size_16,color_FFFFFF,t_70#pic_center) 僅需幾次點選,我們已經通過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叢集,無論該叢集是自管理還是託管在