Kubernetes 環境準備 - centos7 & containerd
定期更新版 kubernetes 安裝筆記
K8s: | v1.25.0 |
OS: | CentOs 7 (kernel = 3.10.1) |
cri: | containerd 1.6.8-3.1.el7 |
Update Time | 2022/09/14 |
主機校時
1 安裝所需套件
|
|
2 啟用自動校正
編輯設定檔
1
sudo vi /etc/ntp.conf
把 server 區塊的設定調整一下,改成中華電信的校時服務 (會比使用 linux 的校時伺服器快一點,因為比較近)
1 2 3 4 5 6 7 8 9 10 11 12
# org linux time server # server 0.centos.pool.ntp.org iburst # server 1.centos.pool.ntp.org iburst # server 2.centos.pool.ntp.org iburst # server 3.centos.pool.ntp.org iburst # taiwan time server server tock.stdtime.gov.tw server watch.stdtime.gov.tw server time.stdtime.gov.tw server clock.stdtime.gov.tw server tick.stdtime.gov.tw
將校時功能加入系統服務
1 2
sudo systemctl start ntpd sudo systemctl enable ntpd
確認啟用狀態
1
systemctl status ntpd
3 參考
containerd
1 核心套件安裝
更新 yum 並定義 docker 工具來源
信息因為 containerd 的最新版也在 docker 的儲存庫中,所以直接使用 docker 的來源1 2 3 4
sudo yum install -y yum-utils sudo yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo
安裝 containerd
1
sudo yum install containerd.io
需要指定版本可使用以下方式安裝
列出可用的版本
1
sudo yum list containerd.io --showduplicates | sort -r
安裝指定版本
1
sudo yum install containerd.io-<VERSION_STRING>
2 調整設定檔
path: /etc/containerd/config.toml
使用 yum 安裝 containerd 時的預設設定檔長這樣
|
|
使用 containerd 內部的預設設定檔覆蓋後再去調整 SystemdCgroup=true
|
|
設定 systemdCgroup = true (官方文件參考)
|
|
3 建立 crictl 設定調整
這個設定檔一開始不存在,需要自己建立並寫入以下內容
|
|
yaml 內容如下
|
|
4 其他輔助性質的工具安裝
4.1 containerd 的 CNI
cni 在文章撰寫當下 (2022-09-19 最新版本為 1.1.1) cni github
|
|
4.2 nerdctl
nerdctl Github 是輔助性質的命令列工具,建議安裝
撰文當下最新版本連結如下
https://github.com/containerd/nerdctl/releases/download/v0.23.0/nerdctl-0.23.0-linux-amd64.tar.gz
|
|
5 啟動 containerd
|
|
Network Setting
ref: 官方文件
|
|
Disable Swap
暫時關閉
```bash sudo swapoff -a ```
警告此命令僅是暫時性關閉,server 重開後仍會啟用 swap,因此需要搭配下一節的設定來完全關閉 swap永久關閉 swap
註解掉以下命令開啟的檔案中,含有 swap 字樣的行次
1
sudo vim /etc/fstab
用 vim 開啟
/etc/sysctl.conf
檔案,並加入vm.swappiness=0
有時候沒有設定此行也可以正常運行,但如果可以的話,關掉也許比較好
新主機設定後此檔案應該會長這樣
1 2 3 4 5 6 7 8 9 10 11
# sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/. # # Vendors settings live in /usr/lib/sysctl.d/. # To override a whole file, create a new file with the same in # /etc/sysctl.d/ and put new settings there. To override # only specific settings, add a file with a lexically later # name in /etc/sysctl.d/ and put new settings there. # # For more information, see sysctl.conf(5) and sysctl.d(5). vm.swappiness=0
Kubernetes CLI & SELinux Set
ref: 官方文件
|
|
先前舊的命令會有 repo_gpgcheck 的問題,在撰文當下 (2022/09/12) 已被修正
若仍有相關問題的話,請參考官方說明
|
|
安裝完畢後可看到安裝了已下套件
|
|
kubernetes cluster init / join command (use kubeadm)
預先拉取需要的 Image
1
kubeadm config images pull
建立 kubernetes cluster
1 2 3 4 5
kubeadm init # 指定 control-plane-endpoint kubeadm init --control-plane-endpoint=<control-plane-endpoint-domain> \ --upload-certs
reset kubernetes cluster
1 2 3 4 5 6 7
kubeadm reset # 清除資料 rm -rf /etc/cni/net.d # 重設 ip table iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
join kubernetes cluster
1 2
kubeadm join <control-endpoint>:6443 --token <token> \ --discovery-token-ca-cert-hash sha256:<hash>
Other Setting
1 Master node
Master Node 在 kubeadm join / init 結束之後,可以考慮將 kubeconfig 檔案設定在 bash_profile 裡面
|
|
.bash_profile 的設定
|
|
2 Weave net CNI
如果是正在建立叢集,就會需要另外安裝 CNI ( Container Network Interface ),這邊採用 Weave net CNI
直接安裝
1
kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
如果要特別調整網段
1
kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')&env.IPALLOC_RANGE=172.30.0.0/16"
清除安裝
1
kubectl delete -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
網段資料參考
全預設 weave-net cni 的 kubernetes cluster 網段
Used For | Subnet | Genmask | Start | End |
---|---|---|---|---|
Kubernetes Pod Subnet (Weave-cni) | 10.32.0.0/12 | 255.240.0.0 | 10.32.0.1 | 10.47.255.254 |
Kubernetes Service Subnet | 10.96.0.0/12 | 255.240.0.0 | 10.96.0.1 | 10.111.255.254 |