Deployments
الهدف من الدرس
Section titled “الهدف من الدرس”ستتعلم كيف يستخدم Deployment كل من ReplicaSet و Pods لتشغيل تطبيق قابل للتحديث، وكيف تنفذ rollout و rollback وتقرأ الحالة.
الفكرة ببساطة
Section titled “الفكرة ببساطة”Deployment هو الطريقة الافتراضية لتشغيل تطبيق stateless في Kubernetes. أنت تقول له: “أريد 3 نسخ من هذا التطبيق بهذه الصورة”، وهو يحافظ على العدد المطلوب ويغير النسخ تدريجيا عند التحديث.
كيف يعمل Deployment؟
Section titled “كيف يعمل Deployment؟”flowchart LR D[Deployment web] --> RS1[ReplicaSet v1] D --> RS2[ReplicaSet v2] RS2 --> P1[Pod web-a] RS2 --> P2[Pod web-b] RS2 --> P3[Pod web-c]- Deployment يدير استراتيجية التحديث.
- ReplicaSet يحافظ على عدد Pods.
- Pod يشغل containers.
YAML كامل
Section titled “YAML كامل”احفظ الملف باسم deployment.yaml:
apiVersion: apps/v1kind: Deploymentmetadata: name: web namespace: default labels: app: webspec: replicas: 3 revisionHistoryLimit: 5 strategy: type: RollingUpdate rollingUpdate: maxUnavailable: 1 maxSurge: 1 selector: matchLabels: app: web template: metadata: labels: app: web tier: frontend spec: containers: - name: nginx image: nginx:1.27 ports: - name: http containerPort: 80 readinessProbe: httpGet: path: / port: http initialDelaySeconds: 5 periodSeconds: 5 livenessProbe: httpGet: path: / port: http initialDelaySeconds: 15 periodSeconds: 10 resources: requests: cpu: "100m" memory: "128Mi" limits: cpu: "500m" memory: "256Mi"نقطتان مهمتان:
| الجزء | لماذا مهم؟ |
|---|---|
selector.matchLabels | يحدد أي Pods يملكها Deployment |
template.metadata.labels | يجب أن تطابق selector، وإلا يفشل إنشاء Deployment |
أوامر kubectl
Section titled “أوامر kubectl”kubectl apply -f deployment.yamlkubectl get deploymentskubectl get replicasetskubectl get pods -l app=webkubectl rollout status deployment/webExpected output:
deployment.apps/web createddeployment "web" successfully rolled outNAME READY UP-TO-DATE AVAILABLE AGEweb 3/3 3 3 45sتنفيذ تحديث
Section titled “تنفيذ تحديث”kubectl set image deployment/web nginx=nginx:1.28kubectl rollout status deployment/webkubectl rollout history deployment/webExpected output:
deployment.apps/web image updateddeployment "web" successfully rolled outREVISION CHANGE-CAUSE1 <none>2 <none>Rollback
Section titled “Rollback”لو الإصدار الجديد فيه مشكلة:
kubectl rollout undo deployment/webkubectl rollout status deployment/webkubectl get pods -l app=webمتى أستخدم ماذا؟
Section titled “متى أستخدم ماذا؟”| أحتاج إلى | استخدم |
|---|---|
| تطبيق web أو API بدون state محلي | Deployment |
| قاعدة بيانات تحتاج اسم ثابت وتخزين ثابت | StatefulSet |
| Agent يعمل على كل Node | DaemonSet |
| مهمة تنتهي مرة واحدة | Job |
| مهمة متكررة بوقت محدد | CronJob |
أخطاء شائعة
Section titled “أخطاء شائعة”| الخطأ | التشخيص | الحل |
|---|---|---|
selector does not match template labels | kubectl apply -f deployment.yaml يفشل | اجعل labels في template تطابق selector |
| rollout لا ينتهي | kubectl rollout status deployment/web يعلق | افحص kubectl describe pod و probes |
| Pods كثيرة تقع | kubectl get pods يظهر CrashLoopBackOff | اقرأ logs وعدل image أو config |
| التحديث أحدث downtime | maxUnavailable كبير أو readiness ناقص | أضف readinessProbe وخفض maxUnavailable |
Lab: Deployment قابل للتحديث والاسترجاع
الهدف: تشغيل Nginx بثلاث نسخ، تنفيذ update، ثم rollback.
Prerequisites
Section titled “Prerequisites”- Cluster محلي يعمل.
kubectl get nodesيرجع Node جاهز.
- احفظ YAML في
deployment.yaml. - طبق الملف:
kubectl apply -f deployment.yamlkubectl rollout status deployment/web- تأكد من عدد النسخ:
kubectl get pods -l app=web- حدث image:
kubectl set image deployment/web nginx=nginx:1.28kubectl rollout status deployment/web- ارجع للإصدار السابق:
kubectl rollout undo deployment/webkubectl rollout status deployment/webExpected output
Section titled “Expected output”NAME READY STATUS RESTARTS AGEweb-xxxxxxxxxx-aaaaa 1/1 Running 0 1mweb-xxxxxxxxxx-bbbbb 1/1 Running 0 1mweb-xxxxxxxxxx-ccccc 1/1 Running 0 1mCleanup
Section titled “Cleanup”kubectl delete -f deployment.yamlkubectl get deployments,pods -l app=webالربط بالمشروع النهائي
Section titled “الربط بالمشروع النهائي”كل مشروع في المسار سيستخدم Deployment للتطبيقات stateless. في مشروع GitOps ستتعلم لماذا يجب أن يكون Deployment مكتوبا في Git، وليس منشأ بأوامر مؤقتة فقط.