《關於我怎麼把一年內學到的新手 IT/SRE 濃縮到 30 天筆記這檔事》 Day 07 使用 Kubespray 建立自己的 K8S(二)

本篇大綱

這篇文章是要講述前面我們利用 Kubespray 建立了自己的 Cluster,但是還沒有使用到,但如果需要加入或減少其他 Node 該怎麼辦呢?Kubespray 也早就想好這部分了!

內文

今天假設資源不夠了,需要新增節點,該怎麼辦呢?Kubespray 也寫好 Script 可以幫你部署了。

新增節點

這裡我們借用 gitlab-runner 來示範新增節點的部分

首先將 gitlab-runner 加入到 inventory/mycluster/inventory.ini 裡面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[all]
k8s-m0 ansible_host=192.168.200.101 ansible_user=ubuntu
k8s-n0 ansible_host=192.168.200.102 ansible_user=ubuntu
k8s-n1 ansible_host=192.168.200.103 ansible_user=ubuntu
gitlab-runner ansible_host=192.168.200.104 ansible_user=ubuntu

[kube_control_plane]
k8s-m0

[etcd]
k8s-m0

[kube_node]
k8s-n0
k8s-n1
gitlab-runner

[calico_rr]

[k8s_cluster:children]
kube_control_plane
kube_node
calico_rr

如果命令列前面沒有 (kubespray-venv) 記得再啟動一次

1
ubuntu@bastion-host:~$ source kubespray-venv/bin/activate

新增好了以後,最後把它給 scale.yml

1
(kubespray-venv) ubuntu@bastion-host:~$ ansible-playbook -i inventory/mycluster/inventory.ini --private-key=../private.key --become --become-user=root scale.yml

https://ithelp.ithome.com.tw/upload/images/20220922/20112934ErrdqtWq5g.png

跑完順利基本上沒有 Error。

https://ithelp.ithome.com.tw/upload/images/20220922/20112934ikrF3eLi79.png

這樣就新增完成了。

確認一下 Node 有進去:

1
(kubespray-venv) ubuntu@bastion-host:~$ kubectl get node

https://ithelp.ithome.com.tw/upload/images/20220922/201129348jjrKovQCJ.png

刪除節點

刪除節點也是很容易,但要刪除 Worker Node 之前,最好還是先手動確認 Pod 是不是已經從該節點清除掉了,不然移除了就沒辦法恢復喔!(如何手動確認 Pod 清除將會再寫一篇 Cheatsheet 做整理,這裡方便我們還是用命令直接刪除。)

1
(kubespray-venv) ubuntu@bastion-host:~$ ansible-playbook -i inventory/mycluster/inventory.ini --private-key=../private.key --become --become-user=root remove-node.yml --extra-vars "node=gitlab-runner"

如果該節點已經連不上了,請再新增 --extra-vars reset_nodes=false,如果是某些節點連不上,就個別在 inventory/mycluster/inventory.ini 加入 reset_nodes=false 就好。

打上 yes 即可刪除。

https://ithelp.ithome.com.tw/upload/images/20220922/20112934XwOdXmmZWl.png

確認一下 Node 已經刪除:

1
(kubespray-venv) ubuntu@bastion-host:~$ kubectl get node

https://ithelp.ithome.com.tw/upload/images/20220922/20112934VyHE4PuEBi.png

重置

如果有天想把 Cluster 刪除,那就是直接 reset 就好了。

1
ansible-playbook -i inventory/mycluster/inventory.ini --private-key=../private.key --become --become-user=root reset.yml

打上 yes 即可刪除。

https://ithelp.ithome.com.tw/upload/images/20220922/20112934VDmwwvYzoK.png

https://ithelp.ithome.com.tw/upload/images/20220922/20112934hJ5wXQuctL.png

升級 Cluster

這裡我反而沒有著墨太多,雖然 Kubespray 有提供 Upgrade 方式,但對於 K8s 升級其實有多要注意的事情,像是 API 更動,我的建議會是備份當前的設定 YAML,像是 Velero 這類的工具,建立測試用的 Cluster 再 Loading 進去,確認升級流程沒有問題再做會比較好。

升級 VM 作業系統的話,可以透過上面所述加入刪除節點的方式,將每一台升級完成。

今天的內容就針對一些比較常用的 Kubespray 操作做說明,下一篇就要來介紹 K8s 每個 Workloads 吧!

本系列內容也會同步貼到我的 iT 邦幫忙 https://ithelp.ithome.com.tw/users/20112934 歡迎來點一下追蹤,那我們就下一篇文章見啦!


《關於我怎麼把一年內學到的新手 IT/SRE 濃縮到 30 天筆記這檔事》 Day 07 使用 Kubespray 建立自己的 K8S(二)
https://blog.yangjerry.tw/2022/09/22/it2022-day07/
作者
Jerry Yang
發布於
2022年9月22日
許可協議