Yes, your Django application in the frontend namespace can definitely connect to a MySQL database running in the database namespace, as long as you set up proper Kubernetes Services and configure your Django app correctly.
✅ Key Requirements:
- MySQL must be exposed via a Service
- Service must be accessible across namespaces
- Django app must use the correct DNS name to reach MySQL
🔧 Step-by-Step Explanation
1. MySQL Pod & Service in database Namespace
Assume you have something like this:
# mysql-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: mysql
namespace: database
spec:
containers:
- name: mysql
image: mysql:8.0
env:
- name: MYSQL_ROOT_PASSWORD
value: "password"
Then expose it with a Service:
# mysql-service.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: database
spec:
ports:
- port: 3306
protocol: TCP
selector:
name: mysql
This creates a service named mysql.database.svc.cluster.local.
2. Django App in frontend Namespace
Your Django app pod should be able to access that MySQL service using its fully qualified domain name (FQDN):
mysql.database.svc.cluster.local
Example of Django settings (settings.py):
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydatabase',
'USER': 'root',
'PASSWORD': 'password',
'HOST': 'mysql.database.svc.cluster.local', # Service name + namespace
'PORT': '3306',
}
}
✅ This assumes the MySQL service is named mysql and is in the database namespace.
📌 How Kubernetes DNS Works
In Kubernetes, services are resolvable within the cluster using DNS names like:
<service-name>.<namespace>.svc.cluster.local
So from any namespace, you can reach a service in another namespace by referencing it like:
mysql.database.svc.cluster.local
If both are in the same namespace, you can just use the service name: mysql.
🔐 Optional: Network Policies (for security)
If you're using Network Policies, ensure that traffic from the frontend namespace is allowed to reach the database namespace on port 3306.
✅ Summary
| Component | Value |
|---|
| MySQL Namespace | database |
| MySQL Service Name | mysql |
| Accessible at | mysql.database.svc.cluster.local |
| Use in Django DB Settings | Yes, set HOST accordingly |
🚀 TL;DR
Yes, your Django app in the frontend namespace can connect to a MySQL instance in the database namespace if:
- MySQL is exposed via a Service.
- The Django app uses the correct DNS name:
mysql.database.svc.cluster.local.
- No network policies or firewalls block inter-namespace communication.
Let me know if you want example manifests or help with Helm-based deployments too!