Kubernetes集群中的访问作业

时间:2019-03-01 16:46:33

标签: node.js docker kubernetes gcloud kubernetes-jobs

我有一些通过脚本旋转的动态作业。作业应尽其所能,但我想公开某种API来说明作业的状态。例如,我有一个小型的NodeJS服务器,该服务器数到300都会死掉。它通过快递路线显示当前计数状态:

const express = require('express');

const app = express();

let status = 0;
const sleep = () => new Promise((resolve) => {
  setTimeout(() => resolve(), 1000);
});


const sleepLoop = async () => {
  for (status; status < 300; status += 1) {
    await sleep();
  }
};

app.get('/status', (req, res) => {
  res.json({
    status,
  });
});

app.listen(8080, () => {
  console.log('Example app listening on port 8080!');
});

sleepLoop();

我的标准Node Dockerfile:

FROM node:latest

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY ./sampleNode/package.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

EXPOSE 8080
# Bundle app source
COPY ./sampleNode .

和我的Kubernetes Config作为JS模块(用于生成脚本):

module.exports = {
  apiVersion: 'batch/v1',
  kind: 'Job',
  metadata: { name: 'example-job' },
  spec:
  {
    template:
    {
      metadata: {
        name: 'example-job',
      },
      spec:
      {
        hostNetwork: true,
        containers:
          [{
            name: 'samplenode',
            image: 'eu.gcr.io/...:latest',
            command: ['/bin/bash', '-c', 'node index.js'],
          }],
        restartPolicy: 'Never',
      },
    },
  },
};

如果我在本地运行它,我当然可以通过localhost:8080 / status检查状态。我该如何在线进行?我在Google Cloud上。

0 个答案:

没有答案