متقدم
البحث فيالمعامل
طريقة البحث
  • ابحث عن مهمة أو أداة (lvm، selinux، ‎@podman)؛ وصفِّ حسب المستوى والكتاب أو الدورة.
  • مع مربع بحث فارغ، الفلتر وحده يعرض المعامل — مثلًا كل المعامل الصعبة لدورة واحدة.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

معمل 4.1CKS, الفصل 4

Enforce the restricted Pod Security Standard and fix a rejected pod

المستوى
متوسط
35 دقيقة
الأجهزة
a Kubernetes cluster 1.25 or newer (kind, minikube or kubeadm)
الحالة
لم يبدأ

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

الهدف

Turn on Pod Security Admission for a namespace in three steps (warn, audit, enforce), watch a typical "works on my laptop" pod get rejected, and rewrite it until it passes the restricted level: non-root, no privilege escalation, all capabilities dropped, a seccomp profile and a read-only root file system.

قبل أن تبدأ

  • kubectl version shows a server of 1.25 or newer (Pod Security Admission is built in and on).
  • Save this pod as bad-pod.yaml:
    bad-pod.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: web
    spec:
      containers:
        - name: web
          image: nginx:1.27
          ports: [{containerPort: 80}]
          securityContext:
            privileged: true

المهام

  1. Make the namespace secure-apps with the labels warn=restricted and audit=restricted only. Apply the bad pod and read the warnings.
  2. Delete the pod and set enforce=restricted with a pinned version (enforce-version=v1.30 or your version).
  3. Apply the bad pod again and read why it is rejected.
  4. Rewrite it as good-pod.yaml so it passes restricted. nginx must still serve pages (it needs a non-root image or port above 1024 and writable temp folders).
  5. Check with a server-side dry run that existing pods in the default namespace would pass baseline.

تحقّق من عملك

kubectl get ns secure-apps --show-labels
kubectl -n secure-apps apply -f good-pod.yaml
kubectl -n secure-apps get pod web
kubectl -n secure-apps exec web -- id
kubectl -n secure-apps port-forward pod/web 8080:8080 & sleep 2; curl -s localhost:8080 | head -4; kill %1
  • The namespace has pod-security.kubernetes.io/enforce=restricted.
  • bad-pod.yaml is rejected with violates PodSecurity "restricted:...".
  • good-pod.yaml is Running; id shows a user that is not 0; curl shows the nginx welcome page.

تلميحات

اعرض التلميحات
  • Labels: pod-security.kubernetes.io/<mode>=<level> and pod-security.kubernetes.io/<mode>-version=<v>.
  • Restricted needs: runAsNonRoot: true, allowPrivilegeEscalation: false, capabilities: drop: [ALL], seccompProfile: type: RuntimeDefault, and no privileged, hostPath, hostNetwork or hostPort.
  • nginxinc/nginx-unprivileged listens on 8080 and runs as user 101.
  • With readOnlyRootFilesystem: true, give nginx emptyDir volumes for /tmp (and its cache folders).
  • kubectl label --dry-run=server --overwrite ns default pod-security.kubernetes.io/enforce=baseline shows warnings for pods that would fail.

الحل

اعرض الحل
  1. Warn and audit first.
    kubectl create ns secure-apps
    kubectl label ns secure-apps pod-security.kubernetes.io/warn=restricted pod-security.kubernetes.io/audit=restricted
    kubectl -n secure-apps apply -f bad-pod.yaml
    Warning: would violate PodSecurity "restricted:latest": privileged (container "web" must not set securityContext.privileged=true), allowPrivilegeEscalation != false, unrestricted capabilities, runAsNonRoot != true, seccompProfile
    pod/web created
  2. Enforce.
    kubectl -n secure-apps delete pod web
    kubectl label ns secure-apps pod-security.kubernetes.io/enforce=restricted pod-security.kubernetes.io/enforce-version=v1.30
    kubectl -n secure-apps apply -f bad-pod.yaml
    Error from server (Forbidden): error when creating "bad-pod.yaml": pods "web" is forbidden: violates PodSecurity "restricted:v1.30": privileged ...
  3. The fixed pod.
    good-pod.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: web
    spec:
      securityContext:
        runAsNonRoot: true
        runAsUser: 101
        seccompProfile:
          type: RuntimeDefault
      containers:
        - name: web
          image: nginxinc/nginx-unprivileged:1.27-alpine
          ports: [{containerPort: 8080}]
          securityContext:
            allowPrivilegeEscalation: false
            readOnlyRootFilesystem: true
            capabilities:
              drop: [ALL]
          volumeMounts:
            - {name: tmp, mountPath: /tmp}
      volumes:
        - {name: tmp, emptyDir: {}}
    kubectl -n secure-apps apply -f good-pod.yaml
    kubectl -n secure-apps exec web -- id
    uid=101(nginx) gid=101(nginx) groups=101(nginx)
  4. Test another namespace without changing it.
    kubectl label --dry-run=server --overwrite ns default pod-security.kubernetes.io/enforce=baseline

ملاحظاتي

لا توجد ملاحظات بعد.

دروس هذا المعمل

الأوامر في هذا المعمل