CKA · Question #38
Create a new NetworkPolicy named allow-port-from-namespace in the existing namespace echo. Ensure that the new NetworkPolicy allows Pods in namespace my-app to connect to port 9000 of Pods in…
The task requires creating a Kubernetes NetworkPolicy YAML definition to restrict ingress traffic to port 9000 for pods in the 'echo' namespace, allowing connections only from pods within the 'my-app' namespace.
Question
Exhibit
Explanation
The task requires creating a Kubernetes NetworkPolicy YAML definition to restrict ingress traffic to port 9000 for pods in the 'echo' namespace, allowing connections only from pods within the 'my-app' namespace.
Approach. The correct interaction involves creating a YAML file (e.g., networkpolicy.yaml) within the provided terminal, defining the NetworkPolicy resource as specified, and then applying it to the Kubernetes cluster.
Steps:
- Open a text editor in the terminal:
vim networkpolicy.yaml - Insert the following YAML content into the file:
apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-port-from-namespace namespace: echo spec: podSelector: {} policyTypes: - Ingress ingress: - from: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: my-app ports: - protocol: TCP port: 9000 - Save the file and exit the editor (e.g.,
Esc,:wq). - Apply the NetworkPolicy using
kubectl apply -f networkpolicy.yaml.
Reasoning for the YAML:
apiVersion: networking.k8s.io/v1andkind: NetworkPolicydefine the resource type.metadata.name: allow-port-from-namespacesets the required name.metadata.namespace: echoensures the policy applies to pods within theechonamespace.podSelector: {}makes the policy apply to all pods within theechonamespace (the target pods).policyTypes: - Ingressexplicitly enables ingress rules for this policy.ingress:block defines the incoming traffic rules.from: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: my-appspecifies that only traffic originating from pods in the namespace labeledmy-app(typicallykubernetes.io/metadata.nameis used by default for namespace names) is allowed.ports: - protocol: TCP port: 9000restricts the allowed ingress traffic to TCP port 9000, ensuring access is not allowed to other ports.
Common mistakes.
- common_mistake. Several common mistakes can lead to an incorrect solution:
- Missing
policyTypes: [Ingress]: Without this, theingressrules will not be enforced, and the NetworkPolicy will effectively allow all ingress traffic (if noegressrules are present andpolicyTypesis omitted,policyTypesdefaults toIngressifingressis specified andEgressifegressis specified. However, explicitly definingpolicyTypesis best practice and prevents ambiguity). - Incorrect
podSelector: IfpodSelectoris too restrictive (e.g.,podSelector: { matchLabels: { app: some-app } }) and doesn't match all desired target pods in theechonamespace, the policy won't apply to all intended pods. Using{}applies to all pods in the policy's namespace. - Incorrect
namespaceSelector: UsingpodSelectorwithin thefromblock instead ofnamespaceSelector, or using incorrect labels for themy-appnamespace, would fail to correctly identify the source pods. - Omitting
portsor incorrectport: If theportssection is omitted from theingressrule, all ports would be allowed, violating the constraint to only allow port 9000. Specifying the wrong port number would also be incorrect. - Wrong
metadata.namespace: Creating the NetworkPolicy in a namespace other thanechowould make it ineffective for the intended target pods.
Concept tested. Kubernetes NetworkPolicies, specifically creating an ingress rule to control traffic flow based on source namespace and target port, using namespaceSelector and ports within the ingress section, and understanding the role of podSelector and policyTypes.
Reference. https://kubernetes.io/docs/concepts/services-networking/network-policies/
Topics
Community Discussion
No community discussion yet for this question.
