【问题标题】:Right namespace for VirtualService and DestinationRule Istio resourcesVirtualService 和 DestinationRule Istio 资源的正确命名空间
【发布时间】:2021-12-31 23:19:30
【问题描述】:

我试图了解与应定义的命名空间相关的 VirtualService 和 DestinationRule 资源,以及它们是否真的是命名空间资源,或者它们也可以被视为集群范围的资源。

我有以下场景:

  • 前端服务(web-frontend)访问后端服务(客户)。
  • 前端服务部署在前端命名空间中
  • 后端服务(客户)部署在后端命名空间中
  • 后端服务客户有 2 个版本(2 个部署),一个与版本 v1 相关,一个与版本 v2 相关。
  • clusterIP 服务的默认行为是在 2 个部署(v1 和 v2)之间对请求进行负载平衡,我的目标是通过创建 DestinationRule 和 VirtualService 将流量仅定向到部署版本 v1。李>
  • 我想了解的是,定义此类 DestinationRule 和 VirtualService 资源的适当命名空间是哪个。我应该在前端命名空间还是后端命名空间中创建必要的 DestinationRule 和 VirtualService 资源?

在前端命名空间中,我有 web 前端部署和相关服务如下:

apiVersion: v1
kind: Namespace
metadata:
  name: frontend
  labels:
    istio-injection: enabled
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-frontend
  namespace: frontend
  labels:
    app: web-frontend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web-frontend
  template:
    metadata:
      labels:
        app: web-frontend
        version: v1
    spec:
      containers:
        - image: gcr.io/tetratelabs/web-frontend:1.0.0
          imagePullPolicy: Always
          name: web
          ports:
            - containerPort: 8080
          env:
            - name: CUSTOMER_SERVICE_URL
              value: 'http://customers.backend.svc.cluster.local'
---
kind: Service
apiVersion: v1
metadata:
  name: web-frontend
  namespace: frontend
  labels:
    app: web-frontend
spec:
  selector:
    app: web-frontend
  type: NodePort
  ports:
    - port: 80
      name: http
      targetPort: 8080

我已经通过定义以下 Gateway 和 VirtualService 资源来公开 Web 前端服务,如下所示:

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: gateway-all-hosts
  # namespace: default # Also working
  namespace: frontend
spec:
  selector:
    istio: ingressgateway
  servers:
    - port:
        number: 80
        name: http
        protocol: HTTP
      hosts:
        - "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: web-frontend
  # namespace: default # Also working
  namespace: frontend
spec:
  hosts:
    - "*"
  gateways:
    - gateway-all-hosts
  http:
    - route:
        - destination:
            host: web-frontend.frontend.svc.cluster.local
            port:
              number: 80

在后端命名空间中,我有客户 v1 和 v2 部署和相关服务,如下所示:

apiVersion: v1
kind: Namespace
metadata:
  name: backend
  labels:
    istio-injection: enabled
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: customers-v1
  namespace: backend
  labels:
    app: customers
    version: v1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: customers
      version: v1
  template:
    metadata:
      labels:
        app: customers
        version: v1
    spec:
      containers:
        - image: gcr.io/tetratelabs/customers:1.0.0
          imagePullPolicy: Always
          name: svc
          ports:
            - containerPort: 3000
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: customers-v2
  namespace: backend
  labels:
    app: customers
    version: v2
spec:
  replicas: 1
  selector:
    matchLabels:
      app: customers
      version: v2
  template:
    metadata:
      labels:
        app: customers
        version: v2
    spec:
      containers:
        - image: gcr.io/tetratelabs/customers:2.0.0
          imagePullPolicy: Always
          name: svc
          ports:
            - containerPort: 3000
---
kind: Service
apiVersion: v1
metadata:
  name: customers
  namespace: backend
  labels:
    app: customers
spec:
  selector:
    app: customers
  type: NodePort
  ports:
    - port: 80
      name: http
      targetPort: 3000

我创建了以下 DestinationRule 和 VirtualService 资源,仅将流量发送到 v1 部署。

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: customers
  #namespace: default # Not working
  #namespace: frontend # working
  namespace: backend # working
spec:
  host: customers.backend.svc.cluster.local
  subsets:
    - name: v1
      labels:
        version: v1
    - name: v2
      labels:
        version: v2
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: customers
  #namespace: default # Not working
  #namespace: frontend # working
  namespace: backend # working
spec:
  hosts:
    - "customers.backend.svc.cluster.local"
  http:
  ## route  - subset: v1
  - route:
      - destination:
          host: customers.backend.svc.cluster.local
          port:
            number: 80
          subset: v1
  • 问题是哪个命名空间适合为客户服务定义 VR 和 DR 资源?

  • 从我的测试中,我发现我可以使用前端命名空间或后端命名空间。为什么可以在前端命名空间或后端命名空间中创建 VR、DR 并且在这两种情况下都可以工作?哪个是正确的?

  • DestinationRule 和 VirtualService 资源真的是命名空间资源还是可以被视为集群范围的资源? 无论命名空间如何,代理的低级路由规则是否传播到所有特使代理?

【问题讨论】:

    标签: kubernetes istio


    【解决方案1】:

    在请求期间实际应用的 DestinationRule 需要位于目标规则查找路径上:

    -> client namespace
    -> service namespace
    -> the configured meshconfig.rootNamespace namespace (istio-system by default)
    

    在您的示例中,“web-frontend”客户端位于 frontend 命名空间(web-frontend.frontend.svc.cluster.local),“customers”服务位于 backend 命名空间( customers.backend.svc.cluster.local),因此应在以下命名空间之一中创建 customers DestinationRule:frontendbackendistio-system。此外,请注意,不建议使用 istio-system 命名空间,除非目标规则确实是适用于所有命名空间的全局配置。

    为确保应用目标规则,我们可以对 web-frontend Pod 使用 istioctl proxy-config cluster 命令:

    $ istioctl proxy-config cluster web-frontend-69d6c79786-vkdv8 -n frontend | grep "customers.backend.svc.cluster.local"
    SERVICE FQDN                                            PORT      SUBSET     DESTINATION RULE
    customers.backend.svc.cluster.local                     80        -          customers.frontend
    customers.backend.svc.cluster.local                     80        v1         customers.frontend
    customers.backend.svc.cluster.local                     80        v2         customers.frontend
    

    default 命名空间中创建目标规则时,将不会在请求期间应用:

    $ istioctl proxy-config cluster web-frontend-69d6c79786-vkdv8 -n frontend | grep "customers.backend.svc.cluster.local"
    SERVICE FQDN                                            PORT      SUBSET    DESTINATION RULE
    customers.backend.svc.cluster.local                     80        -        
    

    有关详细信息,请参阅Control configuration sharing in namespaces 文档。

    【讨论】:

    • 完全正确。 @matt_j 非常感谢。
    猜你喜欢
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 2013-04-12
    • 2012-06-12
    • 1970-01-01
    相关资源
    最近更新 更多