如何设置Kubernetes准备和活跃度探针

时间:2019-11-24 09:21:46

标签: node.js kubernetes minikube

我正在尝试使用readinessprobelivenessprobekubernetes中设置yamlminikube

我的应用已公开并且可以正常运行,但是当我尝试<ip>:<port>/healthz时,它说

  

无法获取/ healthz

这是我的server.js,运行GET请求,检查livenessreadiness

'use strict';

const express = require('express');

// Constants
const PORT = 8080;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) => {
  res.send('Hello world\n');
});

// App
const app = express();
app.get('/healthz', (req, res) => {
  res.send('fine');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

这是我的deployment.yaml文件

apiVersion: apps/v1
kind: Deployment
metadata:
        name: node-web-app
spec:
  replicas: 2
  selector:
    matchLabels:
            name: node-web-app
  template:
    metadata:
      labels:
        # you can specify any labels you want here
              name: node-web-app
    spec:
      containers:
      - name: node-web-app
        # image must be the same as you built before (name:tag)
        image: banuka/node-web-app
        ports:
        - name: http
          containerPort: 8080
          protocol: TCP
        imagePullPolicy: Never
      terminationGracePeriodSeconds: 60
---
apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-http
spec:
  containers:
  - name: liveness
    image: k8s.gcr.io/liveness
    args:
    - /server
    livenessProbe:
      httpGet:
        path: /healthz
        port: 8080
        httpHeaders:
        - name: Custom-Header
          value: Awesome
      initialDelaySeconds: 3
      periodSeconds: 3
---
kind: Service
apiVersion: v1
metadata:
  labels:
    # these labels can be anything
    name: node-web-app-clusterip
  name: node-web-app-clusterip
spec:
  selector:
    name: node-web-app
  ports:
    - protocol: TCP
      port: 80
      # target is the port exposed by your containers (in our example 8080)
      targetPort: 8080
---
kind: Service
apiVersion: v1
metadata:
  labels:
    name: node-server-nodeport
  name: node-server-nodeport
spec:
  # this will make the service a NodePort service
  type: NodePort
  selector:
    name: node-web-app
  ports:
    - protocol: TCP
      # new -> this will be the port used to reach it from outside
      # if not specified, a random port will be used from a specific range (default: 30000-32767)
      nodePort: 32555
      port: 80
      targetPort: 8080

但是它不起作用,如何将livenessprobe吊舱指向我的部署,以便它可以使吊舱/部署状态正常。

1 个答案:

答案 0 :(得分:1)

尝试在您的部署文件的spec字段中放置以下代码段-

  containers:
  - image: banuka/node-web-app
    name: node-web-app
    command: ["node"]
    args: ["server.js"]
    ports:
    - containerPort: 8080
    stdin: true
    tty: true
    livenessProbe:
      httpGet:
        path: /healthz
        port: 8080
      initialDelaySeconds: 10
      periodSeconds: 5
      timeoutSeconds: 2

这将向容器的IP地址发出一个http get请求,如果返回的响应为200或300,则将其标记为成功