This lesson explores a practice test on network policies in a Kubernetes cluster, focusing on traffic control for deployed applications and services.
In this lesson, we explore a practice test on network policies in a Kubernetes cluster. The exercise involves inspecting deployed web applications, services, and network policies to understand how traffic is controlled within the environment.
The exercise begins with the question: “How many network policies do you see in the environment?” In our setup, several web applications and services have been deployed, and a specific network policy is defined to control traffic to one of the pods. Let’s walk through the environment.
Here, notice that the payroll service is exposed on port 8080, the database service on port 3306, and both the external and internal services are available on port 8080.
root@controlplane:~# k describe netpol payroll-policyName: payroll-policyNamespace: defaultCreated on: 2022-04-18 20:35:54 +0000 UTCLabels: <none>Annotations: <none>Spec: PodSelector: name=payroll Allowing ingress traffic: To Port: 8080/TCP From: PodSelector: name=internal Not affecting egress traffic Policy Types: Ingress
This policy allows ingress traffic on TCP port 8080 to the payroll pod only if the traffic originates from pods with the label name=internal. Since no egress rules are defined, the payroll pod continues to allow all outgoing traffic.
By default, all pods allow both ingress and egress traffic. Once a network policy is applied, only the traffic permitted by the policy is allowed. In this scenario:
Only ingress traffic from the internal pod on TCP port 8080 is allowed to reach the payroll pod.
All egress traffic from the payroll pod remains unrestricted.
Pods or sources without the label name=internal cannot access the payroll pod on port 8080.
Connectivity tests revealed that:
The internal-facing application successfully connected to the payroll service on port 8080.
The external-facing application timed out when attempting to access the payroll service, confirming that the policy is working as intended.
The lab exercise also requires creating a new network policy to allow traffic exclusively from the internal application to both the payroll and database (MySQL) services. This policy will restrict egress traffic from the internal pod so that it only communicates with the payroll pod on TCP port 8080 and the MySQL pod on TCP port 3306.Below is a sample YAML specification for this custom network policy. Save it as internal-policy.yaml:
The internal-facing application should be able to access both the payroll and database services.
The external-facing application or any other source will be unable to access the payroll pod on port 8080.
This completes our lab exercise on network policies, demonstrating how to control ingress and egress traffic within a Kubernetes environment effectively.For further information on Kubernetes network policies, consider exploring the Kubernetes Documentation.