Skip to content

คำสั่ง kubectl อธิบาย

คำสั่ง kubectl ที่ใช้ในงานประจำวัน จัดกลุ่มตามหน้าที่: อ่านข้อมูลคลัสเตอร์ ตรวจสอบเวิร์กโหลด ใช้และย้อนกลับการเปลี่ยนแปลง เปิดเผยเซอร์วิส จัดการโหนด และสร้างอ็อบเจกต์คอนฟิก แต่ละแถวจับคู่คำสั่งกับตัวอย่างการเรียกใช้ที่สมจริง

kubectl คือตัวควบคุม Kubernetes cluster ของคุณ โมเดลเป็นแบบประกาศ (declarative): โดยทั่วไปคุณจะ apply manifest (apply -f) แทนการสร้างสิ่งต่าง ๆ แบบ imperative แล้วอ่านผลลัพธ์กลับมา (get, describe, logs) คำสั่งด้านล่างครอบคลุมการใช้งานประจำวัน ให้ใช้ควบคู่กับ flag ของ namespace (-n name) เมื่อคุณไม่ได้อยู่ใน default namespace

ตารางอ้างอิง · 59 รายการ
59 of 59 rows
คลัสเตอร์และ context
แสดงว่า control plane และ service หลักทำงานอยู่ที่ไหนkubectl cluster-info
แสดงรายการ cluster และ namespace ที่คุณสลับไปมาได้kubectl config get-contexts
สลับไปยัง context ของ cluster หรือ namespace อื่นkubectl config use-context my-prod
ตั้ง default namespace ของ context ปัจจุบันkubectl config set-context --current --namespace=staging
เปลี่ยนชื่อ context ที่บันทึกไว้ใน kubeconfig ของคุณkubectl config rename-context old-name prod
แสดงรายการประเภท resource ทั้งหมดที่ cluster รู้จักkubectl api-resources
พิมพ์เวอร์ชัน Kubernetes ของ client และ serverkubectl version
แสดง field ที่ resource รองรับ อ่านมาจาก API schemakubectl explain pod.spec.containers
ตรวจสอบว่าผู้ใช้ปัจจุบันได้รับอนุญาตให้ทำ action นั้นหรือไม่kubectl auth can-i delete pods
ตรวจสอบเวิร์กโหลด
แสดงรายการ pod พร้อมสถานะ เพิ่ม -o wide เพื่อดู node และ IPkubectl get pods -A
แสดง resource ทั่วไปของ namespace ในครั้งเดียวkubectl get all
เพิ่มคอลัมน์ node และ IP ให้คำสั่ง get ใดก็ได้kubectl get pods -o wide
พิมพ์ resource ออกมาเป็น YAML manifest ฉบับเต็มที่อยู่เบื้องหลังkubectl get deploy/web -o yaml
แสดงรายการ deployment และจำนวน replica ที่พร้อมใช้kubectl get deployments
แสดงรายการ statefulset และ replica ที่พร้อมใช้kubectl get statefulsets
แสดงรายการ job แบบ batch และ cronjob ที่ตั้งเวลาไว้kubectl get jobs,cronjobs
แสดง event, label และสถานะของ pod หนึ่งตัวkubectl describe pod web-abc
แสดงความจุ เงื่อนไข และ pod ที่กำลังทำงานของ nodekubectl describe node node-1
พิมพ์ stdout ของ pod; ใช้ -f เพื่อติดตาม, --previous สำหรับการ crash ครั้งล่าสุดkubectl logs -f web-abc
รันคำสั่งภายใน pod ที่กำลังทำงานkubectl exec -it web-abc -- sh
ดู CPU และหน่วยความจำแบบสดของ pod หรือ node (ต้องมี metrics-server)kubectl top pods
แสดงรายการ event ล่าสุด เช่น pull ล้มเหลวและ backoffkubectl get events -A --sort-by=.lastTimestamp
แสดงรายการ ingress rule พร้อมที่อยู่ load balancerkubectl get ingress
คัดลอกไฟล์ระหว่างเครื่องของคุณกับ pod ที่กำลังทำงานkubectl cp web-abc:/var/log/app.log ./app.log
บล็อกรอจนกว่า resource จะตรงตามเงื่อนไขkubectl wait --for=condition=ready pod -l app=web
เริ่ม pod ใช้ครั้งเดียวจาก image โดยไม่ต้องมี manifestkubectl run debug --rm -it --image=busybox -- sh
Apply, scale และ rollout
แสดงว่า apply จะเปลี่ยนอะไรบ้าง โดยไม่เปลี่ยนจริงkubectl diff -f deploy.yaml
สร้างหรืออัปเดต resource จากไฟล์ manifestkubectl apply -f deploy.yaml
apply manifest และลบ object ที่ไม่อยู่ในนั้นแล้วkubectl apply -f dir/ --prune -l app=web
เปิด resource ที่ทำงานอยู่ใน editor แล้ว apply เมื่อบันทึกkubectl edit deploy/web
แก้ไขบาง field ของ resource ที่ทำงานอยู่kubectl patch deploy/web --patch-file fix.json
ชี้ container ของ deployment ไปที่ image ใหม่kubectl set image deploy/web web=nginx:1.27
แทนที่ object โดยลบแล้วสร้างใหม่เมื่อจำเป็นkubectl replace --force -f deploy.yaml
ลบ resource ที่ประกาศไว้ในไฟล์ manifestkubectl delete -f deploy.yaml
เปลี่ยนจำนวน replica ของ deploymentkubectl scale deploy/web --replicas=4
เฝ้าดู rollout จนเสร็จสมบูรณ์หรือล้มเหลวkubectl rollout status deploy/web
แสดงรายการ revision ที่ rollout ผ่านมาkubectl rollout history deploy/web
รีสตาร์ท pod ทุกตัวของ deployment ทีละตัวkubectl rollout restart deploy/web
ย้อน deployment กลับไป revision ก่อนหน้าkubectl rollout undo deploy/web
เซอร์วิสและเครือข่าย
แสดงรายการ service พร้อมชนิดและ cluster IPkubectl get svc
แสดงว่า pod IP ใดกำลังรองรับแต่ละ service อยู่kubectl get endpoints web
สร้าง service ที่อยู่หน้าชุด pod; --type=NodePort หรือ LoadBalancer เข้าถึงได้จากนอก clusterkubectl expose deploy/web --port=80
แสดงที่อยู่ load balancer เบื้องหลังแต่ละ ingress (alpha)kubectl ingress status -A
แสดงรายการ policy ที่จำกัดการรับส่งข้อมูลของ podkubectl get networkpolicies
ทำ tunnel จากพอร์ตในเครื่องไปยัง pod หรือ servicekubectl port-forward svc/web 8080:80
โหนดและการดูแลรักษา
แสดงรายการ node ของ cluster พร้อมสถานะkubectl get nodes -o wide
ทำเครื่องหมายว่า node ไม่รับ pod ใหม่kubectl cordon node-1
ทำเครื่องหมายให้ node รับ pod ได้กลับมาอีกครั้งkubectl uncordon node-1
ไล่ pod ออกจาก node และทำเครื่องหมายว่าไม่รับ pod ใหม่kubectl drain node-1 --ignore-daemonsets
เพิ่มหรือลบ taint ที่ทำให้ pod เลี่ยง nodekubectl taint node node-1 key=value:NoSchedule
ลบ pod ที่ค้างทันที โดยข้ามการปิดแบบ gracefulkubectl delete pod web-abc --grace-period=0 --force
อ็อบเจกต์และคอนฟิก
สร้าง namespace ใหม่เพื่อกำหนดขอบเขตของ resourcekubectl create namespace staging
แสดงรายการ namespace พร้อมสถานะkubectl get namespaces
เก็บคีย์หรือไฟล์คอนฟิกเป็น object ใน clusterkubectl create configmap app --from-file=config.yaml
แสดงรายการ configmap ใน namespacekubectl get configmaps
เก็บรหัสผ่านหรือ certificate เป็น secretkubectl create secret generic db --from-literal=password=s3cret
แสดงรายการ secret ใน namespacekubectl get secrets
เพิ่มหรือลบ label บน resourcekubectl label pod web-abc tier=backend
เพิ่ม metadata ที่ selector จับคู่ไม่ได้kubectl annotate deploy/web release=july