Django not able to connect to MySQL pod over both cluster IP and Nodeport in AKS cluster

1/29/2020

i have a Kubernetes cluster on Azure, in that i have created MySQL pod and Django pod. I have exposed MySQL pod as service and trying to connect from Django. I have given service name in settings.py file in Django and tried to run "runserver" then i am getting below error.

OperationalError: (2003, "Can't connect to MySQL server on 'mysqlserv' (111)").

If i try to connect with "mysql" command then it is working. Below is the command i used.

"mysql -h mysqlerv -u root -p".

But surprisingly, if i give Mysql pod IP in /etc/hosts file(like 10.144.1.5 mysqlserv) of Django pod then "runserver" is working.

Can someone help me to connect Django pod with mysql pod over service (cluster IP)..

Below is my sql-server yaml file.

apiVersion: v1 
kind: Pod
metadata:
  name: sqlpod
  labels:
    app: demo
    env: test
spec:
  containers:
    - name: sqlapp-tester-custom
      image: mydockerregistry.com/mysql:5.7
      env:
        - name: SQL_HOST_NAME
          value: sqlapp-tester-custom
        - name: MYSQL_DATABASE
          value: myapp1_db
        - name: MYSQL_ROOT_PASSWORD
          value: myownPassword123
      ports:
        - name: sqlport
          containerPort: 3306
          protocol: TCP
      volumeMounts:
        - name: azurefile
          mountPath: /sql_data
  volumes:
    - name: azurefile
      azureFile:
        secretName: myazure-secret-name
        shareName: my_azure_file_share_name
        readOnly: false
-- Paramesh
azure-aks
kubernetes

1 Answer

1/30/2020

clusterip will change if pod restart or recreate it's better to use service name.

Kubernetes Services : https://kubernetes.io/docs/concepts/services-networking/service/

you can check mysql running example : https://kubernetes.io/docs/tasks/run-application/run-single-instance-stateful-application/

example service yaml:

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306
  selector:
    app: mysql
  clusterIP: None

I do recommend to check this example once Mysql with wordpress: https://kubernetes.io/docs/tutorials/stateful-application/mysql-wordpress-persistent-volume/

-- Harsh Manvar
Source: StackOverflow