How to Secure MySQL Database Access Using Kubernetes NetworkPolicy
The DevOpscube team is deploying a multi-tier application in a Kubernetes cluster. The application consists of a frontend tier and a database tier running in separate namespaces.
The database must not be accessible by arbitrary workloads in the cluster. Only the frontend application should be permitted to communicate with the MySQL database.
Your task is to configure the application and network access controls so that the frontend can communicate with the database while unauthorized workloads are denied.
Create the following namespaces:
frontenddatabase
Deploy a frontend workload in the frontend namespace with 2 NGINX Pods. The Pods must use the nginx image, expose port 80, and have the labels:
app=nginx
tier=frontendDeploy a MySQL workload in the database namespace with 2 Pods using the mysql:latest image. Configure the required MySQL root password and expose port 3306. The database Pods must have the labels:
app=mysql
tier=databaseCreate a NetworkPolicy named db-access-policy in the database namespace.
The policy must restrict access to the MySQL Pods so that only Pods labeled tier=frontend from the frontend namespace can connect to port 3306.
After applying the policy, verify that:
- The frontend Pods can connect to the MySQL database on port
3306. - Pods from other namespaces cannot access the database.
- Pods without the required frontend label cannot access the database.
- The MySQL Pods remain available and healthy.
Use appropriate Kubernetes commands to inspect the NetworkPolicy and test connectivity from both permitted and denied sources.
NetworkPolicy,deployment refer to the official Kubernetes documentation.Create the Namespaces
Create separate namespaces for the frontend and database workloads:
kubectl create namespace frontend
kubectl create namespace databaseVerify that both namespaces are created:
$ kubectl get namespaces
NAME STATUS AGE
database Active 11s
frontend Active 18sYou should see both frontend and database in the Active state.
Deploy the NGINX Pods
Create a Deployment with 2 NGINX replicas in the frontend namespace:
$ kubectl create deployment nginx \
--image=nginx \
--replicas=2 \
-n frontend \
--dry-run=client -o yaml > nginx-deploy.yamlEdit nginx-deploy.yaml and add the required app=nginx and tier=frontend labels to both the Deployment selector and Pod template.
Apply the Deployment:
$ kubectl apply -f nginx-deploy.yaml
deployment.apps/nginx createdVerify:
$ kubectl get pods -n frontend --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-6f78978d75-nt56c 1/1 Running 0 25s app=nginx,pod-template-hash=6f78978d75,tier=frontend
nginx-6f78978d75-z956w 1/1 Running 0 25s app=nginx,pod-template-hash=6f78978d75,tier=frontendThe Pods should have both: app=nginx,tier=frontend
Expose the NGINX Pods through a Service:
$ kubectl expose deployment nginx \
--name=nginx-service \
--port=80 \
--target-port=80 \
-n frontendThe Service is not required for the NetworkPolicy, but it provides a convenient way to access the frontend application.
Deploy the MySQL Pods
Generate a Deployment manifest with 2 MySQL replicas in the database namespace:
$ kubectl create deployment mysql \
--image=mysql:latest \
--replicas=2 \
-n database \
--dry-run=client -o yaml > mysql-deploy.yamlEdit mysql-deploy.yaml to add the required labels and configure the MySQL password.
Apply the Deployment:
$ kubectl apply -f mysql-deploy.yaml
deployment.apps/mysql createdVerify the Pods and labels:
$ kubectl get pods -n database --show-labels
NAME READY STATUS RESTARTS AGE LABELS
mysql-5947bd4f9b-jmfvw 1/1 Running 0 24s app=mysql,pod-template-hash=5947bd4f9b,tier=database
mysql-5947bd4f9b-jnt2d 1/1 Running 0 24s app=mysql,pod-template-hash=5947bd4f9b,tier=databaseBoth Pods should be Running and have:
Expose MySQL on port 3306:
$ kubectl expose deployment mysql \
--name=mysql \
--port=3306 \
--target-port=3306 \
-n databaseVerify the Service:
$ kubectl get service mysql -n database
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql ClusterIP 10.110.219.11 <none> 3306/TCP 16sThe Service DNS name will be: mysql.database.svc.cluster.local
Create the Database NetworkPolicy
The NetworkPolicy should target the MySQL Pods and allow traffic only from Pods labeled tier: frontend in the frontend namespace.
Create a file named database-np.yaml and define the required policy in it.
Apply the policy:
kubectl apply -f database-np.yamlThis creates the NetworkPolicy in the cluster and applies its traffic rules to the selected database Pods.
How the from Rule Works
The namespaceSelector and podSelector are combined, meaning the source Pod must be in the frontend namespace and have the label tier=frontend. This does not allow every Pod in the namespace. The policy allows only TCP traffic on port 3306 to the MySQL Pods.
Deny Other Database Traffic
To explicitly deny all other ingress and egress traffic in the database namespace, create deny-all-np.yaml:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-all
namespace: database
spec:
podSelector: {}
policyTypes:
- Ingress
- EgressApply the policy:
kubectl apply -f deny-all-np.yamlVerify both policies:
$ kubectl get networkpolicy -n database
NAME POD-SELECTOR AGE
db-access-policy app=mysql,tier=database 84s
default-deny-all <none> 40sImportant Note About Multiple NetworkPolicies
NetworkPolicies are additive, meaning they work together rather than overriding one another. The default-deny-all policy isolates all Pods in the database namespace for ingress and egress, while the db-access-policy adds the required exception, allowing frontend Pods to access MySQL on TCP port 3306.
Test From an Allowed Frontend Pod
Exec into one of the NGINX Pods to Test the MySQL Service:
curl -v telnet://mysql.database.svc.cluster.local:3306You may see output containing:
Connected to mysql.database.svc.cluster.local (10.110.219.11) port 3306followed by an error related to the MySQL protocol.
curl mysql.database.svc.cluster.local:3306
curl: (1) Received HTTP/0.9 when not allowedThis is expected because curl is attempting an HTTP-style request while MySQL speaks its own database protocol. The important part for this connectivity test is that the TCP connection to port 3306 succeeds.
Test a Pod With the Wrong Label
Create a temporary BusyBox Pod in the frontend namespace:
$ kubectl run busybox \
--image=busybox \
--namespace=frontend \
--rm -it -- /bin/shInside the Pod, test:
telnet mysql.database.svc.cluster.local 3306This connection should fail because the BusyBox Pod does not have: tier=frontend Exit the Pod when finished.
Test From Another Namespace
Create a temporary BusyBox Pod in the default namespace:
kubectl run busybox \
--image=busybox \
--namespace=default \
--rm -it -- /bin/shThen test:
telnet mysql.database.svc.cluster.local 3306This should also fail because the Pod is not in the frontend namespace.
CKA Exam Tips
These tips will help you quickly identify the important Kubernetes concepts and avoid common mistakes during the exam.
1. NetworkPolicy Is Namespace-Scoped
A NetworkPolicy in the database namespace controls the selected Pods within that namespace.
2. Understand namespaceSelector and podSelector
When namespaceSelector and podSelector are used in the same from item, their conditions are combined with AND.
3. An Empty podSelector Selects All Pods
An empty selector:
podSelector: {}
selects all Pods in the policy's namespace.
4. NetworkPolicies Are Additive
Adding a specific allow policy does not automatically override or cancel another policy. NetworkPolicies are additive.
5. Verify CNI NetworkPolicy Support
Always verify that the cluster's CNI supports NetworkPolicy enforcement. A correctly configured policy will not be effective if the network plugin does not enforce NetworkPolicies.