kubectl — גיליון לפי משימה
namespace ו-labels: -n, -A, -l
Section titled “namespace ו-labels: -n, -A, -l”kubectl get nskubectl -n <NS> get podskubectl get pods -A # all namespaceskubectl -n <NS> get pods -l app.kubernetes.io/part-of=trident --show-labelskubectl config set-context --current --namespace=<NS> # default namespace for this shellרוב טעויות "not found" הן namespace: בלי -n kubectl מסתכל ב-default. get -A מראה שהאובייקט קיים — אבל לא בהכרח ב-namespace שלך.
מה רץ? get
Section titled “מה רץ? get”kubectl -n <NS> get pods -o wide # node, IPkubectl -n <NS> get all # Pods, Services, Deployments, StatefulSets (not Ingress/PVC/Secret)kubectl -n <NS> get deploy,sts,svc,ingress,pvc,cm,secretkubectl -n argocd get applicationskubectl get sc # StorageClasses (the default is marked)מצבים לזהות: Running + READY 1/1 — תקין. Pending — לא שובץ (בדוק PVC, משאבים). ImagePullBackOff — ה-image לא יורד. CrashLoopBackOff — מתרסק. Running אבל 0/1 — readiness נכשל (probes).
למה זה לא עולה? describe ו-events
Section titled “למה זה לא עולה? describe ו-events”kubectl -n <NS> describe pod -l trident.dev/service=<SERVICE> # read the Events section at the bottomkubectl -n <NS> describe pod postgres-0 # or one Pod by its exact name (from get pods)kubectl -n <NS> get events --sort-by=.lastTimestamp # newest lastkubectl -n <NS> get events --field-selector type=Warningאיך מוודאים: ב-describe, גלול ל-Events: — שם נמצא המשפט המדויק (Failed to pull image, unbound immediate PersistentVolumeClaims, secret "..." not found). קרא אותו מילה במילה במקום לנחש.
אין לוגים? ה-container לא התחיל
Section titled “אין לוגים? ה-container לא התחיל”kubectl -n <NS> logs <POD> -c <CONTAINER> # pick one containerkubectl -n <NS> logs <POD> --all-containerskubectl -n <NS> logs <POD> -p # --previous: the last crashed containerkubectl -n <NS> logs deploy/<SERVICE> --tail=50 --since=10mkubectl -n <NS> logs -l trident.dev/service=<SERVICE> --tail=20kubectl -n <NS> get pod <POD> -o jsonpath='{.status.initContainerStatuses[*].state}{"\n"}'kubectl -n <NS> get events --sort-by=.lastTimestamp | tailkubectl logsבלי-cעל Pod עם כמה containers (או init) בוחר container אחד, לא בהכרח השבור.- לוג ריק = ה-container מעולם לא התחיל. אל תחפש בלוג:
describe pod←Events(StartError,FailedMount,ImagePullBackOff). - init container שיצא
exit 0תקין: הוא לא הבעיה. ה-jsonpathלמעלה מראה את מצבו. - ב-
CrashLoopBackOffהלוג הנוכחי קצר:-pמראה למה הקודם מת.
איך מוודאים: Events מכיל משפט שגיאה מדויק. קרא אותו מילה במילה.
להיכנס ולבדוק: exec, port-forward
Section titled “להיכנס ולבדוק: exec, port-forward”kubectl -n <NS> exec -it deploy/<SERVICE> -- shkubectl -n <NS> exec deploy/<SERVICE> -- ls /run/secrets/<APP_DIR>kubectl -n <NS> port-forward svc/<SERVICE> 8080:8080 # then: curl http://127.0.0.1:8080/info- ל-image ייתכן שאין
shאו כלים (curl,ls): אזexecנכשל, ואבחון הולך דרךlogs/describe. - תמונות Python של הפרויקט אין בהן
curl:exec deploy/<SERVICE> -- python -c "import urllib.request as u; print(u.urlopen('http://<SERVICE>:8080/live').read())". port-forwardנשאר בחזית עד Ctrl+C. הרץcurlמטרמינל שני.
rollout: status, restart, history
Section titled “rollout: status, restart, history”kubectl -n <NS> rollout status deploy/<SERVICE>kubectl -n <NS> rollout restart deploy/<SERVICE>kubectl -n <NS> rollout history deploy/<SERVICE>kubectl -n <NS> rollout undo deploy/<SERVICE> # back one revisionrollout restart שימושי אחרי שינוי ב-Secret/ConfigMap שהאפליקציה קוראת רק בהפעלה. עם Argo: שינוי values ב-Git מחליף Pods בעצמו. rollout undo בקלאסטר ש-Argo מנהל יוחזר על ידי selfHeal: התיקון האמיתי הוא git revert ב-Git.
לקרוא שדה: -o jsonpath
Section titled “לקרוא שדה: -o jsonpath”kubectl -n <NS> get deploy <SERVICE> -o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'kubectl -n <NS> get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.phase}{"\n"}{end}'kubectl -n <NS> get svc <SERVICE> -o jsonpath='{.spec.selector}{"\n"}'kubectl -n <NS> get deploy <SERVICE> -o jsonpath='{.metadata.labels.app\.kubernetes\.io/part-of}{"\n"}'מפתח עם נקודות (כמו app.kubernetes.io/part-of) דורש \. לפני כל נקודה בתוך ה-jsonpath. בלי זה התוצאה ריקה.
פורמטים נוספים: -o wide, -o yaml, -o name, -o custom-columns=NAME:.metadata.name,IMAGE:.spec.containers[0].image.
לפענח ערך של Secret בלי להדפיס אותו בכל מקום
Section titled “לפענח ערך של Secret בלי להדפיס אותו בכל מקום”kubectl -n <NS> get secret <SECRET> -o jsonpath='{.data.postgres_password}' | base64 -d | wc -c # length onlykubectl -n <NS> get secret <SECRET> -o jsonpath='{.data.postgres_password}' | base64 -d # prints the valuekubectl -n <NS> get secret <SECRET> -o jsonpath='{.data}' | jq 'keys' # key names onlyכשרק צריך לבדוק שהערך קיים ותקין — אורך (wc -c) או שם המפתחות מספיקים. שם מפתח עם נקודה: {.data.a\.b}.
base64 של Secret אינו הצפנה, ולכן לא מדפיסים ערכים ל-screen share או ללוג.
לבדוק הרשאות ולנקות
Section titled “לבדוק הרשאות ולנקות”kubectl auth can-i get secrets -n <NS>kubectl -n <NS> delete pod -l trident.dev/service=<SERVICE> # the Deployment recreates itkubectl explain deployment.spec.strategy # field documentation from the API serverתיעוד offline בזמן המבחן: explain, api-resources, wait
Section titled “תיעוד offline בזמן המבחן: explain, api-resources, wait”kubectl explain <RESOURCE>.spec --recursive | head -40 # TRIDENT: statefulset.speckubectl explain <RESOURCE>.spec.<FIELD> # one field, with its descriptionkubectl api-resources | grep -i <KIND> # exact kind/apiVersion/short name, e.g. ingresskubectl -n <NS> wait --for=condition=Ready pod -l <LABEL> --timeout=120sאיך מוודאים: explain מדפיס את עץ השדות; wait מדפיס pod/... condition met או נכשל ב-timeout. (kubectl top לא זמין: אין metrics-server.)
הדפים הבאים
Section titled “הדפים הבאים”- Secrets ו-ConfigMaps — יצירה ל-namespace הנכון.
- Networking — DNS, Ingress,
curl --resolve, NetworkPolicy. - PVC ו-probes — Pod שנתקע ב-Pending, ו-Running אבל לא Ready.