【问题标题】:Istio Inject Delay VirtualServiceIstio 注入延迟虚拟服务
【发布时间】:2020-01-13 21:43:08
【问题描述】:

我的架构很简单。服务:web > datastore

我想要做的就是向数据存储区的任何请求注入 5 秒延迟,因此我创建了一个 VirtualService 来执行此操作:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: datastore-virtualservice
spec:
  hosts:
  - datastore
  http:
  - fault:
      delay:
        percentage:
          value: 1
        fixedDelay: 5s
    route:
    - destination:
        host: datastore

如果有帮助,请提供一些其他信息:

kubectl get services
NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)     AGE
datastore        ClusterIP   10.106.54.111    <none>        27017/TCP   23h
kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP     2d18h
web              ClusterIP   10.106.162.190   <none>        3000/TCP    23h

YAML 文件

web-deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.16.0 (0c01309)
  creationTimestamp: null
  labels:
    io.kompose.service: web
  name: web
spec:
  replicas: 1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: web
    spec:
      containers:
      - image: adamgardnerdt/customnode
        name: web
        ports:
        - containerPort: 3000
        resources: {}
      restartPolicy: Always
status: {}

web-service.yaml

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.16.0 (0c01309)
  creationTimestamp: null
  labels:
    io.kompose.service: web
  name: web
spec:
  ports:
  - name: "3000"
    port: 3000
    targetPort: 3000
  selector:
    io.kompose.service: web
status:
  loadBalancer: {}

datastore-deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.16.0 (0c01309)
  creationTimestamp: null
  labels:
    io.kompose.service: datastore
  name: datastore
spec:
  replicas: 1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: datastore
    spec:
      containers:
      - image: adamgardnerdt/custommongo
        name: datastore
        ports:
        - containerPort: 27017
        resources: {}
      restartPolicy: Always
status: {}

datastore-service.yaml

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.16.0 (0c01309)
  creationTimestamp: null
  labels:
    io.kompose.service: datastore
  name: datastore
spec:
  ports:
  - name: "27017"
    port: 27017
    targetPort: 27017
  selector:
    io.kompose.service: datastore
status:
  loadBalancer: {}

入口网关和虚拟服务

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: quotations-gateway
spec:
  selector:
    istio: ingressgateway # use Istio default gateway implementation
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: quotations-virtualservice
spec:
  hosts:
  - "*"
  gateways:
  - quotations-gateway
  http:
  - match:
    - uri:
        exact: /
    rewrite:
      uri: /
    route:
    - destination:
        host: web
        port:
          number: 3000

编辑

我的web 服务是一个基于nodeJS 的网络服务器。 我已经用httpd 服务器尝试过这个,延迟工作正常。 因此这与节点的异步性质有关。

我的困惑越来越大。不管 nodeJS 的异步特性如何,页面只在renderPage() 函数内呈现给用户。所以...

1) db.collection('quotes').find().toArray((error, quotes)) 被执行。

2) Istio 看到呼叫从 web 转到 datastore 并将其延迟 X 秒。

3) 调用成功,最后调用renderPage

4) 在 Istio 引起的延迟之后呈现页面(使用 DB 内容)。

实际上发生的是页面(带有 DB 内容)正在立即呈现。我不知道这是如何/为什么会发生...


我的web 图像的代码:

const express = require('express');
const filesystem = require('fs');
const app = express();

var MongoClient = require('mongodb').MongoClient;
var MongoURL = "mongodb://datastore:27017/";

var defaultQuotes = [{ "author": "no quotes", "content": "No content. Is MongoDB down?"}];

function renderPage(error, quotes, res) {
  res.render(__dirname + '/index.ejs', { quotes: quotes });
}

app.set('view engine', 'ejs');

app.listen(3000, function() {
  console.log('listening on 3000')
});

app.get('/', (req, res) => {
  // Attempt a connection to MongoDB
  MongoClient.connect(MongoURL, function(err, client) {
    console.log('connected to MongoDB');
    try {
      var db = client.db('quotesDB');
      // Get all quotes and then call the renderPage function.
    db.collection('quotes').find().toArray((error, quotes) => renderPage(error, quotes, res));

    // Close the connection to Mongo
    client.close();
    }
    catch (e) {
      console.log('exception caught');
      renderPage(null, defaultQuotes, res);
    }
  });

});

【问题讨论】:

  • 延迟没有发生?是这个问题吗?
  • 是的,没错。
  • 你如何测试和验证延迟?
  • 点击127.0.0.1 ?页面和 MongoDB 结果立即返回。这些 docker 图像是公开的,因此您应该能够重新创建
  • 你的意思是所有从集群外到网络的请求都应该延迟5s?

标签: node.js kubernetes istio


【解决方案1】:

就我在 istio 中的检查而言

故障: 描述:在客户端应用于 HTTP 流量的故障注入策略。

就像 istio 文档中提供的 here 一样,您可以在调用 web 时添加延迟,但由于它是 tcp 连接,我无法找到使其在 web 和数据存储之间工作的方法,我找不到任何关于在文档中,只有 http 那个。

因此,如果您想延迟调用您的网站,这里是 yaml

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: quotations-virtualservice
spec:
  hosts:
  - "*"
  gateways:
  - quotations-gateway
  http:
  - fault:
      delay:
        fixedDelay: 5s
        percentage:
          value: 100
    route:
    - destination:
        host: web
        port:
          number: 3000

上面的链接中有一个叫做“硬编码连接超时”的东西

reviews:v2 服务对评级服务的调用有 10 秒的硬编码连接超时

也许这就是您正在寻找的东西,所以您实际上应该在部署方面进行更改,而不是 istio。

希望对你有帮助。如果您还有其他问题,请告诉我。

【讨论】:

    猜你喜欢
    • 2021-09-21
    • 2019-07-19
    • 1970-01-01
    • 2021-06-09
    • 2019-08-12
    • 1970-01-01
    • 2020-07-04
    • 2021-12-12
    • 2020-06-30
    相关资源
    最近更新 更多