DEV Community

Cover image for kind ของดีที่ทำให้เรามี K8S Cluster ใช้งานแบบไม่ยุ่งยาก บน macOS
Damrongsak Reetanon
Damrongsak Reetanon

Posted on

4 1

kind ของดีที่ทำให้เรามี K8S Cluster ใช้งานแบบไม่ยุ่งยาก บน macOS

ตอนนี้เขียนจากส่วนประกอบต่าง ๆ ดังนี้

  • macOS Catalina version 10.15.7
  • Docker Desktop version 3.1.0(51484)
  • สร้าง Kubernetes Cluster ชื่อว่า d8k ประกอบด้วย 1 Control Plane และ 2 Node

kind เป็นเครื่องมือที่จะทำให้มี Kubernetes Cluster ใช้งานโดยใช้ Docker Container เป็น node ดังนั้นถ้าระบบปฎิบัติการใดที่มี kind ให้ใช้งานได้ และติดตั้ง Docker ได้ก็จะทำได้ เท่าที่อ่านดูก็เป็นการจับคู่ที่หลากหลาย อย่างใน macOS ก็จะเป็น kind สำหรับ macOS และ Docker Desktop หรือว่า kind สำหรับ Windows 10 และ WSL2 - Windods Subsystem for Linux 2 ส่วนใน Linux ไม่น่าจะต้องห่วงยังไม่ได้ลอง แต่มั่นใจว่าน่าจะใช้งาน kind ได้อยู่แล้ว

ขั้นตอนการเตรียม Docker Desktop ให้มีหน่วยความจำเพียงพอสำหรับการทำงานของ Kubernetes Cluster
Alt Text

ติดตั้ง kind ด้วยเครื่องมือ brew

❯ brew install kind
Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/kind-0.10.0.catalina.bottle.tar.gz
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/88fc4241b464cfec95189c325d2d28eecb0b04aeb1d7e56f9d04a7cd82f
######################################################################## 100.0%
==> Pouring kind-0.10.0.catalina.bottle.tar.gz
==> Caveats
zsh completions have been installed to:
  /usr/local/share/zsh/site-functions
==> Summary
🍺  /usr/local/Cellar/kind/0.10.0: 8 files, 9.2MB
Enter fullscreen mode Exit fullscreen mode

สร้าง Configuration File สำหรับ kind เพื่อสร้าง 1 Control Plane และ 2 Node

❯ cat > lab.yaml <<EOF
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
EOF
Enter fullscreen mode Exit fullscreen mode

สร้าง Kubernetes Cluster ชื่อว่า d8k จาก lab.yaml

❯ kind create cluster --config lab.yaml --name d8k
Creating cluster "d8k" ...
 ✓ Ensuring node image (kindest/node:v1.20.2) 🖼
 ✓ Preparing nodes 📦 📦 📦
 ✓ Writing configuration 📜
 ✓ Starting control-plane 🕹️
 ✓ Installing CNI 🔌
 ✓ Installing StorageClass 💾
 ✓ Joining worker nodes 🚜
Set kubectl context to "kind-d8k"
You can now use your cluster with:

kubectl cluster-info --context kind-d8k

Have a nice day! 👋
Enter fullscreen mode Exit fullscreen mode
  • การติดตั้งใช้เวลานานพอสมควรเนื่อจาก Container Image ของ kind node (v1.20.2) มีขนาด 1.17 GB
  • หลังจากติดตั้งเสร็จ kind จะเพิ่มข้อมูลลงใน .kube/config เพื่อให้พร้อมใช้งาน

​ตรวจสอบความเรียบร้อย และความพร้อมของ Kubenetes Cluster

❯ kind get clusters
d8k

❯ kubectl config get-contexts
CURRENT   NAME                          CLUSTER      AUTHINFO           NAMESPACE
*         kind-d8k                      kind-d8k     kind-d8k
          kubernetes-admin@kubernetes   kubernetes   kubernetes-admin

❯ kubectl get nodes
NAME                STATUS   ROLES                  AGE     VERSION
d8k-control-plane   Ready    control-plane,master   8m41s   v1.20.2
d8k-worker          Ready    <none>                 6m55s   v1.20.2
d8k-worker2         Ready    <none>                 6m56s   v1.20.2

❯ kubectl cluster-info
Kubernetes control plane is running at https://127.0.0.1:50358
KubeDNS is running at https://127.0.0.1:50358/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
Enter fullscreen mode Exit fullscreen mode

หากไม่ต้องการใช้งาน Kubernetes Cluster แล้วสามารถลบด้วยคำสั่ง kind

❯ kind delete clusters d8k
Deleted clusters: ["d8k"]
Enter fullscreen mode Exit fullscreen mode

หากต้องการระบุ version ของ Kubernetes สามารถทำได้โดยการระบุ version ได้ โดยข้อมูลของ image ในแต่ละ version หาได้จาก kind release เช่นต้องการติดตั้ง Kubernetes Cluster version 1.19.7

❯ cat > lab.yaml <<EOF
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  image: kindest/node:v1.19.7@sha256:a70639454e97a4b733f9d9b67e12c01f6b0297449d5b9cbbef87473458e26dca
- role: worker
  image: kindest/node:v1.19.7@sha256:a70639454e97a4b733f9d9b67e12c01f6b0297449d5b9cbbef87473458e26dca
- role: worker
  image: kindest/node:v1.19.7@sha256:a70639454e97a4b733f9d9b67e12c01f6b0297449d5b9cbbef87473458e26dca
EOF

❯ kind create cluster --config lab.yaml --name d8k
Creating cluster "d8k" ...
 ✓ Ensuring node image (kindest/node:v1.19.7) 🖼
 ✓ Preparing nodes 📦 📦 📦
 ✓ Writing configuration 📜
 ✓ Starting control-plane 🕹️
 ✓ Installing CNI 🔌
 ✓ Installing StorageClass 💾
 ✓ Joining worker nodes 🚜
Set kubectl context to "kind-d8k"
You can now use your cluster with:

kubectl cluster-info --context kind-d8k

Have a nice day! 👋

❯ kubectl get nodes
NAME                STATUS   ROLES    AGE     VERSION
d8k-control-plane   Ready    master   3m2s    v1.19.7
d8k-worker          Ready    <none>   2m24s   v1.19.7
d8k-worker2         Ready    <none>   2m24s   v1.19.7
Enter fullscreen mode Exit fullscreen mode

สร้าง pod แรกใน Kubernetes ที่สร้างโดย kind

❯ kubectl run myfiristpod --image damrongsak/hello:8080
pod/myfiristpod created
❯ kubectl get pods
NAME          READY   STATUS    RESTARTS   AGE
myfiristpod   1/1     Running   0          28s
Enter fullscreen mode Exit fullscreen mode

มูลค่าความสุข

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay