kubectl و YAML
الهدف من الدرس
Section titled “الهدف من الدرس”ستتعلم أوامر kubectl الأساسية، بنية ملفات YAML، وكيف تنتقل من أمر سريع إلى manifest قابل للحفظ في Git.
الفكرة ببساطة
Section titled “الفكرة ببساطة”kubectl هو العميل الذي يتحدث مع Kubernetes API Server. أما YAML فهو وصف الحالة المطلوبة: ما الموارد التي تريدها؟ بأي أسماء؟ بأي إعدادات؟
دورة العمل اليومية
Section titled “دورة العمل اليومية”flowchart LR W[اكتب YAML] --> A[kubectl apply] A --> API[API Server] API --> C[Controllers] C --> S[Actual State] S --> G[kubectl get/describe/logs] G --> Wأوامر قراءة الحالة
Section titled “أوامر قراءة الحالة”kubectl get nodeskubectl get namespaceskubectl get pods -Akubectl get pod hello-pod -o widekubectl describe pod hello-podkubectl logs hello-podkubectl events --sort-by=.metadata.creationTimestampExpected output:
NAME STATUS ROLES AGE VERSIONkind-control-plane Ready control-plane 10m v1.34.0بنية YAML
Section titled “بنية YAML”أي object في Kubernetes غالبا يبدأ بهذه الأجزاء:
apiVersion: v1kind: ConfigMapmetadata: name: app-config namespace: default labels: app: demodata: APP_MODE: "dev" LOG_LEVEL: "debug"| الحقل | وظيفته |
|---|---|
apiVersion | إصدار API لهذا المورد |
kind | نوع المورد |
metadata | الاسم، namespace، labels، annotations |
spec أو data | الحالة المطلوبة أو البيانات حسب نوع المورد |
تطبيق وتعديل وحذف
Section titled “تطبيق وتعديل وحذف”kubectl apply -f configmap.yamlkubectl get configmap app-config -o yamlkubectl diff -f configmap.yamlkubectl delete -f configmap.yamlاستخدم kubectl diff قبل التعديل عندما تريد معرفة ما الذي سيتغير.
أوامر مفيدة بدون حفظ
Section titled “أوامر مفيدة بدون حفظ”إنشاء YAML مبدئي بدون تطبيق:
kubectl create deployment web --image=nginx:1.27 --dry-run=client -o yaml > deployment.yamlkubectl create service clusterip web --tcp=80:80 --dry-run=client -o yaml > service.yamlشرح سريع:
| الخيار | المعنى |
|---|---|
--dry-run=client | لا يرسل المورد إلى cluster |
-o yaml | اطبع الناتج بصيغة YAML |
> | احفظ الناتج في ملف |
Namespaces و Contexts
Section titled “Namespaces و Contexts”kubectl config get-contextskubectl config current-contextkubectl config set-context --current --namespace=defaultkubectl get pods --namespace kube-systemلا تعمل على namespace خطأ. قبل أي أمر حساس، نفذ:
kubectl config current-contextkubectl config view --minify --output 'jsonpath={..namespace}'أخطاء شائعة
Section titled “أخطاء شائعة”| الخطأ | السبب | الحل |
|---|---|---|
error: no context exists | kubeconfig غير مضبوط | راجع kubectl config get-contexts |
the server doesn't have a resource type | kind أو apiVersion خطأ | استخدم kubectl api-resources |
forbidden | صلاحيات RBAC غير كافية | افحص kubectl auth can-i |
| YAML لا يطبق | indentation أو field خطأ | استخدم kubectl apply --dry-run=server -f file.yaml |
Lab: من أمر سريع إلى YAML قابل للحفظ
الهدف: توليد Deployment و Service بصيغة YAML، مراجعة الفرق، ثم تطبيقهما.
Prerequisites
Section titled “Prerequisites”- Cluster محلي يعمل.
- صلاحية إنشاء Deployment و Service في namespace الحالي.
- ولد Deployment:
kubectl create deployment web --image=nginx:1.27 --dry-run=client -o yaml > deployment.yaml- ولد Service:
kubectl create service clusterip web --tcp=80:80 --dry-run=client -o yaml > service.yaml- راجع الملفات، ثم طبقها:
kubectl diff -f deployment.yamlkubectl apply -f deployment.yamlkubectl apply -f service.yaml- افحص الموارد:
kubectl get deploy,svc,pods -l app=webExpected output
Section titled “Expected output”deployment.apps/web createdservice/web createdNAME READY UP-TO-DATE AVAILABLEdeployment.apps/web 1/1 1 1Cleanup
Section titled “Cleanup”kubectl delete -f service.yamlkubectl delete -f deployment.yamlالربط بالمشروع النهائي
Section titled “الربط بالمشروع النهائي”كل مشروع ستبنيه سيبدأ بملفات YAML واضحة، ثم سيتطور إلى Helm و GitOps. إتقان kubectl و YAML هو أساس كل شيء بعد ذلك.