CodeBlanks Logo Icon CodeBlanks

kubectl exec

kubectl exec ile Pod içinde komut çalıştırmayı öğrenirsin.

kubectl exec

Log yetmezse container'ın içine bakılır: ayar dosyası doğru mu, ortam değişkenleri gelmiş mi, servis adı çözülüyor mu?

kubectl exec -it deploy/api -- sh
$ printenv | grep DB_
$ cat /etc/resolv.conf
$ wget -qO- http://db:5432 || echo "bağlantı yok"
kubectl exec deploy/api -- ls -l /etc/ayar

Küçük (distroless, scratch) image'larda kabuk bulunmayabilir; bu durumda exec çalışmaz.

kubectl debug

kubectl debug, çalışan pod'a araçlarla dolu geçici (ephemeral) bir container ekler; --target ile hedef container'ın süreçlerini görür. Ayrıca pod'un bir kopyasını ya da node'un kendisini incelemek için de kullanılır:

kubectl debug -it api-xyz --image=busybox:1.36 --target=api
kubectl debug api-xyz -it --copy-to=api-kopya --container=api -- sh
kubectl debug node/worker-1 -it --image=ubuntu:24.04

Uygulama İçeriden bak

Üç komut yaz: deploy/api'de printenv DB_HOST çalıştır; deploy/api'de cat /etc/resolv.conf çalıştır; api-xyz pod'una busybox:1.36 image'lı, api container'ını hedefleyen etkileşimli bir hata ayıklama container'ı ekle.

Başlangıç kodu

terminal
# buraya yaz

Örnek çözüm

terminal
kubectl exec deploy/api -- printenv DB_HOST
kubectl exec deploy/api -- cat /etc/resolv.conf
kubectl debug -it api-xyz --image=busybox:1.36 --target=api

Beklenen çıktı

db
search magaza.svc.cluster.local svc.cluster.local cluster.local
nameserver 10.96.0.10
Targeting container "api". If you don't see processes from this container it may be because the container runtime doesn't support this feature.
/ #

Soru ve cevaplar

Dersin sorularını cevaplarıyla birlikte inceleyebilirsin. Kendini denemek istersen dersi interaktif olarak çöz.

1 Kabuğu olmayan bir image'da hata ayıklamak için hangisi kullanılır?

  • kubectl debug ile geçici bir hata ayıklama container'ı eklemek Doğru cevap
  • kubectl exec -it -- sh
  • Image'ı silmek
  • kubectl logs

Açıklama: kubectl debug pod'a araçlı bir ephemeral container ekler.

2 Pod'a hata ayıklama container'ı ekle.

Soru
kubectl ____ -it api-xyz --image=busybox:1.36 --target=api
Cevap
kubectl debug -it api-xyz --image=busybox:1.36 --target=api

Açıklama: debug ephemeral container ekler.

3 Eşleştir.

Kavram Anlamı
exec -it ... -- sh Container'da kabuk
debug --target Araçlı geçici container
--copy-to Pod kopyasında incele
debug node/ Node'u incele

Orta seviye bölümündeki tüm dersler