【问题标题】:Want to specify rules in VirtualService file where two or more services have same rules想要在两个或多个服务具有相同规则的 VirtualService 文件中指定规则
【发布时间】:2020-05-06 14:15:29
【问题描述】:

我已经通过 Istio sidecar 注入在 Kubernetes 上部署了 8 个服务。 我想在三个服务具有相同规则的 VirtualService 中设置路由规则。 规则:-

  - match:
    - headers:
        location:
          exact: pune
      uri:
        prefix: /wagholi
    route:
    - destination:
        host: wagholi
        port:
          number: 8080
      uri:
        prefix: /yerwada
    route:
    - destination:
        host: yerwada
        port:
          number: 8080
      uri:
        prefix: /hadapsar
    route:
    - destination:
        host: hadapsar
        port:
          number: 8080
  - match:
    - headers:
        location: 
          exact: mumbai
      uri:
        prefix: /chatraparishivajiterminal
    route:
    - destination:
        host: chatraparishivajiterminal
        port:
          number: 8080
      uri:
        prefix: /kalyan
    route:
    - destination:
        host: kalyan
        port:
          number: 8080
  - match:
    - headers:
        location: 
          exact: Pimpari
      uri:
        prefix: /akurdi
    route:
    - destination:
        host: akurdi
        port:
          number: 8080
      uri:
        prefix: /ravet
    route:
    - destination:
        host: ravet
        port:
          number: 8080

在这种情况下,如果我在标头中设置位置 pune,然后尝试调用名为 wagholi 的服务,它应该调用 wagholi,如果我点击 hadapsar,然后点击 hadapsar。 如果我将位置设置为 Pimpri 并调用 ravet 那么它应该只调用 ravet。 有没有什么场景可以这样做!!!!!!

【问题讨论】:

    标签: kubernetes routing kubernetes-ingress istio gateway


    【解决方案1】:

    我用 3 个简单的 nginx pod 复制了您的问题,问题是我无法仅使用 1 个标头和 3 个 uri。

    所以我换了一种方式,每个匹配的虚拟服务都有自己的标头和 uri。

    查看以下示例。

    我们有 3 个 pod -> 3 个服务 -> virtual service -> gateway -> ingressgateway

    部署 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"]
    

    部署 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"]
    

    部署 3

    piVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx3
    spec:
      selector:
        matchLabels:
          run: nginx3
      replicas: 1
      template:
        metadata:
          labels:
            run: nginx3
            app: frontend3
        spec:
          containers:
          - name: nginx3
            image: nginx
            ports:
            - containerPort: 80
            lifecycle:
              postStart:
                exec:
                  command: ["/bin/sh", "-c", "echo Hello nginx3 > /usr/share/nginx/html/index.html"]
    

    服务 1

    apiVersion: v1
    kind: Service
    metadata:
      name: nginx
      labels:
        app: frontend
    spec:
      ports:
      - name: http
        port: 80
        protocol: TCP
      selector:
        app: frontend
    

    服务 2

    apiVersion: v1
    kind: Service
    metadata:
      name: nginx2
      labels:
        app: frontend2
    spec:
      ports:
      - port: 80
        protocol: TCP
      selector:
        app: frontend2
    

    服务 3

    apiVersion: v1
    kind: Service
    metadata:
      name: nginx3
      labels:
        app: frontend3
    spec:
      ports:
      - port: 80
        protocol: TCP
      selector:
        app: frontend3
    

    虚拟服务

    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
      name: nginxvirt
    spec:
      gateways:
      - mesh # traffic inside cluster
      - comp-ingress-gateway # traffic from outside cluster
      hosts:
      - nginx.default.svc.cluster.local
      - nginx.com # outside cluster
      - nginx3.default.svc.cluster.local
      - nginx2.default.svc.cluster.local
      http:
      - name: a
        match:
        - uri:
            prefix: /wagholi
          headers:
            location:
              exact: pune
        rewrite:
          uri: /
        route:
        - destination:
            host: nginx.default.svc.cluster.local
            port:
              number: 80
      - name: b
        match:
        - uri:
            prefix: /yerwada
          headers:
            location:
              exact: pune
        rewrite:
          uri: /
        route:
        - destination:
            host: nginx2.default.svc.cluster.local
            port:
              number: 80
      - name: c
        match:
        - uri:
           prefix: /hadasapar
          headers:
            location:
              exact: pune
        rewrite:
          uri: /
        route:
        - destination:
            host: nginx3.default.svc.cluster.local
            port:
              number: 80
    

    网关

    apiVersion: networking.istio.io/v1alpha3
    kind: Gateway
    metadata:
      name: comp-ingress-gateway
    spec:
      selector:
        istio: ingressgateway
      servers:
      - hosts:
        - '*'
        port:
          name: http
          number: 80
          protocol: HTTP
      - hosts:
        - '*'
        port:
          name: https
          number: 443
          protocol: HTTPS
        tls:
          mode: SIMPLE
          privateKey: /etc/istio/ingressgateway-certs/tls.key
          serverCertificate: /etc/istio/ingressgateway-certs/tls.crt
    

    一些用于测试内部流量的 ubuntu pod

    apiVersion: v1
    kind: Pod
    metadata:
      name: ubu1
    spec:
      containers:
      - name: ubu1
        image: ubuntu
        command: ["/bin/sh"]
        args: ["-c", "apt-get update && apt-get install curl -y && sleep 3000"]
    

    结果:

    内部

    root@ubu1:/# curl -H "location: pune" nginx/wagholi
    Hello nginx1
    root@ubu1:/# curl -H "location: pune" nginx/hadasapar
    Hello nginx3
    root@ubu1:/# curl -H "location: pune" nginx/yerwada  
    Hello nginx2
    

    外面

    curl -H "location: pune" -H "host: nginx.com" ingress_gateway_ip/hadasapar
    Hello nginx3
    curl -H "location: pune" -H "host: nginx.com" ingress_gateway_ip/wagholi
    Hello nginx1
    curl -H "location: pune" -H "host: nginx.com" ingress_gateway_ip/yerwada
    Hello nginx2
    

    编辑

    如何找到 ingress_gateway_ip?

    你可以使用

    kubectl get svc istio-ingressgateway -n istio-system
    

    它是istio-ingressgateway EXTERNAL-IP

    如果设置了 EXTERNAL-IP 值,则您的环境具有可用于入口网关的外部负载平衡器。如果 EXTERNAL-IP 值为(或永久),则您的环境不为入口网关提供外部负载均衡器。在这种情况下,您可以使用服务的node port 访问网关。


    我可以为每个服务做不同的 Ingress,然后映射到 VirtualService。

    我不确定,但我认为这是可能的。检查以下链接

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

    【讨论】:

    • 如何找到ingress_gateway_ip
    • 我可以为每个服务做不同的Ingress,然后映射到VirtualService。
    • 如何从浏览器访问它
    猜你喜欢
    • 2014-04-23
    • 2011-08-14
    • 2014-12-08
    • 1970-01-01
    • 2012-07-11
    • 2014-10-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多