Dasar kubectl
Info Cluster
kubectl cluster-info kubectl get nodes kubectl config current-context kubectl config use-context my-cluster
Perintah Utama
kubectl get <resource>Tampilkan daftar resource
kubectl describe <resource> <name>Info detail resource
kubectl create -f file.yamlBuat resource dari file
kubectl apply -f file.yamlBuat atau perbarui resource
kubectl delete -f file.yamlHapus resource dari file
kubectl edit <resource> <name>Edit resource di tempat
kubectl api-resourcesTampilkan semua tipe resource
Format Output
-o wideKolom tambahan (IP, node)
-o yamlOutput YAML lengkap
-o jsonOutput JSON lengkap
-o jsonpath='{.spec}'Ekstrak field tertentu
--sort-by=.metadata.nameUrutkan output berdasarkan field
Pod
Operasi Pod
kubectl get pods kubectl get pods -A # all namespaces kubectl run nginx --image=nginx # quick pod kubectl delete pod nginx
YAML Pod
apiVersion: v1 kind: Pod metadata: name: myapp labels: { app: myapp } spec: containers: - name: app image: nginx:1.27 ports: - containerPort: 80
Nilai Status Pod
RunningSemua container sudah berjalan
PendingMenunggu penjadwalan atau pull image
CrashLoopBackOffContainer terus crash dan restart
ImagePullBackOffGagal mengunduh image container
CompletedSelesai berjalan (Jobs)
Deployment
YAML Deployment
apiVersion: apps/v1 kind: Deployment metadata: name: web spec: replicas: 3 selector: matchLabels: { app: web } template: metadata: labels: { app: web } spec: containers: - name: web image: nginx:1.27 ports: - containerPort: 80
Perintah Deployment
kubectl get deployTampilkan deployment
kubectl scale deploy web --replicas=5Skala replica
kubectl set image deploy/web web=nginx:1.28Update image (rolling)
kubectl rollout status deploy/webPantau progres rollout
kubectl rollout undo deploy/webRollback ke revisi sebelumnya
kubectl rollout history deploy/webLihat riwayat revisi
Service
Jenis Service
ClusterIPInternal saja (default)
NodePortEkspos di IP setiap node pada port statis
LoadBalancerLoad balancer eksternal (cloud)
ExternalNameAlias DNS ke layanan eksternal
YAML Service
apiVersion: v1 kind: Service metadata: name: web-svc spec: type: ClusterIP selector: { app: web } ports: - port: 80 targetPort: 80
Ekspos Cepat
kubectl expose deploy web --port=80 --type=ClusterIP kubectl expose deploy web --port=80 --type=NodePort kubectl get svc
ConfigMap & Secret
ConfigMap
kubectl create configmap app-cfg \ --from-literal=DB_HOST=db.example.com \ --from-file=config.ini
Secret
kubectl create secret generic db-creds \ --from-literal=username=admin \ --from-literal=password=s3cret
Penggunaan dalam Pod
# As environment variables envFrom: - configMapRef: { name: app-cfg } - secretRef: { name: db-creds } # As volume mount volumes: - name: cfg configMap: { name: app-cfg }
Perintah
kubectl get cmTampilkan ConfigMap
kubectl get secretTampilkan Secret
kubectl describe cm app-cfgTampilkan data ConfigMap
kubectl get secret db-creds -o yamlTampilkan Secret (base64-encoded)
Namespace
Perintah Namespace
kubectl get nsTampilkan namespace
kubectl create ns stagingBuat namespace
kubectl delete ns stagingHapus namespace dan semua resource
kubectl get pods -n stagingTampilkan pod di namespace
kubectl get pods -ATampilkan pod di semua namespace
Atur Namespace Default
kubectl config set-context --current \ --namespace=staging
Volume
PersistentVolumeClaim
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: data-pvc spec: accessModes: [ReadWriteOnce] resources: requests: { storage: 10Gi }
Mount dalam Pod
volumes: - name: data persistentVolumeClaim: claimName: data-pvc containers: - volumeMounts: - name: data mountPath: /app/data
Jenis Volume
emptyDirDirektori sementara, dihapus bersama pod
hostPathMount path filesystem host
persistentVolumeClaimPenyimpanan persisten (PVC)
configMapMount ConfigMap sebagai file
secretMount Secret sebagai file
Ingress
YAML Ingress
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: web-ingress spec: rules: - host: app.example.com http: paths: - path: / pathType: Prefix backend: service: name: web-svc port: { number: 80 }
Catatan Ingress
Ingress ControllerWajib ada (nginx-ingress, traefik, dll.)
pathType: PrefixCocokkan awalan URL
pathType: ExactCocokkan path URL tepat
TLSTambahkan bagian tls: dengan nama secret
Debugging
Perintah Diagnostik
kubectl logs <pod>stdout/stderr container
kubectl logs <pod> -c <ctr>Log container tertentu
kubectl logs <pod> --previousLog dari container yang crash
kubectl describe pod <pod>Event, kondisi, status
kubectl exec -it <pod> -- shMasuk shell ke container
kubectl port-forward <pod> 8080:80Forward port lokal ke pod
kubectl top podsPenggunaan CPU/memori (metrics-server)
kubectl get events --sort-by=.lastTimestampTimeline event cluster
Pod Debug
kubectl run debug --rm -it --image=busybox -- sh # or attach ephemeral container kubectl debug -it --image=busybox
Pola Umum
Label & Selector
kubectl get pods -l app=web kubectl get pods -l 'env in (prod,staging)' kubectl label pod myapp env=prod
Batas Resource
resources: requests: { cpu: 100m, memory: 128Mi } limits: { cpu: 500m, memory: 256Mi }
Liveness & Readiness
livenessProbe: httpGet: { path: /healthz, port: 8080 } initialDelaySeconds: 5 periodSeconds: 10 readinessProbe: httpGet: { path: /ready, port: 8080 }
Resep Cepat
Dry runkubectl apply -f file.yaml --dry-run=client
Generate YAMLkubectl create deploy web --image=nginx --dry-run=client -o yaml
Watchkubectl get pods -w
Copy filekubectl cp file.txt pod:/tmp/
Restart deploykubectl rollout restart deploy/web