WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Kubernetes · Advanced · question 56 of 100

Describe the process of implementing network segmentation and isolation using Kubernetes Network Policies.?

📕 Buy this interview preparation book: 100 Kubernetes questions & answers — PDF + EPUB for $5

Kubernetes Network Policies allow you to define rules to control the traffic flow between Pods in a Kubernetes cluster. By default, Pods can communicate with each other without restrictions. Network Policies provide a way to segment the network and isolate traffic between Pods in a more fine-grained manner.

To implement Network Policies in Kubernetes, you first need to ensure that your cluster supports it. This means that you must have a network plugin that implements the Kubernetes NetworkPolicy API. Many popular plugins like Calico, Weave Net, and Cilium support Network Policies out of the box.

Once you have a supported network plugin, you can start creating Network Policies to control the traffic flow. Network Policies are defined using YAML files and consist of one or more rules that specify which traffic should be allowed or denied. Each rule can match on various criteria such as the source or destination namespace, Pod labels, IP addresses, and ports.

Here is an example Network Policy that allows traffic only from a specific set of Pods:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-web-traffic
spec:
  podSelector:
    matchLabels:
      app: web
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: web
    - podSelector:
        matchLabels:
          app: db
    ports:
    - protocol: TCP
      port: 80

In this example, we have defined a Network Policy that allows traffic to Pods with the label app: web on port 80, but only from other Pods with the labels app: web or app: db. All other traffic is denied.

You can apply the Network Policy to your cluster using the kubectl apply command:

kubectl apply -f allow-web-traffic.yaml

Once the Network Policy is applied, you can verify its status using the kubectl describe command:

kubectl describe networkpolicy allow-web-traffic

Network Policies provide a powerful way to segment and isolate your Kubernetes network, but they require careful planning and testing to avoid unintended consequences. It’s important to thoroughly understand how your applications communicate and to define policies that balance security and usability.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Kubernetes interview — then scores it.
📞 Practice Kubernetes — free 15 min
📕 Buy this interview preparation book: 100 Kubernetes questions & answers — PDF + EPUB for $5

All 100 Kubernetes questions · All topics