How to connect mysql kubernetes container internally with nodejs k8s container?

8/12/2019

I have created mysql k8s container and nodejs k8s container under same namespace.I can't able to connect mysql db.(sequalize)

I have tried to connect using '''http://mysql.e-commerce.svc.cluster.local:3306'''.But i got "SequelizeHostNotFoundError" error.

Here is my service and deployment yaml files.

kind: Service
metadata:
 labels:
   app: mysql
 name: mysql
 namespace: e-commerce
spec:
  type: NodePort
  ports:
  - port: 3306
    targetPort: 3306
    nodePort: 30306
  selector:
    app: mysql
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mysql
  namespace: e-commerce
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.6
        name: mysql-container
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim```
-- Mithun
docker
kubernetes
mysql
node.js
service

4 Answers

8/12/2019

Try with:

kind: Service
metadata:
 labels:
   app: mysql
 name: mysql
 namespace: e-commerce
spec:
  clusterIP: None
  type: ClusterIP
  ports:
  - port: 3306
    targetPort: 3306
  selector:
    app: mysql

with the same connection string.

-- FL3SH
Source: StackOverflow

8/13/2019

As mentioned by community member FL3SH you can change your spec.type to clusterIP. You can reproduce this task using stable helm chart wordpress/mysql.
For newly created pods:

mysql-mariadb-0
mysql-wordpress

and services:

mysql-mariadb
mysql-wordpress

After successfully deployment you can verify if your service is working from the mysql-wordpress pod by running:

kubectl exec -it mysql-wordpress-7cb4958654-tqxm6 -- /bin/bash

In addition, you can install additional tools like nslooukp, telnet:

apt-get update && apt-get install dnsutils telnet

Services and connectivity with db you can test by running f.e. those commands:

nslookup mysql-mariadb
telnet mysql-mariadb 3306
mysql -uroot -hmysql-mariadb -p<your_db_password>

example output:

nslookup mysql-mariadb  
Server:         10.125.0.10
Address:        10.125.0.10#53
Non-authoritative answer:
Name:   mysql-mariadb.default.svc.cluster.local
Address: 10.125.0.76


mysql -u root -hmysql-mariadb -p<your_db_password>
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2068
Server version: 10.1.40-MariaDB Source distribution

You should be able to connect using service name or using ip address. Inside this helm chart you can find also template for statefulset in order to create mysql pods.

Update

From the second pod f.e. ubuntu run this example - Node.js Mysql, install nodes.js and create connection to the database demo_db_connection.js

example:

 var mysql = require('mysql');

    var con = mysql.createConnection({
      host: "mysql-mariadb",
      user: "root",
      password: "yourpassword"
    });

    con.connect(function(err) {
      if (err) throw err;
      console.log("Connected!");
    });

run it:

root@ubuntu:~/test# node demo_db_connection.js
Connected!
-- Hanx
Source: StackOverflow

8/12/2019

You are trying to access database with http protocol, leave it or change with mysql://ip:3306. Some clients won't accept DNS name for databases so you can check ClusterIP of service and try that IP too.

-- Akın Özer
Source: StackOverflow

11/22/2019

From the ClusterIP worked for me or better way to go with the host name of the local cluster service ex. db-mysql.default.svc.cluster.local. This way if you cluster restarts and your IP changes, then you got it covered.

enter image description here

-- Gajen Sunthara
Source: StackOverflow