接上篇
1、安裝 kubeadm、kubelet 和 kubectl
1.1、配置yum源
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
1.2、安裝
你需要在每台機器上安裝以下的軟體包:
- kubeadm:用來初始化叢集的指令。
- kubelet:在叢集中的每個節點上用來啟動 Pod 和容器等。
- kubectl:用來與叢集通信的指令行工具。
指令:
sudo yum install -y kubelet-1.25.0 kubeadm-1.25.0 kubectl-1.25.0
sudo systemctl enable --now kubelet
kubeadm version
執行個體:
2、初始化master節點(192.168.13.60)
在master節點伺服器上執行以下指令:
kubeadm init \
--apiserver-advertise-address=192.168.13.60(master節點IP) \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.25.0 \
--service-cidr=10.10.0.0/12 \
--pod-network-cidr=172.17.0.0/16 \
--ignore-preflight-errors=all \
--cri-socket unix:///var/run/cri-dockerd.sock
(1)、執行報錯解決:
kubelet啟動失敗原因:CRI v1 runtime API is not implemented for end
原因是:cri-dockerd 仍然隻支援 v1alpha2。Kubelet 在 1.26 ( kubernetes/kubernetes#110618 ) 中移除了對 v1alpha2 的支援。
注意: kubernetes1.26和cri-docker不相容,導緻kubelet啟動失敗,需要更換1.25
(2)、執行指令逾時:Initial timeout of 40s passed.
這個時候執行 kubectl version 指令,可以看到:The connection to the server localhost:8080 was refused - did you specify the right host or port?
解決辦法:
vim /etc/profile 在底部增加新的環境變量 export KUBECONFIG=/etc/kubernetes/admin.conf
然後執行 source /etc/profile
如果執行成功,最後會有這一段
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Alternatively, if you are the root user, you can run:
export KUBECONFIG=/etc/kubernetes/admin.conf
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.13.60:6443 --token 3ggk0l.plfnen3vqw3iwee0 \
--discovery-token-ca-cert-hash sha256:f876e746171ac520d6cd66897e2dd6a703e950cafff98584a23407a4e72abfdd
3、初始化worker節點
3.1、在worker節點上執行指令,将worker加入到叢集中
3.2、檢視叢集節點資訊(所有的指令必須都在k8s-master節點中執行)
指令:
kubectl get node -o wide
4、安裝網絡插件calico
4.1、下載下傳yml檔案
wget https://docs.projectcalico.org/manifests/calico.yaml --no-check-certificate
修改定義pod網絡CALICO_IPV4POOL_CIDR的值和kubeadm init pod-network-cidr的值一緻
## 取消注釋
- name: CALICO_IPV4POOL_CIDR
value: "172.17.0.0/16"
3.2、然後自行修改,建議大家使用我修改過的yml直接使用
下載下傳位址:https://download.csdn.net/download/u011837804/87315867
3.3、在k8s-master節點執行指令安裝calico插件
kubectl apply -f calico.yaml
4、測試 kubernetes 叢集
以nginx為例,在k8s-master上執行以下指令
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc
以任意節點IP通路:http://node節點IP:30696 (注意:30696是上述圖中service/nginx的端口,是随機生成的)