Skip to main content

Kubectl Commands

kubectl เป็นเครื่องมือ CLI สำหรับการจัดการ Kubernetes คลัสเตอร์ คุณสามารถใช้ kubectl เพื่อสร้าง อัปเดต ลบ และดูทรัพยากรใน Kubernetes ได้อย่างง่ายดาย

คำสั่งพื้นฐานของ kubectl

1. คำสั่งสำหรับการตรวจสอบคลัสเตอร์

  • ตรวจสอบ Nodes ในคลัสเตอร์

    kubectl get nodes
  • ตรวจสอบ Pods ใน Namespace ปัจจุบัน

    kubectl get pods
  • ตรวจสอบข้อมูลใน Namespace อื่น

    kubectl get pods -n <namespace>
  • ตรวจสอบ Services ทั้งหมด

    kubectl get services
  • ตรวจสอบรายละเอียดของ Resource ใดๆ

    kubectl describe <resource> <name>

    ตัวอย่าง:

    kubectl describe pod my-pod

2. การจัดการทรัพยากร

  • สร้าง Resource จากไฟล์ YAML/JSON

    kubectl apply -f <filename>
  • ลบ Resource

    kubectl delete <resource> <name>

    ตัวอย่าง:

    kubectl delete pod my-pod
  • อัปเดต Resource

    kubectl apply -f <filename>
  • รีสตาร์ท Deployment

    kubectl rollout restart deployment <deployment-name>

3. การดู Logs และ Debugging

  • ดู Logs ของ Pod

    kubectl logs <pod-name>
  • ดู Logs ของ Container ใน Pod ที่ระบุ

    kubectl logs <pod-name> -c <container-name>
  • เข้าสู่ Pod เพื่อ Debug

    kubectl exec -it <pod-name> -- /bin/bash

4. การจัดการ Namespace

  • ดู Namespace ทั้งหมด

    kubectl get namespaces
  • สร้าง Namespace ใหม่

    kubectl create namespace <namespace-name>
  • ลบ Namespace

    kubectl delete namespace <namespace-name>

5. การปรับแต่ง Output

  • แสดงผลในรูปแบบ Wide (รายละเอียดเพิ่มเติม)

    kubectl get pods -o wide
  • แสดงผลในรูปแบบ YAML/JSON

    kubectl get <resource> <name> -o yaml
    kubectl get <resource> <name> -o json

6. การทำงานกับ Context

  • ดู Context ปัจจุบัน

    kubectl config current-context
  • ดู Context ทั้งหมด

    kubectl config get-contexts
  • สลับ Context

    kubectl config use-context <context-name>

7. การจัดการ Label และ Annotation

  • เพิ่ม Label ให้ Resource

    kubectl label <resource> <name> <key>=<value>
  • ลบ Label จาก Resource

    kubectl label <resource> <name> <key>-
  • เพิ่ม Annotation ให้ Resource

    kubectl annotate <resource> <name> <key>=<value>

kubectl เป็นเครื่องมือที่ทรงพลังสำหรับการจัดการ Kubernetes คลัสเตอร์ การฝึกใช้คำสั่งเหล่านี้จะช่วยให้คุณสามารถจัดการและตรวจสอบระบบได้อย่างมีประสิทธิภาพ