今天使用的是兩個VM VirtualBox Ubuntu20.04,配置如下:
在安裝k8s前,請先確認是否已安裝好Docker。
安裝Docker的步驟如下:
sudo apt update
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get install docker-ce
docker --version
sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker
安裝好Docker之後,就開始安裝k8s吧。
在Master跟worker執行
sudo su -c "curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -"
sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
sudo apt update
version=1.18.20-00
echo $version
apt-cache show kubectl | grep "Version: 1.18.20-00"
# Install k8s
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
sudo apt install -y kubelet=$version kubectl=$version kubeadm=$version
sudo apt-mark hold kubelet kubeadm kubectl
cat <<EOF | sudo tee /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
EOF
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo systemctl daemon-reload
sudo systemctl restart docker
只在Master執行
sudo kubeadm init --v=5 --pod-network-cidr=10.244.0.0/16
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
初始化結束後,會拿到一串指令:
kubeadm join 192.168.1.111:6443 --token 1p1r4a.9eh3cybvgw8tdvju \
--discovery-token-ca-cert-hash sha256:7b14f09e50c9d8d5e88ce10bad16034d71da947bbdeb124c9e0438ae400c543a
再來就可以用這串指令,讓Node加入到此叢集了。再來執行kubectl get node
確認一下。
root@master:/home/ubuntu# kubectl get node
NAME STATUS ROLES AGE VERSION
master NotReady master 4m7s v1.18.20
node NotReady <none> 83s v1.18.20
確定有master和node之後,你會發現status顯示NotReady
,這時只要輸入下面的指令去部署CNI
:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
等待一下,再次執行kubectl get node
:
root@master:/home/ubuntu# kubectl get node
NAME STATUS ROLES AGE VERSION
master Ready master 4m23s v1.18.20
node Ready <none> 99s v1.18.20
您好 :
Sorry!我是K8S的新手,請問一下輸入kubectl get node , 只看到 master 沒看到 node , 要如何解決 , 辛苦您了 ,謝謝您!
Sorry! 我忘記在 node 執行 kubeadm join , 問題已解決!