3

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```
2
  • please share with your results - did try the solution below? Commented Aug 16, 2019 at 11:57
  • Hi Mithun, It would be nice to upvote/accept the most useful answer in the thread. stackoverflow.com/help/someone-answers Commented Aug 20, 2019 at 10:47

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

can you please point to a doc explaining about this db-mysql.default.svc.cluster.local more in detail?
1

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.

Comments

1

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!

Comments

0

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.

7 Comments

Are you sure that connection url is correct - I mean the syntax? Try with just service name so url will be `mysql:3306'
Curl command inside nodejs container also not helps.Its in KIND(kubernetes in Docker)
when I tried the wget command inside nodejs container.--> Connecting to mysql:3306 (10.244.0.4:3306) wget: bad header line: 5.6.45
Now try to install mysql client in nodejs container and connect to the database.
Before I used node:10-alpine image as base image of my node cont..Now I changed to node:10.14.2..I got 5.6.45o%cHzQf/??Q.1Kf>Aju.!Zmysql_native_password!??#08S01Got packets out of order when i put curl sql string.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.