首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VirtualService和网关中的“主机”属性是否基于header (第7层)?

VirtualService和网关中的“主机”属性是否基于header (第7层)?
EN

Stack Overflow用户
提问于 2020-11-03 00:28:25
回答 2查看 3.1K关注 0票数 6

如果像下面这样编写网关和VirtualService条目,那么主机属性匹配哪些标准来确定是否应该将传入请求路由到服务?它是HTTP请求中的“主机”头,还是其他什么东西?

代码语言:javascript
复制
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: default-gateway
  namespace: web
spec:
  selector:
    istio: ingressgateway
  servers:
  - hosts:
    - example.com
    port:
      name: www
      number: 80
      protocol: HTTP
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: web
  namespace: web
spec:
  gateways:
  - default-gateway
  hosts:
  - example.com
  http:
  - route:
    - destination:
        host: webserver # assume this is the name of the Service for our deployed container
---
# Assume a Service and Deployment exist for the above Service

另一种方法--如果我们忽略DNS --通过集群/负载均衡器IP可以使用什么“主机”报头来访问服务/部署?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-12-18 20:55:20

回答你的问题

您是说“主机”是基于还是不基于HTTP“主机”头?

是的,它基于http主机头。

根据istio 文档

使用curl访问httpbin服务:

代码语言:javascript
复制
$ curl -s -I -HHost:httpbin.example.com "http://$INGRESS_HOST:$INGRESS_PORT/status/200"
HTTP/1.1 200 OK
server: istio-envoy
...

注意,您使用-H标志来,将HTTP头设置为“httpbin.example.com”。这是必要的,因为您的入口网关被配置为处理“httpbin.example.com”,但是在您的测试环境中,您对该主机没有DNS绑定,只是将请求发送到入口IP。

我使用bookinfo应用进行测试。

代码语言:javascript
复制
kubectl label namespace default istio-injection=enabled
kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml

将虚拟服务hosts*更改为example.com

代码语言:javascript
复制
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: bookinfo-gateway
spec:
  selector:
    istio: ingressgateway # use istio default controller
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: bookinfo
spec:
  hosts:
  - "example.com"
  gateways:
  - bookinfo-gateway
  http:
  - match:
    - uri:
        exact: /productpage
    - uri:
        prefix: /static
    - uri:
        exact: /login
    - uri:
        exact: /logout
    - uri:
        prefix: /api/v1/products
    route:
    - destination:
        host: productpage
        port:
          number: 9080

使用邮递员测试它,您也可以使用上面提到的curl。

example.com主机头->状态200的示例。

代码语言:javascript
复制
curl -v -HHost:example.com xx.xxx.xx.x/productpage
Host:example.com
HTTP/1.1 200 OK

示例使用example2.com主机头->状态404。

代码语言:javascript
复制
curl -v -HHost:example2.com xx.xxx.xx.x/productpage
Host:example2.com
HTTP/1.1 404 Not Found
票数 2
EN

Stack Overflow用户

发布于 2020-11-03 01:41:18

如果可以使用任何dns或集群/负载均衡器IP访问群集,则可以将example.com DNS更改为*。或者其他方法像列表一样将所有的DNSs放到主机块中。

代码语言:javascript
复制
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: default-gateway
  namespace: web
spec:
  selector:
    istio: ingressgateway
  servers:
  - hosts:
    - '*'
    port:
      name: www
      number: 80
      protocol: HTTP
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: web
  namespace: web
spec:
  gateways:
  - default-gateway
  hosts:
  - '*'
  http:
  - route:
    - destination:
        host: webserver # assume this is the name of the Service for our deployed container
---
# Assume a Service and Deployment exist for the above Service

如果只使用一个外部DNS部署多个服务,则需要使用uri块进行匹配,例如,在虚拟服务中可以放置:

代码语言:javascript
复制
apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
      name: web
      namespace: web
    spec:
      gateways:
      - default-gateway
      hosts:
      - '*'
      http:
      - match:
        - uri:
            prefix: /test/service1
        rewrite:
         uri: / 
      - route:
        - destination:
            host: webserver # assume this is the name of the Service for our deployed container

在上面的示例中,您可以从任何主机头访问,但是要匹配到diferentes服务的criterio是http匹配中的uri块。

我希望它对你有用。:)

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64655158

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档