Skip to main content

Services & Networking

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:

  • frontend
  • database

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=frontend

Deploy 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=database

Create 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.

📚
For more details about 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 database

Verify that both namespaces are created:

$ kubectl get namespaces
NAME              STATUS   AGE
database          Active   11s
frontend          Active   18s

You 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.yaml

Edit nginx-deploy.yaml and add the required app=nginx and tier=frontend labels to both the Deployment selector and Pod template.

nginx-deployment.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: nginx namespace: frontend labels: app: nginx tier: frontend spec: replicas: 2 selector: matchLabels: app: nginx tier: frontend template: metadata: labels: app: nginx tier: frontend spec: containers: - name: nginx image: nginx ports: - containerPort: 80

Apply the Deployment:

$ kubectl apply -f nginx-deploy.yaml

deployment.apps/nginx created

Verify:

$ 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=frontend

The 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 frontend

The 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.yaml

Edit mysql-deploy.yaml to add the required labels and configure the MySQL password.

mysql-deployment.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: mysql namespace: database labels: app: mysql tier: database spec: replicas: 2 selector: matchLabels: app: mysql tier: database template: metadata: labels: app: mysql tier: database spec: containers: - name: mysql image: mysql:latest env: - name: MYSQL_ROOT_PASSWORD value: passwd ports: - containerPort: 3306

Apply the Deployment:

$ kubectl apply -f mysql-deploy.yaml

deployment.apps/mysql created

Verify 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=database

Both Pods should be Running and have:

Expose MySQL on port 3306:

$ kubectl expose deployment mysql \
  --name=mysql \
  --port=3306 \
  --target-port=3306 \
  -n database

Verify 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   16s

The 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.

db-access-policy.yaml
apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: db-access-policy namespace: database spec: podSelector: matchLabels: app: mysql tier: database policyTypes: - Ingress ingress: - from: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: frontend podSelector: matchLabels: tier: frontend ports: - protocol: TCP port: 3306

Apply the policy:

kubectl apply -f database-np.yaml

This 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
  - Egress

Apply the policy:

kubectl apply -f deny-all-np.yaml

Verify both policies:

$ kubectl get networkpolicy -n database

NAME               POD-SELECTOR              AGE
db-access-policy   app=mysql,tier=database   84s
default-deny-all   <none>                    40s

Important 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:3306

You may see output containing:

 Connected to mysql.database.svc.cluster.local (10.110.219.11) port 3306

followed by an error related to the MySQL protocol.

curl mysql.database.svc.cluster.local:3306

curl: (1) Received HTTP/0.9 when not allowed

This 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/sh

Inside the Pod, test:

telnet mysql.database.svc.cluster.local 3306

This 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/sh

Then test:

telnet mysql.database.svc.cluster.local 3306

This 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.