This lesson reviews a practical test on network policies within a Kubernetes environment, focusing on deployment, inspection, and connectivity validation.
In this lesson, we review a practical test on network policies within a Kubernetes environment. The lab deploys several web applications, services, and network policies. Your objective is to inspect the environment and answer questions related to the applied network policies.
First, verify the running applications by listing the pods. In this environment, four pods are running: external, internal, mysql (the database), and payroll. Execute the following command:
The next step is to check the applied network policies. Initially, running:
Copy
Ask AI
root@controlplane:~# kubectl get networkpolicieserror: the server doesn't have a resource type "networkpolicies"
Then, using the shorthand command:
Copy
Ask AI
root@controlplane:~# kubectl get netpolNAME POD-SELECTOR AGEpayroll-policy name=payroll 3m31s
The output shows a single network policy, payroll-policy, which applies to the pod labeled name=payroll.
The payroll network policy allows ingress TCP traffic on port 8080 to the payroll pod, but only from pods with the name=internal label. Outbound traffic (egress) is not restricted.
Step 6: Creating a New Network Policy for Internal Pod Egress
The next task is to create a network policy that further restricts the internal pod’s egress traffic. The goal is to allow the internal pod only to access:
The payroll pod on port 8080.
The MySQL (DB) pod on port 3306.
Create a file named internal-policy.yaml with the following content:
Copy
Ask AI
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: internal-policy namespace: defaultspec: # This policy applies to the internal pod. podSelector: matchLabels: name: internal policyTypes: - Ingress - Egress # Ingress traffic remains unrestricted. egress: # Allow egress traffic to the payroll pod on TCP port 8080. - to: podSelector: matchLabels: name: payroll ports: - protocol: TCP port: 8080 # Allow egress traffic to the MySQL (DB) pod on TCP port 3306. - to: podSelector: matchLabels: name: mysql ports: - protocol: TCP port: 3306
Apply this network policy with the following command:
Copy
Ask AI
root@controlplane:~# kubectl create -f internal-policy.yamlnetworkpolicy.networking.k8s.io/internal-policy created
Verify the applied policy:
Copy
Ask AI
root@controlplane:~# kubectl describe netpol internal-policyName: internal-policyNamespace: defaultCreated on: 2022-04-18 20:53:13 +0000 UTCLabels: <none>Annotations: <none>Spec: PodSelector: name=internal Not affecting ingress traffic Allowing egress traffic: To: PodSelector: name=payroll To Port: 8080/TCP To: PodSelector: name=mysql To Port: 3306/TCP Policy Types: Egress
The internal policy ensures that the internal pod can only send egress traffic to the payroll pod on port 8080 and the MySQL pod on port 3306, effectively blocking any other outbound connections.
This configuration confirms that the network policies enforce the intended connectivity restrictions, completing the lab exercise.