Skip to content

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

Terminal window
# แสดง pods ทั้งหมดใน namespace ปัจจุบัน
kubectl get pods
# แสดง pods พร้อม detail เพิ่มเติม
kubectl get pods -o wide
# แสดง pods ทั้งหมดในทุก namespace
kubectl get pods --all-namespaces
# ดูข้อมูล detail ของ pod
kubectl describe pod <pod-name>
# ดู logs ของ pod
kubectl logs <pod-name>
# ดู logs แบบ follow
kubectl logs -f <pod-name>
# exec เข้าไปใน container
kubectl exec -it <pod-name> -- /bin/sh
# ลบ pod
kubectl delete pod <pod-name>

Deployment คือ abstraction layer ที่อยู่บน Pod ช่วยให้จัดการ scaling, rolling updates, และ rollback ได้ง่ายขึ้น

Terminal window
# แสดง deployments ทั้งหมด
kubectl get deployments
# ดูข้อมูล detail ของ deployment
kubectl describe deployment <deployment-name>
# สร้าง deployment จาก yaml file
kubectl apply -f <deployment.yaml>
# ดู rollout status
kubectl rollout status deployment/<deployment-name>
# rollback deployment กลับไป version ก่อนหน้า
kubectl rollout undo deployment/<deployment-name>
# ดู history ของ rollout
kubectl rollout history deployment/<deployment-name>
Terminal window
# scale deployment ไปที่จำนวน replicas ที่ต้องการ
kubectl scale deployment <deployment-name> --replicas=3
# scale แบบ horizontal pod autoscaler
kubectl autoscale deployment <deployment-name> --min=2 --max=10 --cpu-percent=80
Terminal window
# แสดง replicasets ทั้งหมด
kubectl get replicasets
# ดูข้อมูล detail ของ replicaset
kubectl describe replicaset <replicaset-name>

Service คือ abstraction ที่ defines logical set of pods และ policy สำหรับเข้าถึง pods นั้นๆ

Terminal window
# แสดง services ทั้งหมด
kubectl get services
kubectl get svc
# ดูข้อมูล detail ของ service
kubectl describe service <service-name>
# สร้าง service แบบ LoadBalancer
kubectl expose deployment <deployment-name> --port=80 --type=LoadBalancer
# สร้าง service แบบ ClusterIP (internal)
kubectl expose deployment <deployment-name> --port=80 --type=ClusterIP
# สร้าง service แบบ NodePort
kubectl expose deployment <deployment-name> --port=80 --type=NodePort
Terminal window
# forward port จาก local ไปยัง pod
kubectl port-forward pod/<pod-name> 8080:80
# forward port จาก local ไปยัง service
kubectl port-forward svc/<service-name> 8080:80

ConfigMap ใช้เก็บ configuration ที่ไม่เป็น secret เช่น environment variables, command-line arguments, config files

Terminal window
# สร้าง configmap จาก literal
kubectl create configmap <configmap-name> --from-literal=key=value
# สร้าง configmap จาก file
kubectl create configmap <configmap-name> --from-file=<file-path>
# สร้าง configmap จาก env file
kubectl create configmap <configmap-name> --from-env-file=<env-file-path>
# ดู configmap
kubectl get configmaps
kubectl describe configmap <configmap-name>

Secret ใช้เก็บข้อมูลที่ sensitive เช่น passwords, tokens, keys

Terminal window
# สร้าง secret จาก literal
kubectl create secret generic <secret-name> --from-literal=key=value
# สร้าง secret จาก file
kubectl create secret generic <secret-name> --from-file=<file-path>
# สร้าง secret แบบ docker registry
kubectl create secret docker-registry <secret-name> --docker-server=<server> --docker-username=<user> --docker-password=<pass>
# ดู secrets
kubectl get secrets
# ดู secret value (base64 decode)
kubectl get secret <secret-name> -o jsonpath='{.data.key}' | base64 -d

Namespace ช่วยให้แบ่ง cluster ออกเป็นหลาย virtual clusters

Terminal window
# แสดง namespaces ทั้งหมด
kubectl get namespaces
kubectl get ns
# สร้าง namespace
kubectl create namespace <namespace-name>
# เปลี่ยน namespace ปัจจุบัน
kubectl config set-context --current --namespace=<namespace-name>
# แสดง resources ใน namespace ที่ระบุ
kubectl get pods -n <namespace-name>
Terminal window
# แสดง ingresses ทั้งหมด
kubectl get ingress
# ดู ingress detail
kubectl describe ingress <ingress-name>
# apply ingress from yaml
kubectl apply -f <ingress.yaml>
Terminal window
# แสดง nodes ทั้งหมด
kubectl get nodes
# ดูข้อมูล detail ของ node
kubectl describe node <node-name>
# แสดง node resource usage
kubectl top nodes
Terminal window
# แสดง events ทั้งหมด sort by time
kubectl get events --sort-by='.lastTimestamp'
# แสดง events ใน namespace ที่ระบุ
kubectl get events -n <namespace-name>
# แสดง events แบบ watch
kubectl get events --watch
Terminal window
# แสดง pods ที่มีปัญหา
kubectl get pods | grep -v Running
# ดู logs ของ pod ที่มีหลาย containers
kubectl 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 เพื่อ debug
kubectl exec -it <pod-name> -c <container-name> -- /bin/sh
# ตรวจสอบ endpoint
kubectl get endpoints <service-name>
Terminal window
# แสดง resources ทั้งหมดใน cluster
kubectl api-resources
# แสดง resources ที่มีใน namespace
kubectl api-resources --namespaced=true
# แสดง resources ที่ไม่มีใน namespace (cluster-wide)
kubectl api-resources --namespaced=false
# แสดง API version ของ resource
kubectl api-resources -o wide
Terminal window
# แสดง yaml ของ resource (ไม่ apply)
kubectl get pod <pod-name> -o yaml
# สร้าง resource จาก yaml
kubectl apply -f <file.yaml>
# สร้างหลาย resources จาก directory
kubectl apply -f <directory>/
# ลบ resource จาก yaml
kubectl delete -f <file.yaml>
# ดู difference ก่อน apply
kubectl diff -f <file.yaml>
Terminal window
# แสดง contexts ทั้งหมด
kubectl config get-contexts
# เปลี่ยน context
kubectl config use-context <context-name>
# แสดง current context
kubectl config current-context
# แสดง clusters ทั้งหมด
kubectl config get-clusters
# ดู config ทั้งหมด
kubectl config view
Terminal window
# -n : ระบุ namespace
kubectl get pods -n <namespace>
# -o : กำหนด output format (yaml, json, wide)
kubectl get pods -o yaml
kubectl get pods -o json
kubectl get pods -o wide
# -l : filter ด้วย label
kubectl get pods -l app=myapp
# -f : ระบุ filename
kubectl apply -f deployment.yaml
# --dry-run : ทดสอบคำสั่งโดยไม่ apply
kubectl apply -f deployment.yaml --dry-run
# -w : watch mode
kubectl get pods -w
คำสั่งคำอธิบาย
kubectl get podsแสดง pods ทั้งหมด
kubectl get svcแสดง services ทั้งหมด
kubectl get nsแสดง namespaces ทั้งหมด
kubectl get allแสดง resources หลักทั้งหมด
kubectl apply -fapply yaml file
kubectl delete -fdelete จาก yaml file
kubectl describeดู detail ของ resource
kubectl logsดู logs ของ pod
kubectl execexec เข้า container
kubectl port-forwardforward port
kubectl scalescale deployment
kubectl rolloutmanage rollout
kubectl topดู resource usage
kubectl configจัดการ kubeconfig
kubectl api-resourcesแสดง resource types ทั้งหมด