簡體   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