O’z mashinangizda tez va oson Kubernetes-ni klasterini yaratmoqchimisiz? Dasturingizni Kubernetes-da sinab ko’rmoqchimisiz? Unda ushbu maqola sizga mo’ljallangan.

Ushbu maqolada men Kubernetes-ni ixcham ko’rinishi k3s uchun ko’p tugunli klaster yaratishni ko’rsatib o’taman. 

K3s - bu Rancher Labs tomondan ishlab chiqilgan Kubernetes-ning yengil distributivi. k3s barcha kerakli komponentlarni bitta ikkilik faylga to'plash murakkab konfiguratsiya fayllariga bo'lgan ehtiyojni yo'q qiladi va Kubernetes resurslarini tez va oson joylashtirish imkonini beradi.

Agar siz Linux operatsion tizimida ishlayotgan bo’lsangiz, unda k3s-ni muammosiz curl -sfL https://get.k3s.io | sh - bash buyrug’i bilan o’rnatishingiz mumkin. Lekin men Windows-da ishlayotganim sababli, k3s-ni Hyper-V gipervizoridagi Linux o’rnatilgan virtual mashinalarda o’rnataman. Virutal mashinalarni multipass vositasi yordamida yarataman.

 

Birlamchi shartlar

  • Hyper-V gipervizori sozlangan bo’lishi
  • multipass dasturi o’rnatilgan bo’lishi kerak. multipass-nu bu yerdan yuklab o’rnatishingiz mumkin.

 

Virutal mashinalarni yaratish

Klaster 1 ta asosiy tugun va 2 ta ishchi tugunlardan iborat virtual mashinalardan tashkil topadi va ularni mos ravishda k3smaster1, k3sworker1, k3sworker2 deb nomlaymiz. Har bir VM uchun 1 VCPU va 1 GB xotira ajratiladi.

 

Asosiy tugun uchun VM yaratish

Asosiy tugunni quyidagi buyruq yordamida yarating:

multipass launch –name k3smaster1

Buyruq muvaffaqiyatli bajarilgandan so’ng, terminal-da quyidagi xabarni ko’rasiz

Launched: k3smaster1

Yuqoridagi buyruq avval Ubuntu tasvirini ko’chirib oladi va uni VM da ishga tushiradi. multipass find buyrug’i yordamida qaysi tasvirlar mavjudligini ko'rish ko’rishingiz mumkin.

 

Yaratilgan VM haqida batafsil ma’lumot olish uchun quyidagini bajaring:

$ multipass info k3smaster1
Name:            k3smaster1
State:           Running
IPv4:            172.22.43.127
Release:         Ubuntu 22.04.3 LTS
Image hash:      870bd58b5c1e (Ubuntu 22.04 LTS)
CPU(s):          1
Load:            0.11 0.07 0.02
Disk usage:      1.5GiB out of 4.8GiB
Memory usage:    207.2MiB out of 892.2MiB
Mounts:          --

 

Ishchi tugunlarga mo’ljallangan VM-larni yaratish

Birinchi ishchi tugun uchun VM-ni yarating:

$ multipass launch –name k3sworker1

Ikkinchi ishchi tugun uchun VM-ni yarating:

$ multipass launch –name k3sworker2

 

Yaratilgan VM-lar ro’yhatini tekshiring:

$ multipass list
Name                    State         	   IPv4       		 Image
k3smaster1              Running           172.22.43.127    Ubuntu 22.04 LTS
k3sworker1              Running           172.22.44.167    Ubuntu 22.04 LTS
k3sworker2              Running           172.22.42.178    Ubuntu 22.04 LTS

Kubernetes klasteri uchun VM-lar tayyor!

 

Kubernetes tugunlarini yaratish

 

k3s asosiy tugunini yaratish

Asosiy VM ga kiring:

$ multipass shell k3smaster1

k3s-ni o’rnating

k3smaster1:~$ sudo curl -sfL https://get.k3s.io | sh -

ESLATMA! Agar VM-da bir nechta tarmoq interfeysi va IP manzili mavjud bo'lsa --flannel-iface va --node-ip parametrlaridan foydalanib tugun aynan qaysi interfeys bilan ishlashini belgilashingiz mumkin.

k3s o’rnatilishi uchun taxminan 30 soniya vaqt oladi va muvaffaqiyatli o'rnatilgandan keyin, terminal oxirida quyidagini ma’lumotni ko’rasiz:

…
[INFO]  systemd: Starting k3s

k3s asosiy komponentalaridan tashqari kubectl, crictl, k3s-killall.sh, k3s-uninstall.sh larni ham o’rnatadi.

Agar siz kubectl vositasidan root foydalanuvchisiz foydanalmoqchi bo’lsangiz, quyidagi xatolikka duch kelasiz:

Masalan klaster haqida ma’lumot olsihga harakat qiling:

k3smaster1:~$ kubectl cluster-info

Natijada quyidagiz xatolikni ko’rasiz:

error: error loading config file "/etc/rancher/k3s/k3s.yaml": open /etc/rancher/k3s/k3s.yaml: permission denied

Xataolik sababi: Odatda KUBCECONFIG fayli /etc/rancher/k3s/k3s.yaml manzilida joylashgan va u root foydalanuvchisi uchun faqat o'qish ruxsatiga ega. Shu sababli kubectl buyruqlarini sudo bilan bajarishingizni talab qiladi. Buni to'g'rilash uchun siz boshqa foydalanuvchilarga ham ruxsat berishingiz yoki VM-ga root foydalanuchisi bilan kirishingiz kerak.

KUBCECONFIG faylini boshqa foydalanuvchilar uchun ham o'qiy oladigan qilish uchun quyidagini bajaring:

k3smaster1:~$ sudo chmod 644 /etc/rancher/k3s/k3s.yaml

Buni tekshirish uchun klaster haqida ma’lumot oling:

k3smaster1:~$ kubectl cluster-info
Kubernetes control plane is running at https://127.0.0.1:6443
CoreDNS is running at https://127.0.0.1:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
Metrics-server is running at https://127.0.0.1:6443/api/v1/namespaces/kube-system/services/https:metrics-server:https/proxy

Umuman olganda sizda 1 tugunli klaster foydalanishga tayyor. Keling, qolgan ishchi tugunlarni klasterga bog’laylik.

Worker1 tugunini klasterga ulash uchun siz k3smaster1 serveridan token olishingiz kerak:

master1:~$ sudo cat /var/lib/rancher/k3s/server/node-token
K10b5b5972c3b9303a3d25e1bef544c11eb1abf592135e95961d727a5fcd7530733::server:****

Tokenni olganingizdan keyin k3sworker1 ishchi tugunini yarating:

TAKLIF! Har bir tugun uchun buyruqlarni alohida terminllarda bajaring.

k3sworker1 ishchi tuguniga kiring:

multipass shell k3sworker1

ESLATMA! VM-ga root foydalanuvchisi bilan kirish uchun multipass shell k3sworker1 o’rniga multipass exec -n k3sworker1 -- sudo bash buyrug'idan foydalanish mumkin.

root@k3sworker1:/# curl -sfL https://get.k3s.io | K3S_URL=https://172.22.43.127:6443 K3S_TOKEN=**** sh -

K3S_URL - asosiy tugun manizili. Uni multipass list yordamidan ko’rishingiz mumkin.

K3S_TOKEN - asosiy tugun tokeni. Uni yuqorida serverga so’rov yordamida oldingiz.

 

k3s o’rnatilishi bir muncha vaqt talab qiladi! Ishchi tugun muvaffaqiyatli o’rnatilgandan keyin, terminalda quyidagi ma’lumotni ko’rasiz:

[INFO]  systemd: Starting k3s-agent

Xuddi shu kabi, yuqoridagi buyruqni k3sworker2 ikkinchi ishchi tuguni uchun ham bajaring.

 

Ishchi tugunlarni muvaffaqiyatli o’rnatgandan keyin, tugunlar ro’yhatini ko’ring:

k3smaster1:~$ kubectl get node
NAME          STATUS    ROLES                   AGE     VERSION
k3smaster1    Ready     control-plane,master    20m     v1.27.4+k3s1
k3sworker1    Ready     <none>                  9m      v1.27.4+k3s1
k3sworker2    Ready     <none>                  8m7s    v1.27.4+k3s1

 

k3s klasteri foydalanish uchun tayyor!

 

k3s-ni o’chirish

k3s server-ni o’chirish uchun quyidagini bajaring:

master1:~$ sudo /usr/local/bin/k3s-uninstall.sh

k3s agent-larini o’chirish uchun quyidagini bajaring:

worker1:~$ sudo /usr/local/bin/k3s-agent-uninstall.sh

 

VM-larni o’chirish

multipass delete –all –purge

 

Qo’shimcha ma’lumotlar

  • Har gal asosiy tugunga kirmaslik uchun, KUBCECONFIG faylini multipass transfer buyrug’i yordamida xost mashinangizga ko’chirib, xost mashina terminalidan foydalasangiz bo’ladi:

    1. KUBCECONFIG faylini xost mashinangizga ko’chiring
multipass transfer k3smaster1:/etc/rancher/k3s/k3s.yaml d:\.kube\config\k3s

          2. Faylni oching va k3smaster1 server manzilini o'zgartirib faylni saqlang:

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: ***
    # server - k3s server manizli
    server: https://172.22.43.127:6443
  name: default

        3. PowerShell-da KUBECONFIG o’zgaruchisini ko’chirib olgan k3s.yaml manizlini ko’rsating: 

$env:KUBECONFIG="d:\.kube\config\k3s\k3s.yaml"

 

 

Yana nimalarni bilish kerak?

E’tibor bergan bo'lsangiz virutal mashinalarimiz tarmog’ini sozlamadik. Odatda VM yaratganimizda Hyper-V “Default Switch” kommutatoridan foydalanadi va ularga dinamik IP manzilini tayinlaydi. Bu esa, VM-larni yoki xost mashinangizni qayta ishga tushirganingizda VM-larning IP manzili o'zgarishi oqibatida klaster tugunlari o'rtasida aloqa uzilib, nosoz holatga kelishi mumkin. Bunining uchun NAT tarmog’ini sozlashingiz kerak. Hyper-V da NAT tarmog’ini sozlash uchun ushbu havoladan batafsil ma’lumot olishingiz mumkin va multipass vositasi yordamida VM-larni Static IP bilan ishga tushirishni bu yerdan ko’rishingiz mumkin.