如何测试在kubernetes集群上部署为pod的nodejs应用程序?

时间:2018-06-15 07:26:20

标签: node.js docker kubernetes dockerfile

我开发了一个nodejs REST API,为此我构建了一个docker镜像,现在将其部署为我的kubernetes集群上的pod。

Dockerfile

FROM mhart/alpine-node:8
WORKDIR /home/appHome/
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "npm", "start" ]

然而,我正在通过npm start作为我的容器启动命令。

但我如何使用npm测试测试我的节点js应用程序并验证测试结果?

当我在我的本地系统中执行此操作时,我曾经在后端运行应用程序并用于运行

npm test

在另一个窗口中。 如何在kubernetes集群上部署它时实现相同的目标!并在成功后删除测试部分!

下面是我的package.json脚本,

"scripts": {
"test": "mocha ./test/scheduledTaskTest.js",
"start": "nodemon app.js --exec babel-node --presets es2015,stage-2",
"cmd": "set NODE_ENV=devConfig&& npm start"
},

以下是部署和作业yaml文件。

Deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: scheduled-task-test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: scheduled-task-test
  template:
    metadata:
      labels:
        app: scheduled-task-test
    spec:     # pod spec       
      containers:
      - name: st-c1
        image: 104466/st
        imagePullPolicy: Always
        env: 
        - name: NODE_ENV
          value: devConfig
      - name: st-c2
        image: 104466/st
        imagePullPolicy: Always
        command: [ 'sh','-c','npm test && sleep 3600']'

TestJob.yaml

apiVersion: batch/v1
kind: Job
metadata:
  name: scheduled-task-test-job
  labels:
    purpose: test
spec:
  template:
    spec:
      containers:
      - name: st-c1
        image: 104466/st
        imagePullPolicy: Always
        env: 
        - name: NODE_ENV
          value: devConfig
      - name: st-c2
        image: 104466/st
        imagePullPolicy: Always
        command: [ 'sh','-c','npm test && sleep 3600']
      restartPolicy: Never

Pod状态

scheduled-task-test-7d86c87d44-q9zdv                              2/2       Running   1          8m        100.96.9.87   ip-172-20-34-139.us-west-2.compute.internal
scheduled-task-test-job-gt9hx                                     1/2       Error     0          7m        100.96.9.88   ip-172-20-34-139.us-west-2.compute.internal

成功执行测试用例pod日志(部署结果)

kubectl logs scheduled-task-test-7d86c87d44-q9zdv -c st-c2

> ScheduledTask@1.0.0 test /home/appHome/ScheduledTask
> mocha ./test/scheduledTaskTest.js



  Testing listTask api
    ✓ List all the Tasks (435ms)

  Testing addTask api
    ✓  Add a new task  (438ms)

  Testing updateTask api
    ✓  Update task

  Testing delete task api
    ✓  Delete task  (434ms)


  4 passing (1s)

测试失败的日志,(在作业中执行容器)

kubectl logs scheduled-task-test-job-gt9hx -c st-c2

> ScheduledTask@1.0.0 test /home/appHome/ScheduledTask
> mocha ./test/scheduledTaskTest.js



  Testing listTask api
    1) List all the Tasks

  Testing addTask api
    2)  Add a new task

  Testing updateTask api
    3)  Update task

  Testing delete task api
    4)  Delete task


  0 passing (61ms)
  4 failing

  1) Testing listTask api
       List all the Tasks:
     Uncaught TypeError: Cannot read property 'body' of undefined
      at Request._callback (test/scheduledTaskTest.js:24:29)
      at self.callback (node_modules/request/request.js:186:22)
      at Request.onRequestError (node_modules/request/request.js:878:8)
      at Socket.socketErrorListener (_http_client.js:387:9)
      at emitErrorNT (internal/streams/destroy.js:64:8)
      at _combinedTickCallback (internal/process/next_tick.js:138:11)
      at process._tickCallback (internal/process/next_tick.js:180:9)

  2) Testing addTask api
        Add a new task :
     Uncaught TypeError: Cannot read property 'body' of undefined
      at Request._callback (test/scheduledTaskTest.js:43:20)
      at self.callback (node_modules/request/request.js:186:22)
      at Request.onRequestError (node_modules/request/request.js:878:8)
      at Socket.socketErrorListener (_http_client.js:387:9)
      at emitErrorNT (internal/streams/destroy.js:64:8)
      at _combinedTickCallback (internal/process/next_tick.js:138:11)
      at process._tickCallback (internal/process/next_tick.js:180:9)

  3) Testing updateTask api
        Update task :
     Uncaught TypeError: Cannot read property 'body' of undefined
      at Request._callback (test/scheduledTaskTest.js:63:20)
      at self.callback (node_modules/request/request.js:186:22)
      at Request.onRequestError (node_modules/request/request.js:878:8)
      at Socket.socketErrorListener (_http_client.js:387:9)
      at emitErrorNT (internal/streams/destroy.js:64:8)
      at _combinedTickCallback (internal/process/next_tick.js:138:11)
      at process._tickCallback (internal/process/next_tick.js:180:9)

  4) Testing delete task api
        Delete task :
     Uncaught TypeError: Cannot read property 'body' of undefined
      at Request._callback (test/scheduledTaskTest.js:83:20)
      at self.callback (node_modules/request/request.js:186:22)
      at Request.onRequestError (node_modules/request/request.js:878:8)
      at Socket.socketErrorListener (_http_client.js:387:9)
      at emitErrorNT (internal/streams/destroy.js:64:8)
      at _combinedTickCallback (internal/process/next_tick.js:138:11)
      at process._tickCallback (internal/process/next_tick.js:180:9)



npm ERR! Test failed.  See above for more details.

描述Pod(部署为作业的容器)的日志

kubectl describe pod scheduled-task-test-job-gt9hx
Name:           scheduled-task-test-job-gt9hx
Namespace:      default
Node:           ip-172-20-34-139.us-west-2.compute.internal/172.20.34.139
Start Time:     Tue, 19 Jun 2018 16:28:06 +0000
Labels:         controller-uid=bf5569e5-73dd-11e8-8ede-02924b27b126
                job-name=scheduled-task-test-job
Annotations:    kubernetes.io/created-by={"kind":"SerializedReference","apiVersion":"v1","reference":{"kind":"Job","namespace":"default","name":"scheduled-task-test-job","uid":"bf5569e5-73dd-11e8-8ede-02924b27b126","...
                kubernetes.io/limit-ranger=LimitRanger plugin set: cpu request for container st-c1; cpu request for container st-c2
Status:         Running
IP:             100.96.9.88
Created By:     Job/scheduled-task-test-job
Controlled By:  Job/scheduled-task-test-job
Containers:
  st-c1:
    Container ID:   docker://7fc17e717921020cec074774ccb87956979712e7de55480be6e6bd586b28ce6d
    Image:          104466/st
    Image ID:       docker-pullable://104466/st@sha256:91414cc48e86975041c233329b1814d7c6d2b76bc839a24fa68e99f92750390f
    Port:           <none>
    State:          Running
      Started:      Tue, 19 Jun 2018 16:28:08 +0000
    Ready:          True
    Restart Count:  0
    Requests:
      cpu:  100m
    Environment:
      NODE_ENV:  devConfig
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-2tvsm (ro)
  st-c2:
    Container ID:  docker://95837799d34e1e14f7718acc1dc745042b94a33f115aab11a03a25ab4a569c18
    Image:         104466/st
    Image ID:      docker-pullable://104466/st@sha256:91414cc48e86975041c233329b1814d7c6d2b76bc839a24fa68e99f92750390f
    Port:          <none>
    Command:
      sh
      -c
      npm test && sleep 3600
    State:          Terminated
      Reason:       Error
      Exit Code:    1
      Started:      Tue, 19 Jun 2018 16:28:10 +0000
      Finished:     Tue, 19 Jun 2018 16:28:12 +0000
    Ready:          False
    Restart Count:  0
    Requests:
      cpu:        100m
    Environment:  <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-2tvsm (ro)
Conditions:
  Type           Status
  Initialized    True
  Ready          False
  PodScheduled   True
Volumes:
  default-token-2tvsm:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-2tvsm
    Optional:    false
QoS Class:       Burstable
Node-Selectors:  <none>
Tolerations:     node.alpha.kubernetes.io/notReady:NoExecute for 300s
                 node.alpha.kubernetes.io/unreachable:NoExecute for 300s
Events:          <none>

2 个答案:

答案 0 :(得分:1)

只需在deployment.yaml的容器部分指定'command:[“npm”,“test”]'即可。

答案 1 :(得分:1)

如果您只想使用Kubernetes,那么使用Jobs将是最简单的方法。作业创建一个或多个pod,并确保指定数量的pod成功终止。随着pod成功完成,该作业跟踪成功完成。达到指定数量的成功完成后,作业本身就完成了。

粗略计划:

<强> 1。为您的申请创建一份工作。
您可以将应用程序的部署用作模板。您需要更改kind: Job并添加spec.containers.command: ["npm", "test"],最后一个将替换Dockerfile中定义的CMD [ "npm", "start" ]。这是一个例子:

apiVersion: batch/v1
kind: Job
metadata:
  name: npm-test-job
  labels:
    purpose: test
spec:
  template:
    spec:
      containers:
      - name: npm-test-job
        image: <your-image>
        command: ["npm", "test"]
      restartPolicy: Never

<强> 2。运行工作
运行作业并等待它完成:

kubectl create -f npm-test-job

第3。检查作业的状态
检查作业的状态,例如:

kubectl describe jobs kubectl describe jobs | grep "Pods Statuses"

<强> 4。运行部署
如果测试成功完成,您可以开始部署:

kubectl create -f npm-deployment

当然,您需要自动执行此过程。因此,您需要开发一个实现此逻辑的脚本。

但我的建议是:最好使用CI工具,例如Jenkins。它可以轻松地与Kubernetes集成,并为此类案例提供大量功能。