網(wǎng)絡(luò)上教程很多是舊版本的鏡像犁河,拉取不到。需要按照自己的情況去搭建魄梯。本文使用的kubernete版本是1.12.1桨螺。
一、環(huán)境安裝
- ubuntu == 20.04
- docker == 20.10.7
- k8s == 1.21.1
教程如下:https://blog.csdn.net/M82_A1/article/details/95635705/
$ apt-get install -y kubelet=1.21.1-00 kubeadm=1.21.1-00 kubectl=1.21.1-00
# 卸載方法
$ kubeadm reset
$ apt-get remove kubelet kubeadm kubectl
禁用swap:參考 https://zhuanlan.zhihu.com/p/138554103
$ sudo swapoff -a
$ sudo vim /etc/fstab #修改/etc/fstab酿秸,注釋掉swap那行灭翔,持久化生效
注冊(cè)aliyun賬號(hào)并且,docker login: 參考: https://help.aliyun.com/document_detail/60743.html
拉取相關(guān)的image允扇,其中有一個(gè)bug缠局,需要重新打tag:
# 查詢需要下載的鏡像
$ kubeadm config images list --config kubeadm.yaml
# 查詢已經(jīng)有的鏡像
$ docker images
# 注意coredns會(huì)出現(xiàn)問題
$ docker pull registry.aliyuncs.com/google_containers/coredns:1.8.0
# 重命名
$ docker tag registry.aliyuncs.com/google_containers/coredns:1.8.0 registry.aliyuncs.com/google_containers/coredns/coredns:v1.8.0
# 刪除原有鏡像
$ docker rmi registry.aliyuncs.com/google_containers/coredns:1.8.0
二则奥、k8s集群組建
目的:一個(gè)master node,一個(gè)worker node.
執(zhí)行命令:
$ kubeadm init --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=10.112.122.9
其中10.112.122.9是master node host ip狭园,執(zhí)行結(jié)果:
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 10.112.122.9:6443 --token u4nw4p.3fxtty1sbpkyi6n0 \
--discovery-token-ca-cert-hash sha256:c1ea97710f4e5ed7722b0e293ca9f7f2db66fac1cde17d65c22a724d08c5a1af
完成初始化操作:
$ mkdir -p $HOME/.kube
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config
一個(gè)誤操作和復(fù)原方法读处,記錄
$ kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
$ kubectl delete -f https://docs.projectcalico.org/manifests/calico.yaml
此時(shí)網(wǎng)絡(luò)還沒跑起來:
$ kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-545d6fc579-lxzt9 0/1 Pending 0 2m6s
coredns-545d6fc579-tx6sj 0/1 Pending 0 2m6s
etcd-hyongtaonew-precision-tower-5810 1/1 Running 0 2m21s
kube-apiserver-hyongtaonew-precision-tower-5810 1/1 Running 0 2m16s
kube-controller-manager-hyongtaonew-precision-tower-5810 1/1 Running 0 2m16s
kube-proxy-d5b69 1/1 Running 0 2m6s
kube-scheduler-hyongtaonew-precision-tower-5810 1/1 Running 0 2m16s
CNI網(wǎng)絡(luò)部署
# 這個(gè)yaml文件有700多行 不復(fù)制了
$ wget https://docs.projectcalico.org/v3.10/manifests/calico.yaml
$ kubectl apply -f calico.yaml
稍等片刻后,coredns等服務(wù)都處于running狀態(tài)了:
$ kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
calico-kube-controllers-7c5dd46f7d-bm5m5 1/1 Running 0 77s
calico-node-pl2cr 0/1 Running 0 77s
coredns-545d6fc579-lxzt9 1/1 Running 0 7m23s
coredns-545d6fc579-tx6sj 1/1 Running 0 7m23s
etcd-hyongtaonew-precision-tower-5810 1/1 Running 0 7m38s
kube-apiserver-hyongtaonew-precision-tower-5810 1/1 Running 0 7m33s
kube-controller-manager-hyongtaonew-precision-tower-5810 1/1 Running 0 7m33s
kube-proxy-d5b69 1/1 Running 0 7m23s
kube-scheduler-hyongtaonew-precision-tower-5810 1/1 Running 0 7m33s
構(gòu)建Worker Node
# 在worker node執(zhí)行下面的命令
$ kubeadm join 10.112.122.9:6443 --token u4nw4p.3fxtty1sbpkyi6n0 --discovery-token-ca-cert-hash sha256:c1ea97710f4e5ed7722b0e293ca9f7f2db66fac1cde17d65c22a724d08c5a1af
# 在master node執(zhí)行
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
amdlattice-precision-tower-5810 Ready <none> 2m v1.21.1
hyongtaonew-precision-tower-5810 Ready control-plane,master 14m v1.21.1
啟動(dòng)一個(gè)Nginx服務(wù)
$ kubectl create deployment nginx --image=nginx
deployment.apps/nginx created
$ kubectl expose deployment nginx --port=80 --type=NodePort # 暴露80端口
service/nginx exposed
$ kubectl get pod,svc # 查看對(duì)外端口
NAME READY STATUS RESTARTS AGE
pod/nginx-6799fc88d8-fzhnv 1/1 Running 0 29s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/flaskapp-1 NodePort 10.99.22.77 <none> 5000:30005/TCP 3m15s
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 19m
service/nginx NodePort 10.109.12.14 <none> 80:32101/TCP 16s
最后訪問http://10.117.169.29:32101/的結(jié)果圖:
10.117.169.29為worker node的真實(shí)ip唱矛。
nginx.png
三罚舱、幾個(gè)相關(guān)的yaml文件
kubeadm.yaml的內(nèi)容,沒有用到绎谦,留作參考:
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
controllerManager:
extraArgs:
horizontal-pod-autoscaler-use-rest-clients: "true"
horizontal-pod-autoscaler-sync-period: "10s"
node-monitor-grace-period: "10s"
apiServer:
extraArgs:
runtime-config: "api/all=true"
kubernetesVersion: v1.21.1
imageRepository: registry.aliyuncs.com/google_containers