手動建立K8S集群,使用Kubeadm Docker。環境使用virtualBox
< https://ithelp.ithome.com.tw/articles/10291343
< https://ithelp.ithome.com.tw/articles/10235069
1.建立虛擬機器(1~5步驟)
- Folder欄位建議要更改位置,個人更改成D:\virtual-box-workspace
- ISO使用ubuntu-20.04.5-live-server-amd64.iso,Ubuntu官方下載
- 完成後按下一步
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 →
1.1 設定
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 →
1.2設定虛擬機器cpu & mem
- master node個人使用3cpu & 3096 mem
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 →
1.3 Ubuntu安裝設定幾乎使用預設,網路方面後續再調整
我們只安裝open-ssh,其他套件都手動安裝
profile setup如下:
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 →
將openssh加入預設安裝
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 →
只要顯示Install Complete就可以按下stop update & reboot按鈕
1.4 更新成靜態網路
sudo vim /etc/netplan/00-installer-config.yaml
原本使用dhcp4動態網路,我們需要更改成靜態網路
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 →
1.5 請將此台虛擬機器更新網路 (使用橋接介面卡)
設定完之後,就可以使用ssh遠端連線
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 →
2.K8S建置 (1~x步驟)
2.1 k8s不使用swap,所以我們需要將他關閉
個人使用永久關閉,此設定要reboot才啟動
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 →
2.2 更新repository
2.3 設定DNS,更新host
sudo vim /etc/hosts
2.4 安裝 docker (version 20.10.12)
2.4.1 Dcoker預設cgroup driver為cgroupfs,K8S使用systemd,修改Docker為systemd
2.4.2 修改Docker為systemd成功
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 →
2.4.3 要額外安裝 cri-dockerd,K8S不支援直接使用docker,要透過cri-dockerd方式進行橋接
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 →
2.4.4 Service Reload (docker建置完畢)

2.5.1事前準備
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 →
2.5.2 安裝k8s package
2.5.3 開始建立control panel (建立Node請跳到2.6)
notes: 我使用過kubelet 1.26.0版本,會跑出It seems like the kubelet isn't running or healthy.錯誤訊息,所以選擇降版本
note: 無法使用cri-socket的話,使用default containerd.sock

2.5.4 複製一份root的憑證資訊到home目錄下
2.5.5 確認K8S Cluster是否建立完畢 (下面兩個步驟完成,代表k8s cluster建置完畢)
-
docker ps

-
kubectl get nodes

-
kubectl get cs

2.6 建立k8s node節點 (依照上述不走建置,下面簡單描述)
- 建立兩台node節點,所以啟用兩台VM (參考步驟1)
- 參考步驟2建立K8S,只有2.5.3 ~ 2.5.5 以後不需要操作
2.6.1 接下來node加入 k8s集群
note: 無法使用cri-socket的話,使用default containerd.sock
- 缺點: 無法從docker指令看到container資訊,要使用crictl取代
- 優點會更快,不依賴docker
介紹
2.6.2 node加入成功畫面:

2.6.3 到master中檢查是否node1有加入成功
kubectl get nodes

3 K8S建立完成後,建立簡單服務(Nginx)測試
3.1 先建立網路溝通橋樑(Flannel)
先查看當初建立cluster使用的網路

更改剛剛下載的檔案: kube-flannel.yml

接下來執行,等待一會後,查看所有node狀態,從NonReady -> Ready

創立nginx檔案


我們可以看到,nginx server透過Node Port轉發port,所以我們可以透過主機直接進行連線。
創立nginx檔案

4 K8S Dashboard
下載官方recommended.yaml
更新 recommended.yaml
-
加入hostNetwork: true

-
加入nodePort

-
更新k8s dashboard token參數時間,不然15分鐘後就要重新登入,非常痛苦

啟用k8s dashboard與檢查
kubectl get pods -n kubernetes-dashboard

kubectl get services -n kubernetes-dashboard

啟用成功後,與nginx一樣可以透過port 31367直接進行訪問:
權限方面按照以下連結進行處理
https://github.com/kubernetes/dashboard/blob/master/docs/user/access-control/creating-sample-user.md
成果:

錯誤 (未解決)
指令