安裝Kuberentes(K8s)
使用環境
Ubuntu 20.04
docker 20.10.7
kubernetes 1.24.3
kubectl 1.24.3
kustomize 4.54
kubeadm 1.24.3
kubelet 1.24.3
docker安裝(建議這邊不要使用docker desktop版本)
先移除舊版sudo apt-get remove docker docker-engine docker.io containerd runc
更新現有apt資料庫(這一步跟我前面寫的docker桌面版本安裝一樣就不一一說明了)
1 | apt-get update |
Kuberentes安裝
在安裝 k8s 前,必須把系統上的 swap disable 。
1 | sudo swapoff -a #暫時性 |
1 | sudo apt-get update && sudo apt-get install -y apt-transport-https curl |
Kuberentes 在 master 中初始化 kubeadm(只有master端要做,node端請跳過這步)
1 | sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --service-cidr=10.245.0.0/16 --apiserver-advertise-address=<master_IP> |
重製kubeadm(有出問題再使用,使用完在重作上一步)
1 | kubeadm reset -f |
成功的話會看到下面顯示這個敘述
1 | Your Kubernetes control-plane has initialized successfully! |
如果這裡出錯可能試試看
1 | rm /etc/containerd/config.toml |
權限調整
1 | mkdir -p $HOME/.kube |
Kuberentes檢查版本
1 | kubectl version |
Kuberentes檢查node的Status
1 | kubectl get nodes |
安裝CNI
這裡使用官方建議的Calico
curl https://projectcalico.docs.tigera.io/manifests/calico.yaml -O
注意: 在 apply calico.yaml 前,如果你有意變更 Pod CIDR 的 IP 的話,請修正3644行的指令,將其填上你 init 的 Pod CIDR IP 修改CALICO_IPV4POOL_CIDR
value:
kubectl apply -f calico.yaml
將node端加入cluster
查詢節點token(如果剛剛master端那步沒有記下來的話)
kubeadm token create --print-join-command
查完會出現類似下面Apply cluster node的指令 ### Apply cluster node
1 | sudo kubeadm join 192.168.0.1:6443 --token f9bvtp.zfurci0tw593y4bu \ |
Kuberentes檢查元件狀態
kubectl -n kube-system get all
安裝Kuberentes dashboard
1 | kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.6.0/aio/deploy/recommended.yaml |
創建Kuberentes dashboard帳號
1 | kubectl apply -f dashboard-adminuser.yaml |
刪除帳號
1 | kubectl delete sa cicd |
安裝kubernetes metrics-server(要安裝metrics才能在dashboard看到資料)
抱歉我累了...
請自行閱讀
https://github.com/kubernetes-sigs/metrics-server#readme
錯誤排除
遇到xxx.xxx.xxx.xxx:8080
代表kubeadm inti後面少做權限調整遇到xxx.xxx.xxx.xxx:6443
1
2
3
4sudo -i
swapoff -a
exit
strace -eopenat kubectl version遇到kubernetes dashboard:connect : no route to host
代表IPtable 出包了1
2
3
4
5
6systemctl stop kubelet
systemctl stop docker
iptables - -flush
iptables -tnat - - flush
systemctl start kubelet
systemctl start docker其他故障排除
1
2
3sudo systemctl restart docker
sudo systemctl daemon-reload
sudo systemctl restart kubelet
參考資料
- https://kangjw.me/ubuntu-20-04-%E5%AE%89%E8%A3%9Ddocker/
- https://www.gundam.com.tw/2021/11/04/%E5%A6%82%E4%BD%95%E5%9C%A8-ubuntu-20-04-%E4%B8%8A%E5%AE%89%E8%A3%9D%E5%92%8C%E4%BD%BF%E7%94%A8-docker/
- https://hackmd.io/@cnsrl/BkjmFubTw#Step-4-Install-CNI
- https://github.com/z416352/Kubeadm-installation
- https://blog.csdn.net/qq_43580215/article/details/125153959
- https://www.akiicat.com/2019/04/26/Kubernetes/kubernetes-reinstall-on-ubuntu/
- https://blog.csdn.net/woay2008/article/details/93250137
- https://itnext.io/big-change-in-k8s-1-24-about-serviceaccounts-and-their-secrets-4b909a4af4e0
- https://kubernetes.io/zh-cn/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/
- https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/#pod-network
- https://ithelp.ithome.com.tw/articles/10203675
- https://github.com/kubernetes/kubernetes/issues/53533
- https://projectcalico.docs.tigera.io/getting-started/kubernetes/self-managed-onprem/onpremises#install-calico-with-etcd-datastore