Kubernetes pod无法在同一节点内建立连接,错误代码为`getaddrinfo ENOTFOUND`

mwkjh3gx  于 2023-08-03  发布在  Kubernetes
关注(0)|答案(1)|浏览(113)

我有以下基于微服务的node.js程序:posts负责创建帖子并通过axiosPOST请求向event bus发送相应的事件。event bus还可以在启动时通过axios GET请求与posts建立连接。从event buspostsGET请求成功,但从postsevent busPOST请求失败,错误为getaddrinfo ENOTFOUND event-bus-srvpostsevent bus都运行在同一个节点中由kubernetes管理的Pod中。维斯URL http://localhost:31218/,只有posts可供用户使用。当我使用postman发送到端点http://localhost:31218/posts时,我成功地接收到代码201,但事件从未转发到event bus。我的问题是:什么是导致getaddrinfo ENOTFOUND event-bus-srv和我如何修复它?
相关代码:

  1. posts index.js
    1.事件总线index.js
    1.事件总线KUBERNETES部署文件
    1.发布Kubernetes部署文件
    1.发布KUBERNETES节点端口文件
_____________________________________POSTS___________________________________________
const express = require('express');
const { randomBytes } = require('crypto');
const bodyParser = require('body-parser');
const cors = require('cors');
const axios = require('axios');

const app = express();
app.use(cors());
app.use(bodyParser.json());

const posts = {};

app.get('/posts', (req, res) => {
  res.send(posts);
});

app.post('/posts', async(req, res) => {
  const id = randomBytes(4).toString('hex');
  const { title } = req.body;
  posts[id] = {
    id, title
  };

  await axios.post('http://event-bus-srv:4005/events', {
      type: 'PostCreated',
      data: {
        id, title
      }
  }).catch((err) => {
    console.log("сбой такой: " + err.message);
  });

  res.status(201).send(posts[id]);
});

app.post('/events', (req, res) => {
  res.status(200);
})

app.listen(4000, () => {
  console.log('Listening to port ' + 4000);
});

_____________________________________EVENT BUS___________________________________________
const express = require('express');
const bodyParser = require('body-parser');
const axios = require('axios');

const app = express();
app.use(bodyParser.json());

const events = [];

app.get('/events', (req, res) => {
    res.send(events);
});

app.post('/events', (req, res) => {
    const event = req.body;

    events.push(event);

    axios.post('http://posts-clusterip-svr:4000/events', event).catch((err) => {
        console.log(err.message);
      });

    res.send({status: 'OK'});
});

app.listen(4005, async() => {
    console.log("listening to " + 4005);
    const res = await axios.get('http://posts-clusterip-svr:4000/posts').catch((err) => {
      console.log("сбой такой: " + err.message);
    });
});

___________EVENT BUS KUBERNETES DEPLOYMENT/SERVICE FILE_________________________________

apiVersion: apps/v1
kind: Deployment
metadata: 
  name: event-bus-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: event-bus
  template:
    metadata:
      labels:
        app: event-bus
    spec:
      containers:
        - name: event-bus
          image: wliamwhite/event-bus:latest
---
apiVersion: v1
kind: Service
metadata:
  name: event-bus-svr
spec:
  selector:
    app: event-bus
  type: ClusterIP
  ports:
    - name: event-bus
      protocol: TCP
      port: 4005
      targetPort: 4005

___________POSTS KUBERNETES DEPLOYMENT/SERVICE FILE_____________________________________
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: posts-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: posts
  template:
    metadata:
      labels:
        app: posts
    spec:
      containers:
        - name: posts
          image: wliamwhite/posts:latest
---
apiVersion: v1
kind: Service
metadata:
  name: posts-clusterip-svr
spec:
  type: ClusterIP
  selector:
    app: posts
  ports:
    - name: posts
      protocol: TCP
      port: 4000
      targetPort: 4000

___________POSTS KUBERNETES NODEPORT FILE_________________________________________
apiVersion: v1
kind: Service
metadata:
  name: posts-svr
spec:
  type: NodePort
  selector:
    app: posts
  ports:
    - name: posts
      protocol: TCP
      port: 4000
      targetPort: 4000

字符串

eblbsuwk

eblbsuwk1#

除了event-bus-srv和event-bus-svr名称混淆之外。您收到的错误消息通常表示DNS解析从主机名失败。
对此问题需要考虑的事项:
1.就像@DavidMaze提到的那样,你需要仔细检查主机名并确保它拼写正确。
1.还要仔细检查使用主机名的配置文件或代码。
1.确保DNS服务器配置正确。您可以执行ping或nslookup命令进行测试。
1.确保在DNS记录中正确配置主机名并指向正确的IP地址
1.检查可能阻止主机名连接的防火墙规则,并确保打开必要的端口。

相关问题