CKAD · Question #24
Update the Pod ckad00018-newpod in the ckad00018 namespace to use a NetworkPolicy allowing the Pod to send and receive traffic only to and from the pods web and db
The task requires creating a Kubernetes NetworkPolicy to restrict a specific Pod's ingress and egress traffic solely to other Pods identified by 'web' and 'db' labels.
Question
Exhibits
Explanation
The task requires creating a Kubernetes NetworkPolicy to restrict a specific Pod's ingress and egress traffic solely to other Pods identified by 'web' and 'db' labels.
Approach. The correct interaction involves creating a YAML definition for a Kubernetes NetworkPolicy and applying it to the cluster using kubectl.
- Set Context (if not already): The exhibit shows
kubectl config use-context k8s. In an exam, this might be a preliminary step or already configured. - Identify Pod Labels: Since
NetworkPolicyusespodSelectorto target pods by labels, the first step is to identify the labels of theckad00018-newpodpod. A common command would bekubectl get pod ckad00018-newpod -n ckad00018 -o yaml. For the purpose of this explanation, we assume the podckad00018-newpodhas the labelapp: ckad00018-newpod, and thewebanddbpods have labelsapp: webandapp: dbrespectively, as is standard practice in such scenarios. - Create NetworkPolicy YAML: Create a file (e.g.,
newpod-policy.yaml) with the following content:apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: ckad00018-newpod-restrict-policy namespace: ckad00018 # Policy must be in the same namespace as the pod spec: podSelector: matchLabels: app: ckad00018-newpod # Selects the target pod to apply the policy to policyTypes: - Ingress # Apply rules for incoming traffic - Egress # Apply rules for outgoing traffic ingress: - from: - podSelector: matchLabels: app: web # Allow ingress from pods with label app: web - podSelector: matchLabels: app: db # Allow ingress from pods with label app: db egress: - to: - podSelector: matchLabels: app: web # Allow egress to pods with label app: web - podSelector: matchLabels: app: db # Allow egress to pods with label app: db - Apply NetworkPolicy: Apply the created YAML file using
kubectl apply -f newpod-policy.yaml.
Common mistakes.
- common_mistake. 1. Incorrect
podSelector: Using apodSelectorthat does not match the actual labels ofckad00018-newpod,web, ordbpods will result in the policy not being applied or not functioning as intended. Always verify actual pod labels (kubectl get pod <pod-name> -o yaml).
- Missing or Incorrect
policyTypes: IfpolicyTypesis omitted or only one ofIngressorEgressis specified when both are needed, the policy will either not apply correctly or will only restrict one direction of traffic, leaving the other unrestricted. The question explicitly states 'send and receive traffic'. - Wrong Namespace: NetworkPolicies are namespace-scoped. Creating the policy in a different namespace than
ckad00018will mean it won't apply tockad00018-newpod. - Incorrect Rule Structure: Mistakes in the YAML structure, such as placing
podSelectordirectly underingressoregressinstead of withinfromortoclauses, or misconfiguring thematchLabelsfor thewebanddbpods, will lead to a non-functional policy. - Forgetting Default Deny: Once a
NetworkPolicytargets a pod, all traffic not explicitly allowed by that policy (or other policies also targeting the pod) is implicitly denied. Assuming traffic will flow if not explicitly denied is a common misunderstanding once a policy is in place.
Concept tested. Kubernetes NetworkPolicies, including their structure (apiVersion, kind, metadata, spec), how to use podSelector to target specific pods and define allowed communication sources/destinations, specifying policyTypes (Ingress, Egress), and understanding the declarative 'default deny' behavior when a policy is applied.
Topics
Community Discussion
No community discussion yet for this question.

