Coding Gun

ติดตั้ง Kubernetes บน local ด้วย Kind

Kind ย่อมาจาก Kubernetes in Docker ตือ เครื่องมือที่ใช้จำลอง Kubernetes cluster ขึ้นมาในเครื่องของเรา ซึ่งข้อดีของ Kind คือสามารถเพิ่มและลบ node ได้ตามที่เราต้องการ

คำสั่งของ Kind ที่ต้องรู้จัก

คำสั่งแรกคือ คำสั่งในการสร้าง Kubernetes Cluster เราสามารถสั่ง create cluster ได้เลย โดยที่ไม่ค้องกำหนด parameter อะไรแบบนี้

$ kind create cluster

หลังจากที่เราสร้าง cluster ขึ้นมาแล้วให้ลอง list รายการของ docker container ออกมาดู

$ docker ps

เราจะพบว่ามี container ที่ถูกสร้างขึ้นมาใหม่ 1 ตัว คือ kind-control-plane ทำหน้าที่เป็น master node แต่จะสังเกตุว่าเรายังไม่มี worker node เลยซักตัว ซึ่งเดี๋ยวเราจะทำการเพิ่มขึ่้นมาหลังจากนี้

CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS          PORTS                       NAMES
3cb0473dd019   kindest/node:v1.21.1     "/usr/local/bin/entr…"   3 minutes ago    Up 3 minutes    127.0.0.1:60058->6443/tcp   kind-control-plane

ตอนนนี้่เราสร้าง Cluster ขึ้นมา 1 Cluster ซึ่ง Cluster ที่สร้างขึ้นมาจะมีชื่อว่า kind(ถ้าไม่ได้ตัวชื่อใหม่ cluster จะเป็นชื่อนี้) แต่ถ้าเราต้องการตั้งชื่อของ cluster ให้เติม –name เข้าไปแบบนี้

$ kind create cluster --name demo-cluster

คราวนี้เราจะได้ cluster ตัวที่ 2 ซึ่งมีชื่อว่า demo-cluster ลอง docker ps ดูใหม่จะพบว่าเรามี container เพิ่มขึ้นมาอีก 1 ตัวคือ demo-cluster-control-plane (ชื่อ cluster ตามด้วยคำว่า control-plane)

$ docker ps

CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS          PORTS                       NAMES
610fc9255a20   kindest/node:v1.21.1     "/usr/local/bin/entr…"   29 seconds ago   Up 26 seconds   127.0.0.1:60083->6443/tcp   demo-cluster-control-plane
3cb0473dd019   kindest/node:v1.21.1     "/usr/local/bin/entr…"   3 minutes ago    Up 3 minutes    127.0.0.1:60058->6443/tcp   kind-control-plane

ตอนนี้เรามี cluster อยู่ 2 cluster เราสามารถ list รายการของ cluster ทั้งหมดที่มีออกมาดูด้วย

$ kind get clusters

เราจะได้ผลลัพธ์ออกมาแบบนี้

demo-cluster
kind

ทีนี้ตำถามที่ตามมาก็คือเราจะติดต่อกับ cluster แต่ละตัวได้ยังไง เมื่อเรา run คำสั่ง kubectl แล้วจะมีผลกับ cluster ไหน คำตอบคือ cluster ไหนถูกสร้างขึ้่นมาหลังสุด kubectl จะชี้ไปที่ cluster นั้น ซึ่งถ้าเราต้องการสลับไปทำงานกับ cluster อื่นให้เปลี่ยน context ไปมาด้วยคำสั่งนี้

$ kubectl config use-context kind-[ชื่อ cluster]

Context ของ Kind จะชึ้นต้นด้วยคำว่า kind แล้วตามด้วยชื่อ cluster เช่น ถ้าเราต้องการกลับไปทำงานกับ cluster ตัวแรกให้ใช้คำสั่ง

$ kubectl config use-context kind-kind

จะมีคำว่า kind ซ้ำกัน 2 รอบ เพราะ kind- เป็น prefix นำหน้าชื่อ context จะได้ไม่ชนกับ context ที่เราสร้างเอง ส่วน kind ตัวหลังคือชื่อ cluster ที่เป็น default เมื่อรวมกันแล้วเลยกลายเป็น kind-kind เช่นเดียวกันถ้าเราต้องการกลับไปทำงานใน demo-cluster เราจะใช้คำสั่ง

$ kubectl config use-context kind-demo-cluster

Kind Configuration

เราสามารถกำหนดจำนวนของ node ใน cluster ได้ด้วยการเขียน configuration file ในตัวอย่างนี้ผมจะตั้งชื่อไฟล์ว่า kind-config.yaml ซึ่งจะมีเนื้อหาแบบนี้

1
2
3
4
5
6
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
    - role: control-plane
    - role: worker
    - role: worker

หลังจากนั้นเราจะสร้าง cluster โดยที่ระบุ configuration เข้่าไปแบบนี้

$ kind create cluster --config kind-config.yaml

ในตัวอย่างนี้เราไม่ได้ระบุชื่อดังนั้น kind จะใช้ชื่อ kind ที่เป็น default ถ้าเรา run คำสั่ง kind create cluster ด้านบนมาแล้วให้ลบ cluster ที่ชื่อ kind ออกก่อน(คำสั่งลบ cluster อยู่ด้านล่าง)

นอกจากนี้เรายังสามารถสร้าง cluster ที่มี High Availability(มี Control Plane หรือ Master มากกว่า 1 ตัว) ได้ง่ายๆ เพียงแค่เพิ่ม Control Plane เข้าไปอีก 2 ตัวแบบนี้

1
2
3
4
5
6
7
8
9
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
    - role: control-plane
    - role: control-plane
    - role: control-plane
    - role: worker
    - role: worker
    - role: worker

ลบ Cluster ที่ไม่ต้องการออก

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

$ kind delete cluster

ถ้าเราไม่ได้ระบุชื่อ cluster จะเป็นการลบ cluster ที่ชื่อ kind เหมือนกับตอน create cluster แต่ถ้าเราต้องการลบ cluster ที่มีชื่ออื่นให้ใส่ –name เข้าไปแบบนี้

$ kind delete cluster --name demo-cluster

อ่านต่อเพิ่มเติมได้ที่

Phanupong Permpimol
Follow me

Software Engineer ที่เชื่อในเรื่องของ Process เพราะเมื่อ Process ดี Product ก็จะดีตาม ปัจจุบันเป็นอาจารย์และที่ปรึกษาด้านการออกแบบและพัฒนา Software และ Web Security