如何使用 Istio 创建内部网关?

cjs*_*ook 5 kubernetes istio

目前,我们成功地设置了 Istio 来创建一对入口网关,如 api.example.com 和 app.example.com,将流量路由到具有目标规则等的各种服务。除此之外,我们很想使用Istio 仅用于内部 API 的功能,但我们不确定如何设置类似的东西。是否可以在不退出集群的情况下使用 Istio 的网关和 VirtualServices CRD 来路由流量?如果是这样,我们将如何进行设置?

jt9*_*t97 3

我会在 Arghya Sadhu 的回答中添加一些内容。

我认为我在另一篇文章中的示例可以回答您的问题,特别是虚拟服务网关和主机。此示例需要额外的目标规则,因为我们有一些子集,这些子集标记了此处到 nginx 正确子集的路由,并且它们是在目标规则中定义的。

因此,举个例子,我会调用诸如内部网关/a 或内部网关/b 之类的东西,它们将被路由到服务 A 或 B

我做了类似的东西

2 个 nginx pod -> 2 个服务 -> 虚拟服务

部署1

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx1
spec:
  selector:
    matchLabels:
      run: nginx1
  replicas: 1
  template:
    metadata:
      labels:
        run: nginx1
        app: frontend
    spec:
      containers:
      - name: nginx1
        image: nginx
        ports:
        - containerPort: 80
        lifecycle:
          postStart:
            exec:
              command: ["/bin/sh", "-c", "echo Hello nginx1 > /usr/share/nginx/html/index.html"]
Run Code Online (Sandbox Code Playgroud)

部署2

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx2
spec:
  selector:
    matchLabels:
      run: nginx2
  replicas: 1
  template:
    metadata:
      labels:
        run: nginx2
        app: frontend2
    spec:
      containers:
      - name: nginx2
        image: nginx
        ports:
        - containerPort: 80
        lifecycle:
          postStart:
            exec:
              command: ["/bin/sh", "-c", "echo Hello nginx2 > /usr/share/nginx/html/index.html"]
Run Code Online (Sandbox Code Playgroud)

服务1

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: frontend
spec:
  ports:
  - port: 80
    protocol: TCP
  selector:
    app: frontend
Run Code Online (Sandbox Code Playgroud)

服务2

apiVersion: v1
kind: Service
metadata:
  name: nginx2
  labels:
    app: frontend2
spec:
  ports:
  - port: 80
    protocol: TCP
  selector:
    app: frontend2
Run Code Online (Sandbox Code Playgroud)

虚拟服务

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: nginxvirt
spec:
  hosts:
  - nginx.default.svc.cluster.local
  - nginx2.default.svc.cluster.local
  http:
  - name: a
    match:
    - uri:
        prefix: /a
    rewrite:
      uri: /
    route:
    - destination:
        host: nginx.default.svc.cluster.local
        port:
          number: 80
  - name: b
    match:
    - uri:
        prefix: /b
    rewrite:
      uri: /
    route:
    - destination:
        host: nginx2.default.svc.cluster.local
        port:
          number: 80
Run Code Online (Sandbox Code Playgroud)

上述虚拟服务仅在网状网关内部工作。

您有 2 个 nginx 服务的 2 个匹配项。

root@ubu1:/# curl nginx/a
Hello nginx1

root@ubu1:/# curl nginx/b
Hello nginx2
Run Code Online (Sandbox Code Playgroud)

我建议检查 istio 文档并阅读以下内容:

和 istio 示例:

这样我就可以编造一个实际上并不存在的 DNS 名称或 IP 地址

我想你误解了,它一定存在,但不在网格中。例如,某些不在网格中的数据库,但您仍然可以使用服务条目将其连接到网格。

istio 文档和整个外部服务文档中有维基百科的示例。

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