Kubernetes
import ChatMessage from ’../../../components/ChatMessage.astro’;
ระบบจัดการ container orchestration ที่ช่วยให้จัดการ container หลายตัวใน cluster ได้อย่างมีประสิทธิภาพ รองรับการ scale, deploy, และ manage applications อัตโนมัติ
Pod คือ smallest deployable unit ใน Kubernetes โดยแต่ละ Pod จะ contain หนึ่งหรือหลาย containers ที่ใช้ shared storage และ network
# แสดง pods ทั้งหมดใน namespace ปัจจุบันkubectl get pods
# แสดง pods พร้อม detail เพิ่มเติมkubectl get pods -o wide
# แสดง pods ทั้งหมดในทุก namespacekubectl get pods --all-namespaces
# ดูข้อมูล detail ของ podkubectl describe pod <pod-name>
# ดู logs ของ podkubectl logs <pod-name>
# ดู logs แบบ followkubectl logs -f <pod-name>
# exec เข้าไปใน containerkubectl exec -it <pod-name> -- /bin/sh
# ลบ podkubectl delete pod <pod-name>Deployment
Section titled “Deployment”Deployment คือ abstraction layer ที่อยู่บน Pod ช่วยให้จัดการ scaling, rolling updates, และ rollback ได้ง่ายขึ้น
# แสดง deployments ทั้งหมดkubectl get deployments
# ดูข้อมูล detail ของ deploymentkubectl describe deployment <deployment-name>
# สร้าง deployment จาก yaml filekubectl apply -f <deployment.yaml>
# ดู rollout statuskubectl rollout status deployment/<deployment-name>
# rollback deployment กลับไป version ก่อนหน้าkubectl rollout undo deployment/<deployment-name>
# ดู history ของ rolloutkubectl rollout history deployment/<deployment-name>Scaling
Section titled “Scaling”# scale deployment ไปที่จำนวน replicas ที่ต้องการkubectl scale deployment <deployment-name> --replicas=3
# scale แบบ horizontal pod autoscalerkubectl autoscale deployment <deployment-name> --min=2 --max=10 --cpu-percent=80ReplicaSet
Section titled “ReplicaSet”# แสดง replicasets ทั้งหมดkubectl get replicasets
# ดูข้อมูล detail ของ replicasetkubectl describe replicaset <replicaset-name>Services
Section titled “Services”Service คือ abstraction ที่ defines logical set of pods และ policy สำหรับเข้าถึง pods นั้นๆ
# แสดง services ทั้งหมดkubectl get serviceskubectl get svc
# ดูข้อมูล detail ของ servicekubectl describe service <service-name>
# สร้าง service แบบ LoadBalancerkubectl expose deployment <deployment-name> --port=80 --type=LoadBalancer
# สร้าง service แบบ ClusterIP (internal)kubectl expose deployment <deployment-name> --port=80 --type=ClusterIP
# สร้าง service แบบ NodePortkubectl expose deployment <deployment-name> --port=80 --type=NodePortPort Forwarding
Section titled “Port Forwarding”# forward port จาก local ไปยัง podkubectl port-forward pod/<pod-name> 8080:80
# forward port จาก local ไปยัง servicekubectl port-forward svc/<service-name> 8080:80ConfigMaps
Section titled “ConfigMaps”ConfigMap ใช้เก็บ configuration ที่ไม่เป็น secret เช่น environment variables, command-line arguments, config files
# สร้าง configmap จาก literalkubectl create configmap <configmap-name> --from-literal=key=value
# สร้าง configmap จาก filekubectl create configmap <configmap-name> --from-file=<file-path>
# สร้าง configmap จาก env filekubectl create configmap <configmap-name> --from-env-file=<env-file-path>
# ดู configmapkubectl get configmapskubectl describe configmap <configmap-name>Secrets
Section titled “Secrets”Secret ใช้เก็บข้อมูลที่ sensitive เช่น passwords, tokens, keys
# สร้าง secret จาก literalkubectl create secret generic <secret-name> --from-literal=key=value
# สร้าง secret จาก filekubectl create secret generic <secret-name> --from-file=<file-path>
# สร้าง secret แบบ docker registrykubectl create secret docker-registry <secret-name> --docker-server=<server> --docker-username=<user> --docker-password=<pass>
# ดู secretskubectl get secrets
# ดู secret value (base64 decode)kubectl get secret <secret-name> -o jsonpath='{.data.key}' | base64 -dNamespaces
Section titled “Namespaces”Namespace ช่วยให้แบ่ง cluster ออกเป็นหลาย virtual clusters
# แสดง namespaces ทั้งหมดkubectl get namespaceskubectl get ns
# สร้าง namespacekubectl create namespace <namespace-name>
# เปลี่ยน namespace ปัจจุบันkubectl config set-context --current --namespace=<namespace-name>
# แสดง resources ใน namespace ที่ระบุkubectl get pods -n <namespace-name>Ingress
Section titled “Ingress”# แสดง ingresses ทั้งหมดkubectl get ingress
# ดู ingress detailkubectl describe ingress <ingress-name>
# apply ingress from yamlkubectl apply -f <ingress.yaml># แสดง nodes ทั้งหมดkubectl get nodes
# ดูข้อมูล detail ของ nodekubectl describe node <node-name>
# แสดง node resource usagekubectl top nodesEvents
Section titled “Events”# แสดง events ทั้งหมด sort by timekubectl get events --sort-by='.lastTimestamp'
# แสดง events ใน namespace ที่ระบุkubectl get events -n <namespace-name>
# แสดง events แบบ watchkubectl get events --watchLogging & Debugging
Section titled “Logging & Debugging”# แสดง pods ที่มีปัญหาkubectl get pods | grep -v Running
# ดู logs ของ pod ที่มีหลาย containerskubectl logs <pod-name> -c <container-name>
# ดู logs จาก previous container instance (ถ้า container restart แล้ว)kubectl logs <pod-name> --previous
# describe pod ดูสาเหตุkubectl describe pod <pod-name>
# เข้าไปใน container เพื่อ debugkubectl exec -it <pod-name> -c <container-name> -- /bin/sh
# ตรวจสอบ endpointkubectl get endpoints <service-name>Resources
Section titled “Resources”# แสดง resources ทั้งหมดใน clusterkubectl api-resources
# แสดง resources ที่มีใน namespacekubectl api-resources --namespaced=true
# แสดง resources ที่ไม่มีใน namespace (cluster-wide)kubectl api-resources --namespaced=false
# แสดง API version ของ resourcekubectl api-resources -o wideYAML Operations
Section titled “YAML Operations”# แสดง yaml ของ resource (ไม่ apply)kubectl get pod <pod-name> -o yaml
# สร้าง resource จาก yamlkubectl apply -f <file.yaml>
# สร้างหลาย resources จาก directorykubectl apply -f <directory>/
# ลบ resource จาก yamlkubectl delete -f <file.yaml>
# ดู difference ก่อน applykubectl diff -f <file.yaml>Context & Configuration
Section titled “Context & Configuration”# แสดง contexts ทั้งหมดkubectl config get-contexts
# เปลี่ยน contextkubectl config use-context <context-name>
# แสดง current contextkubectl config current-context
# แสดง clusters ทั้งหมดkubectl config get-clusters
# ดู config ทั้งหมดkubectl config viewCommon Options
Section titled “Common Options”# -n : ระบุ namespacekubectl get pods -n <namespace>
# -o : กำหนด output format (yaml, json, wide)kubectl get pods -o yamlkubectl get pods -o jsonkubectl get pods -o wide
# -l : filter ด้วย labelkubectl get pods -l app=myapp
# -f : ระบุ filenamekubectl apply -f deployment.yaml
# --dry-run : ทดสอบคำสั่งโดยไม่ applykubectl apply -f deployment.yaml --dry-run
# -w : watch modekubectl get pods -wCheat Sheet Quick Reference
Section titled “Cheat Sheet Quick Reference”| คำสั่ง | คำอธิบาย |
|---|---|
kubectl get pods | แสดง pods ทั้งหมด |
kubectl get svc | แสดง services ทั้งหมด |
kubectl get ns | แสดง namespaces ทั้งหมด |
kubectl get all | แสดง resources หลักทั้งหมด |
kubectl apply -f | apply yaml file |
kubectl delete -f | delete จาก yaml file |
kubectl describe | ดู detail ของ resource |
kubectl logs | ดู logs ของ pod |
kubectl exec | exec เข้า container |
kubectl port-forward | forward port |
kubectl scale | scale deployment |
kubectl rollout | manage rollout |
kubectl top | ดู resource usage |
kubectl config | จัดการ kubeconfig |
kubectl api-resources | แสดง resource types ทั้งหมด |