简体   繁体   English

如何在内部将 mysql kubernetes 容器与 nodejs k8s 容器连接?

[英]How to connect mysql kubernetes container internally with nodejs k8s container?

I have created mysql k8s container and nodejs k8s container under same namespace.I can't able to connect mysql db.(sequalize)我在同一个命名空间下创建了 mysql k8s 容器和 nodejs k8s 容器。我无法连接 mysql db。(sequalize)

I have tried to connect using ''' http://mysql.e-commerce.svc.cluster.local:3306 '''.But i got "SequelizeHostNotFoundError" error.我尝试使用 ''' http://mysql.e-commerce.svc.cluster.local:3306 ''' 进行连接。但是我收到了“SequelizeHostNotFoundError”错误。

Here is my service and deployment yaml files.这是我的服务和部署 yaml 文件。

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```

You are trying to access database with http protocol, leave it or change with mysql://ip:3306.您正在尝试使用 http 协议访问数据库,保留它或更改为 mysql://ip:3306。 Some clients won't accept DNS name for databases so you can check ClusterIP of service and try that IP too.有些客户端不接受数据库的 DNS 名称,因此您可以检查服务的 ClusterIP 并尝试该 IP。

As mentioned by community member FL3SH you can change your spec.type to clusterIP .正如社区成员FL3SH所提到的,您可以将您的spec.type更改为clusterIP You can reproduce this task using stable helm chart wordpress/mysql .您可以使用稳定的掌舵图表wordpress/mysql重现此任务。
For newly created pods:对于新创建的 pod:

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:成功部署后,您可以通过运行以下命令来验证您的服务是否在mysql-wordpress pod 中工作:

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

In addition, you can install additional tools like nslooukp, telnet:此外,您还可以安装其他工具,例如 nslooukp、telnet:

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

Services and connectivity with db you can test by running fe those commands:您可以通过运行这些命令来测试服务和与 db 的连接:

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.您应该能够使用服务名称或使用 IP 地址进行连接。 Inside this helm chart you can find also template for statefulset in order to create mysql pods.在这个掌舵图中,您还可以找到 statefulset 的模板,以便创建 mysql pod。

Update更新

From the second pod fe ubuntu run this example - Node.js Mysql , install nodes.js and create connection to the database demo_db_connection.js从第二个 pod fe ubuntu 运行这个例子 - Node.js Mysql ,安装 node.js 并创建到数据库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!

From the ClusterIP worked for me or better way to go with the host name of the local cluster service ex.从 ClusterIP 为我工作或更好的方式去使用本地集群服务的主机名 ex。 db-mysql.default.svc.cluster.local . db-mysql.default.svc.cluster.local This way if you cluster restarts and your IP changes, then you got it covered.这样,如果您的集群重新启动并且您的 IP 发生变化,那么您就可以解决它。

在此处输入图片说明

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.使用相同的连接字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM