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.x.x.x 并将其输入到 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 地址,所以我使用了它的名称:)