Try   HackMD

k3s 輕量化叢集組建

硬體環境需求

ARM

  • Board:Raspberry Pi 1/2/3
  • System:ubuntu_Mate、Raspbian
  • Storage:8GB (micro_sdcard)

PC Base

  • Hardware Base:OpenStack、VMware、VitrualBox、PC(x86_64)
  • System:ubuntu_16.04 / ubuntu_18.04
  • CPU:1 core
  • RAM:1GB
  • Storage:10GB

環境預備動作

添加主機識別

  • 增加主機 IP + hostname
${host ip}  ${hostname} 
  • 示意圖
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Network Gateway 修改

  • 改成你的子網 Gateway IP
nano /etc/reslov.conf
  • 示意圖
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

系統更新 與 curl / wget 網路下載套件安裝

apt-get install update
apt-get install -y curl wget 

k3s 叢集搭建-官方快速版

參考網站:https://k3s.io/

Master 端搭建 (官方版 Master 具有 Cluster 角色)

curl -sfL https://get.k3s.io | sh -
  • 示意圖
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Master 環境驗證

  • 官方版可用 kubectl & k3s kubectl 兩個功能指令。
kubectl get nodes -o wide
k3s kubectl get nodes -o wide
  • 示意圖
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

k3s 中 kubeconfig 位置

  • 需要折騰的可以來這邊搞一下www
/etc/rancher/k3s/k3s.yaml

取得添加 cluster 加入叢集所需金鑰

cat /var/lib/rancher/k3s/server/node-token
  • 示意圖
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Cluster 端 Join Master 指令

  • Token 錯誤一樣會派 Job 在 cluster 動作,但 Master 端不會看到 Cluster 有註冊的動作。
curl -sfL https://get.k3s.io | K3S_URL=https://k3s-Server:6443 K3S_TOKEN=XXX sh -
  • 示意圖
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Cluster 添加驗證

k3s kubectl get node -o wide
  • 示意圖
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

k3s 叢集搭建-Github Release 包

k3s Github 參考網址:https://github.com/rancher/k3s/releases

  • PC 平台使用 k3s (紅筐處)
  • ARM 系列平台使用 k3s arm (橘筐處)
  • Rasberry Pi 使用 k3s armhf
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

PC 平台

wget https://github.com/rancher/k3s/releases/download/v0.9.1/k3s
  • 下載完會出現 k3s 應用程式
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Raspberry Pi 平台

wget https://github.com/rancher/k3s/releases/download/v0.9.1/k3s-armhf
  • 下載完會出現 k3s-armhf 應用程式
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

k3s 轉移成 OS 全域可用應用程式

PC 平台

mv ./k3s /usr/bin/k3s

Raspberry Pi 平台

  • 為確保之後在 OS 中呼叫的名稱,因此都將應用程式更名為 k3s
mv ./k3s-armhf /usr/bin/k3s

權限調整

chmod 755 /usr/bin/k3s

環境測試

k3s version
  • 測試結果
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Master 端啟動

  • 前景執行 (驗證執行狀態)
  • 若不想讓 Master Node 具有 Cluster Node 角色,添加 disable-agent 即可
k3s server (--disable-agent)
  • 背景執行
nohup k3s server (--disable-agent) > server.log 2&>1 &

k3s 中 kubeconfig 位置

  • 需要折騰的可以來這邊搞一下www
/etc/rancher/k3s/k3s.yaml

執行狀態檢查

k3s kubectl get node -o wide
  • 示意圖
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

取得添加 cluster 加入叢集所需金鑰

cat /var/lib/rancher/k3s/server/node-token
  • 示意圖
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Cluster 端 Join Master 指令

  • 前景執行 (驗證執行狀態)
k3s agent --server https://myserver:6443 --token ${NODE_TOKEN}
  • 背景執行
nohup k3s --server https://myserver:6443 --token ${NODE_TOKEN} > cluster.log 2&>1 &
  • 示意圖

Cluster 添加驗證

k3s kubectl get node -o wide
  • 示意圖
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Raspberry Pi 的小天地

  • 應該有人發現上面的指令在 Raspberry Pi 上會錯誤吧(???)

執行錯誤參考

  • cgroup 相關服務錯誤
    https://ithelp.ithome.com.tw/upload/images/20191002/20121071YGVl1EIdUD.png

熱血開修

  • 打開需修改的檔案
nano /boot/cmdline.txt
  • 添加下面的這行指令
cgroup_memory=1 cgroup_enable=memory
  • 確認修改後的文件
    https://ithelp.ithome.com.tw/upload/images/20191002/20121071fgSwwog7yV.png
  • 系統重新啟動 Raspberry Pi
init 6
  • 重新開機之後,再重新將 k3s 的服務啟動即可。

k3s 叢集環境停止與清除

官方快速版

Server 服務移除 (k3s 應用程式會不見)

curl -sfL https://get.k3s.io | k3s-uninstall.sh
  • 示意圖

Server 服務停止 (可重新執行 server)

curl -sfL https://get.k3s.io | k3s-killall.sh
  • 示意圖

Cluster 服務移除 (k3s 應用程式會不見)

curl -sfL https://get.k3s.io | k3s-agent-uninstall.sh
  • 示意圖

Cluster 服務停止 (可重新執行 Join Master)

curl -sfL https://get.k3s.io | k3s-killall.sh
  • 示意圖

Github Release 包版本

停止 k3s 服務 (Master / Cluster 相同)

  • 取得k3s 在系統上的 Process ID
pidof k3s
  • 刪除 k3s 執行進程
kill -9 ${k3s-pid}
  • 環境重建
rm -rf /var/lib/rancher

移除 k3s 服務 (Master / Cluster 相同)

  • 刪除 k3s 的系統執行 binary 包
rm /usr/lib/k3s

k3s 叢集異常處理

無法添加節點 (401 Unauthorized)

  • 停掉 Server ,刪除k3s 服務相依目錄
  • 重新啟動 Server & 獲取 Token
rm -rf /var/lib/rancher

無法添加節點 (may not match server passwd entry)

  • 刪除 Server 上的 Cluster 驗證密碼
nano /var/lib/rancher/k3s/server/cred/node-passwd
  • 示意圖