จะต้องบอกว่า วิธีการนี้ สามารถนำไปใช้กับ Linux แทบทุกตระกูล ไม่ว่าจะเป็น AlmaLinux หรือ Ubuntu
งั้นเรามาเริ่มกันเลย
1. ทำการแก้ไขไฟล์ /etc/hosts โดยการเพิ่ม host และ ip ของแต่ละตัว เช่น
10.166.1.2 k8s-master
10.166.1.3 k8s-worker1
10.166.1.4 k8s-worker2
2. ทำการ ตั้งชื่อ hostname ในแต่ละ host ให้ตรงกับที่ตั้งไว้ใน /etc/hosts ด้วยคำสั่ง
ใน host แรก
sudo hostnamectl set-hostname "k8s-master"
ใน host 2
sudo hostnamectl set-hostname "k8s-worker2"
ใน host 3
sudo hostnamectl set-hostname "k8s-worker3"
3. ทำการ disable swap ด้วยคำสั่ง ในทุกๆ โหนด/host
sudo swapoff -a
4. ทำการโหลด module ที่จำเป็น ในทุกๆ โหนด/host
sudo modprobe overlay
sudo modprobe br_netfilter
5. สร้างไฟล์เพื่อให้โหลดทุกครั้งที่รีบูต ในทุกๆ โหนด
sudo tee /etc/modules-load.d/k8s.conf <<EOF
overlay
br_netfilter
EOF
6. ทำการสร้างไฟล์ เพื่อแก้ไขค่าเกี่ยวกับเน็ตเวิร์ก
sudo nano /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
7. พิมพ์ คำสั่งเพื่อโหลดค่าใหม่
sudo sysctl --system
8. ทำการติดตั้ง docker ด้วยคำสั่ง
sudo dnf install dnf-plugins-core -y
sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo dnf install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
sudo systemctl enable --now docker
9. ทำการสร้างไฟล์ config ของ containerd ด้วยคำสั่ง
sudo sh -c "containerd config default > /etc/containerd/config.toml"
sudo sed -i 's/ SystemdCgroup = false/ SystemdCgroup = true/' /etc/containerd/config.toml
sudo systemctl restart containerd.service
10. ทำการติดตั้ง kubernetes ในทุกโหนด ด้วยคำสั่งเพิ่ม repo ก่อน
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/v1.33/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/v1.33/rpm/repodata/repomd.xml.key
EOF
11. ทำการติดตั้งโปรแกรมที่จำเป็น
sudo dnf install curl ca-certificates -y
12. ทำการติดตั้ง
sudo apt install kubelet kubeadm kubectl -y
13. คราวนี้ ทำการพิมพ์คำสั่ง ---เฉพาะใน master node----
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
หลังจากนั้น ระบบจะบอก ข้อมูลให้ท่านพิมพ์ โดยท่านจะต้องพิมพ์ตาม บนหน้าจอ
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
นอกจากนั้นท่านจะเห็นว่ามีข้อมูลการ join cluster ซึ่งท่านก็อปปี้และไปพิมพ์ใน worker node
14. ทำการติดตั้ง Calico CNI
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.30.2/manifests/operator-crds.yaml
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.30.2/manifests/tigera-operator.yaml
15. ดาวน์โหลด configure
curl https://raw.githubusercontent.com/projectcalico/calico/v3.30.2/manifests/custom-resources.yaml -O
*** เลข version อาจจะต้องเปลี่ยนเป็นตัวล่าสุด กรุณาเช็คในเว็บ Calico
16. แก้ค่า network ให้ตรงกับ network ที่ท่านใช้
sed -i 's/cidr: 192\.168\.0\.0\/16/cidr: 10.244.0.0\/16/g' custom-resources.yaml
โดยเราใช้ 10.244.0.0 เป็นค่าของเรา
17. แก้ไขไฟล์เข้าระบบ
kubectl create -f custom-resources.yaml