繁体   English   中英

NodeJS 应用程序识别集群 IP 但不与 pod 本身通信

[英]NodeJS app recognize cluster IP but not communicate with the pod himself

我有在 K8S 中运行的 Nodejs 应用程序(在 hyperv 中运行的 Minikube),该应用程序连接到数据库。

这个数据库也在 k8S 中作为 MySQL 运行。

Nodejs 数据库连接

var mysql = require('mysql');
var connection = mysql.createConnection({
    host: "mysql" ,//work only if you get the ip of the mysql running pod ! need to exec the pod the get the ip
    user:'root',
    password:'root',
    database:'crud'
});
connection.connect(function(error){
    if(!!error) {
        console.log(error);
    } else {
        console.log('Connected..!');
    }
});

module.exports = connection;

如您所见,我使用"host: "mysql" ,nodejs 日志告诉我 NodeJs 应用程序识别它是什么”mysql“dns 名称,因为我有它的 clusterIP 服务

  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '10.102.140.203',
  port: 3306,
  fatal: true

我在日志中看到的这个 IP 地址 (10.102.140.203) 是 MySQL 服务的 CluserIP 地址

NAME          TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
app           ClusterIP   10.111.211.9     <none>        3000/TCP         62m
appnodeport   NodePort    10.99.179.176    <none>        3000:30958/TCP   62m
kubernetes    ClusterIP   10.96.0.1        <none>        443/TCP          71m
mysql         ClusterIP   10.102.140.203   <none>        3306/TCP         62m
mysqlnode     NodePort    10.106.157.92    <none>        3306:31186/TCP   57m

但是如果我自己输入 SQL 的 pod 并获取 IP 地址 172.xxx 并将其输入到 NodeJS 数据库连接

host: "172.x.x.x.x" 
user:'root',
password:'root',
database:'crud'

NodeJS 应用程序连接正常,但我不想以这种方式执行此操作,因为如果 Pod 重新启动,Pod 本身的 IP 有时会更改,我想使用服务名称或未更改的名称。

NodeJS 部署 Yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: C:\Users\xx\Desktop\xx\kompose.exe convert
    kompose.version: 1.24.0 (7c629530)
  creationTimestamp: null
  labels:
    io.kompose.service: app
  name: app
spec:
  replicas: 1
  selector:
    matchLabels:
      io.kompose.service: app
  strategy: {}
  template:
    metadata:
      annotations:
        kompose.cmd: C:\Users\xx\Desktop\xx\kompose.exe convert
        kompose.version: 1.24.0 (7c629530)
      creationTimestamp: null
      labels:
        io.kompose.service: app
    spec:
      containers:
        - image: xxx/app:6
          name: employees-app
          ports:
            - containerPort: 3000
          imagePullPolicy: Always
          resources: {}
      hostname: app
      restartPolicy: Always
status: {}

NodeJS 服务 yaml

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: C:\Users\xxx\Desktop\xxx\kompose.exe convert
    kompose.version: 1.24.0 (7c629530)
  creationTimestamp: null
  labels:
    io.kompose.service: app
  name: app
spec:
  ports:
    - name: "3000"
      port: 3000
      targetPort: 3000
  selector:
    io.kompose.service: app
status:
  loadBalancer: {}

MySQL部署yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        env:
          # Use secret in real usage
        - name: MYSQL_ROOT_PASSWORD
          value: "root"
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - mountPath: '/var/lib/mysql'
          name: mysql-persistent-storage
      hostname: mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim

MySQL 服务

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: C:\Users\xxx\Desktop\xxx\kompose.exe convert
    kompose.version: 1.24.0 (7c629530)
  creationTimestamp: null
  labels:
    io.kompose.service: mysql
  name: mysql
spec:
  ports:
    - name: "3306"
      port: 3306
      targetPort: 3306
  selector:
    io.kompose.service: mysql
status:
  loadBalancer: {}

找到了一个解决方案,只是使用了“端点”端点,它是正在运行的 pod 的 IP 地址,所以我使用了它的名称:)

如果在定义 mysql-service.yaml 时使用 ClusterIP: none,它也会起作用。 这会将端点公开为端点而不是 VirtualIP。

暂无
暂无

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

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