exam questions

Exam CKA All Questions

View all questions & answers for the CKA exam

Exam CKA topic 1 question 5 discussion

Actual exam question from CNCF's CKA
Question #: 5
Topic #: 1
[All CKA Questions]

SIMULATION -


Task -
Create a new NetworkPolicy named allow-port-from-namespace in the existing namespace fubar.
Ensure that the new NetworkPolicy allows Pods in namespace internal to connect to port 9000 of Pods in namespace fubar.
Further ensure that the new NetworkPolicy:
✑ does not allow access to Pods, which don't listen on port 9000
✑ does not allow access from Pods, which are not in namespace internal

Show Suggested Answer Hide Answer
Suggested Answer:



Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
TemitopeWalker
Highly Voted 2 years, 1 month ago
I think this asnwer is wrong the solution should be apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-port-from-namespace namespace: fubar spec: podSelector: {} policyTypes: - Ingress ingress: - from: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: internal ports: - protocol: TCP port: 9000
upvoted 57 times
...
pentium2000
Highly Voted 2 years ago
For this question, we should create a label for "internal" namespace in further YAML. # k label ns internal tier=internal apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-port-from-namespace namespace: fubar spec: podSelector: {} policyTypes: - Ingress ingress: - from: - namespaceSelector: matchLabels: tier: internal ports: - protocol: TCP port: 9000
upvoted 12 times
Jibbajabba
1 year ago
Don't think you need to create a label specifically unless you need to work with multiple namespaces "The Kubernetes control plane sets an immutable label kubernetes.io/metadata.name on all namespaces, the value of the label is the namespace name. While NetworkPolicy cannot target a namespace by its name with some object field, you can use the standardized label to target a specific namespace." I suppose that implies you CAN but you don't HAVE TO.
upvoted 7 times
...
...
noahsark
Most Recent 1 month, 3 weeks ago
killer_sh_lab: part1 # Changing to port 80 for test purposes # From internal to fubar # netpol is in fubar # ingress is from internal k create ns fubar --labels=’name=fubar’ k run nginx -n=fubar --image nginx --port 80 k create ns internal --labels=’name=internal’ k run nginx2 -n=internal --image nginx --port 80 --labels=’name=internal’
upvoted 1 times
...
Pi_otR
10 months, 3 weeks ago
Due to this part: "- does not allow access from Pods, which are not in namespace internal" -means that even pods in namespace fubar should not be able to reach other pods in same namespace. I would suggest to do following : ---- apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-port-from-namespace namespace: fubar spec: podSelector: {} # Selects all Pods in the `fubar` namespace policyTypes: - Ingress - Egress ingress: - from: - namespaceSelector: matchLabels: name: internal ports: - protocol: TCP port: 9000 this way Egress is specified but due to fact nothing is defined pod in same NSs are not able to communicate.
upvoted 2 times
fonte
5 months ago
No need for that... the policy already restricts the traffic to the internal ns. Tested it and even another pod in the fubar ns cannot reach the other pods listening port 9000.
upvoted 2 times
...
...
Alencar_07
1 year ago
apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-port-from-namespace namespace: fubar spec: podSelector: # Selects Pods in the namespace where the NetworkPolicy is applied matchLabels: {} policyTypes: - Ingress ingress: - from: - namespaceSelector: # Allow traffic only from Pods in the 'internal' namespace matchLabels: name: internal ports: - protocol: TCP port: 9000 # Allow connections to port 9000 egress: - to: - namespaceSelector: # Allow traffic only to Pods in the 'fubar' namespace matchLabels: name: fubar ports: - protocol: TCP port: 9000 # Allow connections to port 9000
upvoted 1 times
...
Stunomatic
1 year, 1 month ago
apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-port-from-namespace namespace: fubar spec: podSelector: {} policyTypes: - Ingress ingress: - from: - namespaceSelector: matchLabels: name: internal ports: - protocol: TCP port: 9000
upvoted 1 times
...
aloshari
1 year, 2 months ago
I think we need to check my-app labels first to match it,
upvoted 1 times
...
Shenannigan
1 year, 4 months ago
Tested locally and this worked for me Used Nginx Pod with port set to 9000 in the fubar namespace Used Alpine Pod image alpine/curl in the internal namespace for testing exec into the Alpine Pod and run the command: curl (your nginx pod IP seperated by dashes).fubar.pod.cluster.local:9000 Policy: apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-port-from-namespace namespace: fubar spec: podSelector: {} policyTypes: - Ingress ingress: - from: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: "internal" ports: - protocol: TCP port: 9000
upvoted 4 times
VivekSolutionArchitect
1 year, 2 months ago
It doesn't work for me when I use port 9000 for nginx, however port 80 works fine. Not sure if I am doing something incorrectly.
upvoted 2 times
...
...
didorins
1 year, 5 months ago
I still fail to understand this question. Do they want me to create a policy that allows only traffic on port 9000 from namespace internal (x2 ingress) or do they want to create a network policy to restrict incoming traffic, so that only pods FROM (ingress) internal namespace are allowed and pods TO (egress) port 9000 ?
upvoted 1 times
...
Nurbol
1 year, 6 months ago
To one who wonder where this from: kubernetes.io/metadata.name: internal, run: k get ns internal --show-labels
upvoted 3 times
...
sonixrw
1 year, 6 months ago
Should we also add deny any any and add NP to access port 9000 in ns foobar, from internal?
upvoted 1 times
...
ahmedovelshan
1 year, 7 months ago
Maybe this? apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-port-from-namespace namespace: fubar spec: podSelector: matchLabels: - namespaceSelector: matchExpressions: - key: namespace operator: In Values: ["fubar"] policyTypes: - Ingress - Egress ingress: - from: - namespaceSelector: matchExpressions: - key: namespace operator: In Values: ["internal"] ports: - protocol: TCP port: 9000
upvoted 3 times
...
kopper2019
1 year, 7 months ago
using this I get an error so I had to use label, at least practicing not in exam yet kubernetes.io/metadata.name: internal
upvoted 2 times
dayody
1 year, 4 months ago
me too I got an error using it
upvoted 1 times
...
kopper2019
1 year, 7 months ago
I was using kubernetes.io/metadata.name=echo instead of kubernetes.io/metadata.name: echo apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: test-network-policy namespace: my-app spec: podSelector: {} policyTypes: - Ingress ingress: - from: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: echo ports: - protocol: TCP port: 9000
upvoted 2 times
...
...
Sylzys
1 year, 8 months ago
Is there a template during the exam or do we have to write it all from scratch?
upvoted 2 times
...
ramon712
1 year, 9 months ago
Sorry, I disagree with : matchLabels: kubernetes.io/metadata.name: internal I suggest : ingress: - from: - namespaceSelector: matchLabels: items[0].metadata.namespace: internal # from query kubectl get po with jsonpath What do you think ?
upvoted 2 times
ramon712
1 year, 9 months ago
I made an error. So, the answer from Kubernetes's document : kubernetes.io/metadata.name Example: kubernetes.io/metadata.name: "mynamespace" Used on: Namespaces The Kubernetes API server (part of the control plane) sets this label on all namespaces. The label value is set to the name of the namespace. You can't change this label's value. This is useful if you want to target a specific namespace with a label selector.
upvoted 1 times
...
...
rajusai
1 year, 11 months ago
They have asked us for namespace internal, hence following is the correct under matchlabels kubernetes.io/metadata.name: internal
upvoted 2 times
...
Steve122
1 year, 11 months ago
no magic: (this policy is ns scoped so no need any labelling on ns) tested, works apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-port-from-namespace namespace: fubar spec: podSelector: {} policyTypes: - Ingress ingress: - from: ports: - protocol: TCP port: 9000
upvoted 1 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago