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

本篇大綱

這篇文章是要講述自建 K8s 了,如果不想採用 kubeadm 方式,可以採用 Kubespray 自動化安裝,實務上來說有多方測試調教會較穩定,也比較方便。

內文

Kubespray 我這裡採用當下最新版本 v2.19.1,需要先在系統內安裝 Ansible,Ansible 需要 Python 才能運作,我們可以看看文件來查看相容性問題。

https://github.com/kubernetes-sigs/kubespray/blob/v2.19.1/docs/ansible.md

VM 上的版本預先裝好的是 Python 3.10.4 的版本,依照官方教學文件建議裝上 Ansible 2.12 版本,並且使用 Python Virtual Environment。

day06-01.png

這裡我們先裝 Python Virtual Environment

1
2
ubuntu@bastion-host:~$ sudo apt update
ubuntu@bastion-host:~$ sudo apt install python3-virtualenv

day06-02.png

那這樣我們就可以把 Kubespray 專案抓下來了,複製下列命令就會抓特定版本。

1
ubuntu@bastion-host:~$ git clone --depth 1 --branch v2.19.1 https://github.com/kubernetes-sigs/kubespray.git

接下來就開始安裝 Kubespray 相關環境:

1
2
3
4
5
6
7
8
9
10
VENVDIR=kubespray-venv  # 指定 Python Virtualenv 的位置
KUBESPRAYDIR=kubespray # 指定 Kubespray 資料夾
ANSIBLE_VERSION=2.12 # 指定 Ansible 版本
virtualenv --python=$(which python3) $VENVDIR # 建立 Python Virtualenv
source $VENVDIR/bin/activate # 啟動 Python Virtualenv
cd $KUBESPRAYDIR # 進入資料夾
pip install -U -r requirements-$ANSIBLE_VERSION.txt # 安裝所需套件
test -f requirements-$ANSIBLE_VERSION.yml && \
ansible-galaxy role install -r requirements-$ANSIBLE_VERSION.yml && \
ansible-galaxy collection -r requirements-$ANSIBLE_VERSION.yml

這邊都會是順利進行的,然後就會看到前面多了 (kubespray-venv) 就代表已經啟動 Python Virtualenv 了。

接下來就來複製 Inventory:

1
cp -rfp inventory/sample inventory/mycluster

複製完後,編輯 inventory/mycluster/inventory.ini,那檔案就是我們的 Server 的設定相關資訊。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# ## Configure 'ip' variable to bind kubernetes services on a
# ## different ip than the default iface
# ## We should set etcd_member_name for etcd cluster. The node that is not a etcd member do not need to set the value, or can set the empty string value.
[all]
# node1 ansible_host=95.54.0.12 # ip=10.3.0.1 etcd_member_name=etcd1
# node2 ansible_host=95.54.0.13 # ip=10.3.0.2 etcd_member_name=etcd2
# node3 ansible_host=95.54.0.14 # ip=10.3.0.3 etcd_member_name=etcd3
# node4 ansible_host=95.54.0.15 # ip=10.3.0.4 etcd_member_name=etcd4
# node5 ansible_host=95.54.0.16 # ip=10.3.0.5 etcd_member_name=etcd5
# node6 ansible_host=95.54.0.17 # ip=10.3.0.6 etcd_member_name=etcd6

# ## configure a bastion host if your nodes are not directly reachable
# [bastion]
# bastion ansible_host=x.x.x.x ansible_user=some_user

[kube_control_plane]
# node1
# node2
# node3

[etcd]
# node1
# node2
# node3

[kube_node]
# node2
# node3
# node4
# node5
# node6

[calico_rr]

[k8s_cluster:children]
kube_control_plane
kube_node
calico_rr

這些相關參數設定可以在 https://github.com/kubernetes-sigs/kubespray/blob/v2.19.1/docs/ansible.md#inventory 這裡找到。

基本上把所有被註解的相關參數都刪除掉就好,[bastion] 如果你的 Node 是無法直接連線就需要設定,有需要的就自行設定,因為這裡都是直接連線到 bastion-host 做操作,因此不需要。

那就依照 Ansible 給的語法把 Host 資訊填寫上去,[all] 填寫所有的主機內容、IP、登入 Username。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[all]
# <主機名字> ansible_host=<IP> ansible_user=<USERNAME>
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

[kube_control_plane] # Master node
k8s-m0

[etcd] # etcd 的位置,我也還是選 Master node
k8s-m0

[kube_node] # Worker node
k8s-n0
k8s-n1

[calico_rr]

[k8s_cluster:children]
kube_control_plane
kube_node
calico_rr

參數基本上只要照預設也是可以跑,需要調參數就自行去翻 Kubespray 文件。

那我們就改兩個地方

  • inventory/mycluster/group_vars/k8s_cluster/k8s-cluster.ymlcluster_name,改成 k8s.yjerry.tw(名字自訂)。
  • inventory/mycluster/group_vars/all/all.yml 的 Line 20 解除註解 loadbalancer_apiserver_localhost 設定為 false,因為我們沒有多個 Master node,因此不需要 Load Balancer API Server。

最後下此命令安裝:

1
2
# ansible-playbook -i <INVENTORY_FILE> --private-key=<PRIVATE_KEY> --become --become-user=root cluster.yml
ansible-playbook -i inventory/mycluster/inventory.ini --private-key=~/private.key --become --become-user=root cluster.yml

安裝完以後會顯示這樣。

day06-03.png

我們已經安裝好了,但是需要拿它裡面的 Token 才可以存取,Token 會放在 Master node 的 /etc/kubernetes/admin.conf

1
2
3
4
5
# ssh -i ~/private.key [email protected]
ubuntu@k8s-m0:~$ sudo cp /etc/kubernetes/admin.conf ~/
ubuntu@k8s-m0:~$ sudo chown ubuntu:ubuntu ~/admin.conf
ubuntu@k8s-m0:~$ mkdir -p .kube
ubuntu@k8s-m0:~$ mv ~/admin.conf ~/.kube/config

嘗試用 kubectl 存取節點

1
2
ubuntu@k8s-m0:~$ kubectl get node
ubuntu@k8s-m0:~$ kubectl get pod -A

day06-04.png

那接下來我們就把 scp 把 config 檔案下載出來,回到 bastion-host

1
2
(kubespray-venv) ubuntu@bastion-host:~$ mkdir -p ~/.kube
(kubespray-venv) ubuntu@bastion-host:~$ scp -i ~/private.key [email protected]:~/.kube/config ~/.kube/config

開啟編輯,把裡面的 127.0.0.1 改為 192.168.200.101

但是因為我們還沒在 bastion-host 裝上 kubectl,去官方網站下載,因為 Cluster 版本為 1.23.7,下載的 kubectl 也是要在 1.23.7 (一個 minor version 以內是可以相容的,裝上 1.22 或 1.24 都可以)

1
2
3
4
5
6
7
8
9
10
11
curl -LO "https://dl.k8s.io/release/v1.23.7/bin/linux/amd64/kubectl"
curl -LO "https://dl.k8s.io/v1.23.7/bin/linux/amd64/kubectl.sha256"
echo "$(cat kubectl.sha256) kubectl" | sha256sum --check

# Check OK 就可以安裝

sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

# 刪除安裝的殘餘檔案

rm kubectl*

bastion-host 再執行一次 kubectl get node

1
kubectl get node

day06-05.png

這樣就可以在 bastion-host 控制 Kubernetes cluster 了。

到這裡為止我們已經建好 Cluster,但是如果想要擴充資源該怎麼辦呢?Kubespray 也可以做到,明天就接續講 Kubespray 加入刪除節點。

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


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