Rancher部署並導入K8S集群


Rancher 的部署可以有三種架構:
  • 高可用 Kubernetes 安裝: 建議使用 Kubernetes 程序包管理器 Helm 在專用的 Kubernetes 集群上安裝 Rancher。在 RKE 集群中,需要使用三個節點以實現高可用性。在 K3s 集群(輕量級kubernetes)中,僅需要兩個節點即可。
  • 單節點 Kubernetes 安裝: 另一個選擇是在 Kubernetes 集群上使用 Helm 安裝 Rancher,僅在集群中使用單個節點。雖然在這種情況下的 Rancher Server 不具有高可用性,但是這種架構既節省了資源,又保留了可擴展性。如果您想在短期內通過使用單個節點來節省資源,同時又保留高可用性遷移路徑,最合適的架構就是單節點 Kubernetes 安裝。
  • 單節點 Docker 安裝: 將 Rancher 與 Docker 一起安裝在單個節點上。這種安裝方式的優點是開箱即用,建議在測試和演示環境中使用這種架構。它的缺點也非常明顯,單節點 Docker 安裝和 Kubernetes 安裝之間無法遷移。如果您已經決定了要在生產環境中使用 Rancher,我們推薦您在上述的兩種 Kubernetes 安裝中選擇一種,在生產環境中使用。
 
一、部署Rancher v2.27環境(單節點Docker安裝)
1)這里我將Rancher部署到k8s集群的其中一個node節點172.16.60.234上。
需要提前下載rancher鏡像,這里選擇 "rancher/rancher:stable"版本鏡像
下載rancher服務鏡像
[root@k8s-node01 ~]# docker pull rancher/rancher:stable

查看鏡像
[root@k8s-node01 ~]# docker images
REPOSITORY                                                TAG                 IMAGE ID            CREATED             SIZE
rancher/rancher                                           stable              fb0772b142fe        13 days ago         856MB
.........

  

2)以容器方式啟動Rancher服務
啟動rancher容器
[root@k8s-node01 ~]# docker run -d -v /tmp/rancher:/tmp/rancher --restart=unless-stopped -p 80:80 -p 443:443 rancher/rancher:stable

查看rancher容器
[root@k8s-node01 ~]# docker ps|grep rancher
2c2b36cb138b        rancher/rancher:stable                                        "entrypoint.sh"          3 hours ago         Up 3 hours          0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   lucid_robinson

  

稍微等一會兒,待rancher容器進程完全拉起來后,就可以在瀏覽器里訪問rancher了。訪問地址是:https://172.16.60.234,首次訪問會提示設置admin管理員密碼。比如這里設置管理員密碼為: admin@123456

 

二、導入k8s集群到Rancher環境
將已經部署好的k8s集群導入到Rancher環境中
1)依次點擊 "添加集群" -> "導入" -> "添加集群 - Import"

2) 給自己導入的集群起一個名稱。比如這里我導入的k8s集群名稱設置為"kuvin-test-cluster"

3)按照下面的指向,執行第一步驟和第三步驟(因為我這里二進制部署的k8s集群是有證書,所以跳過第二步驟,直接執行第三步驟)

 先查看node節點kubelet配置中的USER_ACCOUNT,默認是default-auth

  • 執行第一步驟操作(在k8s集群的master節點操作)
[root@k8s-master01 work]# kubectl create clusterrolebinding cluster-admin-binding --clusterrole cluster-admin --user default-auth
clusterrolebinding.rbac.authorization.k8s.io/cluster-admin-binding created
  • 接着執行第三步驟(在k8s集群的master節點操作)
首先在瀏覽器里訪問上面截圖中的第三步驟中的yaml地址,查看yaml文件里配置的image鏡像,提前在所以node節點將這些image鏡像下載下來。
也可以將瀏覽器里這些yaml配置內容粘貼到master節點的一個文件里,比如rancher_imoprt.yml,然后執行"kubectl create -f rancher_imoprt.yml"。這里我直接使用上面截圖中第三步的命令進行cattle-cluster-agent和cattle-node-agent的pod創建

 

 查出來image鏡像后,提前在所有的node節點節點上下載這個rancher/rancher-agent:v2.4.5鏡像

[root@k8s-node01 ~]# docker pull rancher/rancher-agent:v2.4.5

[root@k8s-node01 ~]# docker images
REPOSITORY                                                TAG                 IMAGE ID            CREATED             SIZE
rancher/rancher-agent                                     v2.4.5              2e6c7ac4e072        13 days ago         294MB
.......

  

接着執行上面截圖中的第三步驟操作
[root@k8s-master01 work]# curl --insecure -sfL https://172.16.60.234/v3/import/qx7972dgq5876dlvtx66hmb55l49w57tvpr8v58ldwvlk75bb44msn.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-ad2b8cf created
clusterrole.rbac.authorization.k8s.io/cattle-admin created
deployment.apps/cattle-cluster-agent created
daemonset.apps/cattle-node-agent created

  

待上面第三步驟成功操作后,到k8s集群的master節點上查看信息
檢查會發現k8s中多了一個cattle-system的命名空間:
檢查會發現k8s中多了一個cattle-system的命名空間
[root@k8s-master01 work]# kubectl get ns
NAME              STATUS   AGE
cattle-system     Active   18m
default           Active   3d19h
kube-node-lease   Active   3d19h
kube-public       Active   3d19h
kube-system       Active   3d19h

cattle-system的命名空間下創建了cattle-cluster-agent和cattle-node-agent的pod
[root@k8s-master01 work]# kubectl get pods -n cattle-system
NAME                                   READY   STATUS    RESTARTS   AGE
cattle-cluster-agent-87944bb89-9l8vn   1/1     Running   0          18m
cattle-node-agent-24vnp                1/1     Running   0          18m
cattle-node-agent-86hdx                1/1     Running   0          18m
cattle-node-agent-qz59v                1/1     Running   0          18m

[root@k8s-master01 work]# kubectl get deployment -n cattle-system
NAME                   READY   UP-TO-DATE   AVAILABLE   AGE
cattle-cluster-agent   1/1     1            1           19m

[root@k8s-master01 work]# kubectl get daemonset -n cattle-system
NAME                DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
cattle-node-agent   3         3         3       3            3           <none>          19m
  • 觀察集群導入后的狀態
執行完上面截圖中的第三步驟后,觀察下導入的集群的狀態,狀態依次從"Waiting" 變為 "Active"

 

 

三、通過Rancher界面管理k8s集群

 

 

 

 點擊上圖右上角的"執行kubelet命令行",就可以在webshell里面通過kubelet命令查看集群信息了

還可以通過查看導入的k8s集群的項目里的資源,來查看每個service下的pod情況

 

 

 動態伸縮pod副本

登錄pod容器內部

 pod重新部署

Rancher還有很多管理k8s集群的其他操作功能,在這里就不一一介紹了。

 

四、刪除已導入的k8s集群,重新導入
1)從Rancher界面直接刪除已導入的集群

 

2)刪除已經導入的 "API&Keys" 信息

3)刪除上面截圖中第一步驟中添加的權限(在k8s集群的master節點操作)

[root@k8s-master01 work]# kubectl delete clusterrolebinding cluster-admin-binding
4)刪除上面截圖中第三步驟中添加的cattle-cluster-agent和cattle-node-agent的pod
[root@k8s-master01 work]# kubectl delete deployment cattle-cluster-agent -n cattle-system
[root@k8s-master01 work]# kubectl delete daemonset cattle-node-agent -n cattle-system

或者將上面截圖中第三步驟的yaml文件地址里的內容粘貼到k8s的master節點的一個yaml文件,比如rancher_imoprt.yml,直接針對這個yml文件執行"kubectl delete -f rancher_imoprt.yml"操作即可刪除

[root@k8s-master01 work]# kubectl delete -f rancher_imoprt.yml


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM