Cheat Sheet Kubernetes
Terakhir diperbarui
Context dan info cluster
Context adalah gabungan cluster, user, dan namespace default, yang disimpan di ~/.kube/config.
| Kegunaan | Perintah |
|---|---|
| Tampilkan versi client dan server | kubectl version |
| Tampilkan alamat cluster | kubectl cluster-info |
| Tampilkan daftar context | kubectl config get-contexts |
| Tampilkan context aktif | kubectl config current-context |
| Pindah ke context lain | kubectl config use-context prod |
| Atur namespace default untuk context ini | kubectl config set-context --current --namespace=dev |
| Tampilkan kubeconfig yang sudah digabung | kubectl config view |
| Tampilkan daftar node | kubectl get nodes |
| Tampilkan semua tipe resource dan nama singkatnya | kubectl api-resources |
| Jelaskan sebuah field dari resource | kubectl explain deployment.spec.replicas |
Menampilkan resource
kubectl get menampilkan resource dari satu tipe. Tambahkan -n untuk namespace lain.
| Kegunaan | Perintah |
|---|---|
| Tampilkan pod di namespace saat ini | kubectl get pods |
| Tampilkan pod di semua namespace | kubectl get pods -A |
| Tampilkan pod di satu namespace | kubectl get pods -n kube-system |
| Tambahkan kolom node dan IP | kubectl get pods -o wide |
| Tampilkan label | kubectl get pods --show-labels |
| Filter berdasarkan label | kubectl get pods -l app=web |
| Filter berdasarkan field | kubectl get pods --field-selector status.phase=Running |
| Terus pantau perubahan | kubectl get pods -w |
| Tampilkan deployment, service, dan pod sekaligus | kubectl get deploy,svc,pods |
| Tampilkan resource yang umum | kubectl get all |
| Tampilkan satu resource dalam YAML | kubectl get deployment web -o yaml |
| Tampilkan daftar service | kubectl get svc |
| Tampilkan daftar namespace | kubectl get ns |
| Tampilkan event terbaru, dari yang terlama | kubectl get events --sort-by=.metadata.creationTimestamp |
Describe, log, dan exec
Tempat mencari tahu saat pod tidak Running.
| Kegunaan | Perintah |
|---|---|
| Tampilkan detail dan event sebuah pod | kubectl describe pod web-7d4b9c8f6-x2k8p |
| Tampilkan detail deployment | kubectl describe deployment web |
| Tampilkan log pod | kubectl logs web-7d4b9c8f6-x2k8p |
| Ikuti log | kubectl logs -f web-7d4b9c8f6-x2k8p |
| Log container sebelum restart | kubectl logs --previous web-7d4b9c8f6-x2k8p |
| Log satu container di dalam pod | kubectl logs web-7d4b9c8f6-x2k8p -c sidecar |
| Log pod milik sebuah deployment | kubectl logs deployment/web |
| Log semua pod dengan label tertentu | kubectl logs -l app=web --tail=50 |
| Buka shell di pod | kubectl exec -it web-7d4b9c8f6-x2k8p -- sh |
| Jalankan satu perintah di pod | kubectl exec web-7d4b9c8f6-x2k8p -- env |
| Teruskan port lokal 8080 ke port 80 pod | kubectl port-forward pod/web-7d4b9c8f6-x2k8p 8080:80 |
| Teruskan ke service | kubectl port-forward svc/web 8080:80 |
| Salin file dari pod | kubectl cp web-7d4b9c8f6-x2k8p:/etc/nginx/nginx.conf ./nginx.conf |
| Tampilkan pemakaian CPU dan memori (butuh metrics-server) | kubectl top pods |
| Jalankan pod sementara dengan shell | kubectl run tmp --rm -it --image=busybox -- sh |
Membuat dan menerapkan
apply membuat resource atau memperbaruinya agar sesuai dengan file; inilah perintah untuk manifest yang disimpan di Git.
| Kegunaan | Perintah |
|---|---|
| Buat atau perbarui dari file | kubectl apply -f deployment.yaml |
| Terapkan semua file di folder | kubectl apply -f k8s/ |
| Terapkan folder kustomization | kubectl apply -k overlays/prod |
| Tampilkan apa yang akan diubah oleh apply | kubectl diff -f deployment.yaml |
| Buat deployment | kubectl create deployment web --image=nginx --replicas=3 |
| Jalankan satu pod | kubectl run web --image=nginx |
| Expose deployment sebagai service | kubectl expose deployment web --port=80 --target-port=8080 |
| Expose di port setiap node | kubectl expose deployment web --type=NodePort --port=80 |
| Buat ConfigMap dari nilai | kubectl create configmap app-config --from-literal=LOG_LEVEL=debug |
| Buat Secret dari nilai | kubectl create secret generic db-pass --from-literal=password=s3cret |
| Buat ConfigMap dari file | kubectl create configmap nginx-conf --from-file=nginx.conf |
| Tulis YAML untuk deployment tanpa membuatnya | kubectl create deployment web --image=nginx --dry-run=client -o yaml > web.yaml |
| Jalankan Job sekali | kubectl create job hello --image=busybox -- echo hello |
| Edit resource yang sedang aktif di editormu | kubectl edit deployment web |
Deployment dan rollout
| Kegunaan | Perintah |
|---|---|
| Scale ke 5 replica | kubectl scale deployment web --replicas=5 |
| Ganti image (memulai rollout) | kubectl set image deployment/web nginx=nginx:1.27 |
| Pantau rollout sampai selesai | kubectl rollout status deployment/web |
| Tampilkan riwayat rollout | kubectl rollout history deployment/web |
| Kembali ke versi sebelumnya | kubectl rollout undo deployment/web |
| Kembali ke revisi tertentu | kubectl rollout undo deployment/web --to-revision=2 |
| Restart semua pod sebuah deployment | kubectl rollout restart deployment/web |
| Jeda rollout | kubectl rollout pause deployment/web |
| Lanjutkan rollout | kubectl rollout resume deployment/web |
| Autoscale berdasarkan pemakaian CPU | kubectl autoscale deployment web --min=2 --max=10 --cpu-percent=70 |
| Atur environment variable | kubectl set env deployment/web LOG_LEVEL=debug |
| Atur request CPU dan memori | kubectl set resources deployment web --requests=cpu=100m,memory=128Mi |
Menghapus
Deployment akan membuat ulang pod apa pun yang kamu hapus; hapus deployment-nya untuk menghilangkan pod-nya selamanya.
| Kegunaan | Perintah |
|---|---|
| Hapus apa yang dibuat oleh sebuah file | kubectl delete -f deployment.yaml |
| Hapus pod | kubectl delete pod web-7d4b9c8f6-x2k8p |
| Hapus deployment beserta pod-nya | kubectl delete deployment web |
| Hapus pod berdasarkan label | kubectl delete pods -l app=web |
| Hapus pod seketika, tanpa menunggu | kubectl delete pod web-7d4b9c8f6-x2k8p --grace-period=0 --force |
| Hapus namespace beserta seluruh isinya | kubectl delete namespace dev |
Namespace
| Kegunaan | Perintah |
|---|---|
| Tampilkan daftar namespace | kubectl get namespaces |
| Buat namespace | kubectl create namespace dev |
| Jalankan perintah apa pun di sebuah namespace | kubectl get pods -n dev |
| Jadikan default untuk context ini | kubectl config set-context --current --namespace=dev |
| Tampilkan namespace dari context aktif | kubectl config view --minify -o jsonpath='{..namespace}' |
Label dan annotation
| Kegunaan | Perintah |
|---|---|
| Tambahkan label | kubectl label pods web-7d4b9c8f6-x2k8p tier=frontend |
| Ubah label | kubectl label pods web-7d4b9c8f6-x2k8p tier=backend --overwrite |
| Hapus label | kubectl label pods web-7d4b9c8f6-x2k8p tier- |
| Tambahkan annotation | kubectl annotate deployment web owner=team-a |
| Patch sebuah field | kubectl patch deployment web -p '{"spec":{"replicas":2}}' |
Node
| Kegunaan | Perintah |
|---|---|
| Tampilkan node beserta IP dan OS-nya | kubectl get nodes -o wide |
| Tampilkan kapasitas, kondisi, dan pod sebuah node | kubectl describe node worker-1 |
| Cegah pod baru ditempatkan di node | kubectl cordon worker-1 |
| Pindahkan pod dari node sebelum maintenance | kubectl drain worker-1 --ignore-daemonsets --delete-emptydir-data |
| Izinkan pod di node itu lagi | kubectl uncordon worker-1 |
| Tambahkan taint | kubectl taint nodes worker-1 gpu=true:NoSchedule |
| Hapus taint | kubectl taint nodes worker-1 gpu=true:NoSchedule- |
| Tampilkan pemakaian CPU dan memori node (butuh metrics-server) | kubectl top nodes |
Format output
Tambahkan -o ke get mana pun untuk mengubah apa yang dicetaknya.
| Kegunaan | Perintah |
|---|---|
| Objek lengkap dalam YAML | kubectl get pod web-7d4b9c8f6-x2k8p -o yaml |
| Objek lengkap dalam JSON | kubectl get pod web-7d4b9c8f6-x2k8p -o json |
| Nama saja (type/name) | kubectl get pods -o name |
| Pilih field dengan JSONPath | kubectl get pods -o jsonpath='{.items[*].metadata.name}' |
| Kolom pilihanmu sendiri | kubectl get pods -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeName |
| Urutkan berdasarkan field | kubectl get pods --sort-by=.status.startTime |
| Decode nilai Secret | kubectl get secret db-pass -o jsonpath='{.data.password}' | base64 -d |
Nama singkat
Sebagian besar tipe resource punya nama singkat yang bisa kamu ketik sebagai gantinya.
| Resource | Nama singkat |
|---|---|
| Pods | po |
| Deployments | deploy |
| ReplicaSets | rs |
| StatefulSets | sts |
| DaemonSets | ds |
| Services | svc |
| Ingresses | ing |
| ConfigMaps | cm |
| Namespaces | ns |
| Nodes | no |
| PersistentVolumeClaims | pvc |
| PersistentVolumes | pv |
| ServiceAccounts | sa |
| HorizontalPodAutoscalers | hpa |
| CronJobs | cj |
Semua perintah kubectl yang sering kamu pakai saat bekerja dengan Kubernetes (k8s), dalam tabel siap salin: context dan namespace lebih dulu, lalu menampilkan dan menjelaskan resource, log dan exec, menerapkan manifest, deployment dan rollout, scaling, label, node, dan format output.
kubectl berbicara dengan cluster yang ditunjuk oleh context aktifmu, jadi periksa kubectl config current-context sebelum menghapus apa pun. Untuk mencoba perintahnya tanpa cluster, buka playground terminal, yang sudah dilengkapi simulator Kubernetes satu node.
FAQ cheat sheet kubectl
Apa itu kubectl?
kubectl get pods menampilkan daftar pod, kubectl apply -f membuat atau memperbarui resource dari YAML, sedangkan kubectl logs dan kubectl exec melihat ke dalam container yang sedang berjalan. Cluster mana yang diajak bicara ditentukan oleh context aktif di ~/.kube/config.Apakah k8s dan Kubernetes itu sama?
Ya. k8s adalah singkatan dari Kubernetes: huruf k, 8 huruf di tengah, lalu huruf s. Cheat sheet k8s sama dengan cheat sheet kubectl, karena kubectl adalah cara kamu bekerja dengan cluster.
Apa bedanya kubectl apply dan kubectl create?
kubectl create membuat resource dan gagal kalau resource itu sudah ada. kubectl apply membuatnya atau memperbaruinya agar sesuai dengan filemu, jadi kamu bisa menjalankannya lagi setiap kali ada perubahan. Pakai apply untuk manifest yang kamu simpan di Git, dan create untuk resource cepat sekali pakai.Bagaimana cara melihat kenapa pod tidak mau jalan?
kubectl describe pod <name> dan baca bagian Events di bawah: error saat pull image, penjadwalan yang gagal, dan probe yang gagal muncul di sana. Kalau container sempat berjalan lalu crash, kubectl logs --previous <name> menampilkan output dari run yang crash itu.Bagaimana cara pindah namespace di kubectl?
-n dev ke satu perintah, atau jadikan dev default untuk context aktifmu dengan kubectl config set-context --current --namespace=dev.Bagaimana cara restart deployment?
kubectl rollout restart deployment/web mengganti setiap pod dengan pod baru, beberapa sekaligus, tanpa downtime kalau replica-nya lebih dari satu. kubectl rollout status deployment/web menunggu sampai prosesnya selesai.Bisakah saya berlatih perintah kubectl secara online?
kubectl get, describe, apply, logs, exec, deployment, rollout, dan service merespons seperti di cluster sungguhan, tanpa perlu instalasi. Beberapa perintah, seperti port-forward dan edit, butuh cluster sungguhan seperti kind atau minikube.Apakah cheat sheet kubectl ini gratis?
Ya. Gratis untuk dibaca, disalin, dan dicetak, tanpa perlu daftar.