【问题标题】:VirtualService hosts and minikubeVirtualService 主机和 minikube
【发布时间】:2021-05-27 18:54:57
【问题描述】:

this项目中,假设我将hosts的值设置为hello

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: hello-k8s
spec:
  hosts:
    - ???
  http:
    - name: hello-k8s
      match:
        - uri:
            prefix: /
      route:
        - destination:
            host: hello               # <--------------
            port:
              number: 8080

将项目部署到我本地的 minikube 后:

  • 我应该将请求发送到哪个域名? hello.local?
$ curl what.should.be.here:8080
  • 有没有办法使用kubectl 和自省来找出它?

更新 1:

我把主机改成了hello.local

然后我验证istiominikube中启用:

➜ minikube addons list
|-----------------------------|----------|--------------|
|         ADDON NAME          | PROFILE  |    STATUS    |
|-----------------------------|----------|--------------|
| ambassador                  | minikube | disabled     |
| csi-hostpath-driver         | minikube | disabled     |
| dashboard                   | minikube | disabled     |
| default-storageclass        | minikube | enabled ✓    |
| efk                         | minikube | disabled     |
| freshpod                    | minikube | disabled     |
| gcp-auth                    | minikube | disabled     |
| gvisor                      | minikube | disabled     |
| helm-tiller                 | minikube | disabled     |
| ingress                     | minikube | disabled     |
| ingress-dns                 | minikube | disabled     |
| istio                       | minikube | enabled ✓    |
| istio-provisioner           | minikube | enabled ✓    |
| kubevirt                    | minikube | disabled     |
| logviewer                   | minikube | disabled     |
| metallb                     | minikube | disabled     |
| metrics-server              | minikube | disabled     |
| nvidia-driver-installer     | minikube | disabled     |
| nvidia-gpu-device-plugin    | minikube | disabled     |
| olm                         | minikube | disabled     |
| pod-security-policy         | minikube | disabled     |
| registry                    | minikube | disabled     |
| registry-aliases            | minikube | disabled     |
| registry-creds              | minikube | disabled     |
| storage-provisioner         | minikube | enabled ✓    |
| storage-provisioner-gluster | minikube | disabled     |
| volumesnapshots             | minikube | disabled     |
|-----------------------------|----------|--------------|

我部署了应用程序并验证一切正常:

➜ kubectl apply -k base/
service/hello-k8s unchanged
deployment.apps/hello-k8s unchanged
virtualservice.networking.istio.io/hello-k8s configured

➜ kubectl get deployments/hello-k8s
NAME        READY   UP-TO-DATE   AVAILABLE   AGE
hello-k8s   1/1     1            1           20h

➜ kubectl get pods
NAME                         READY   STATUS    RESTARTS   AGE
hello-k8s-6d9cc7d655-plzs8   1/1     Running   0          20h

➜ kubectl get service/hello-k8s
NAME        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
hello-k8s   ClusterIP   10.98.144.226   <none>        8080/TCP   21h

➜ kubectl get virtualservice/hello-k8s
NAME        GATEWAYS   HOSTS             AGE
hello-k8s              ["hello.local"]   20h

➜ kubectl get pods -n istio-system
NAME                                    READY   STATUS    RESTARTS   AGE
istio-ingressgateway-8577c95547-6c9sk   1/1     Running   0          21h
istiod-6ccd677dc7-7cvr2                 1/1     Running   0          21h
prometheus-7767dfd55-x2pv6              2/2     Running   0          21h

不知道为什么我必须这样做,但显然应该这样做:

➜ kubectl label namespace default istio-injection=enabled --overwrite
namespace/default labeled

➜ kubectl get namespace -L istio-injection
NAME              STATUS   AGE   ISTIO-INJECTION
default           Active   21h   enabled
istio-operator    Active   21h
istio-system      Active   21h   disabled
kube-node-lease   Active   21h
kube-public       Active   21h
kube-system       Active   21h
playground        Active   21h

我检查了minikube 的IP,并在/etc/hosts 中添加了hello.local 的条目:

➜  minikube ip
192.168.49.2

➜ tail -n 3  /etc/hosts

# Minikube
192.168.49.2    hello.local

然后我根据this blog post查询istio-ingressgateway的端口:

➜ kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].nodePort}'
30616

最后我向hello.local:30616 发送了一个请求,但请求没有到达我的应用程序:

➜ curl -iv hello.local:30616/hello
*   Trying 192.168.49.2:30616...
* TCP_NODELAY set
* Connected to hello.local (192.168.49.2) port 30616 (#0)
> GET /hello HTTP/1.1
> Host: hello.local:30616
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 404 Not Found
HTTP/1.1 404 Not Found
< date: Thu, 25 Feb 2021 14:51:18 GMT
date: Thu, 25 Feb 2021 14:51:18 GMT
< server: istio-envoy
server: istio-envoy
< content-length: 0
content-length: 0

<
* Connection #0 to host hello.local left intact

【问题讨论】:

  • 你好@Behrang,你的问题还没有解决吗?

标签: kubernetes istio minikube


【解决方案1】:

我应该将请求发送到哪个域名? hello.local?

答案是您可以使用任何您想要的域,但您需要将此域添加到/etc/hosts。那么/etc/hosts域将是istio gateway loadbalancer的IP地址。 多亏了它,您将能够使用该域而不是入口网关 ip

关于虚拟服务中的主机/主机有几点需要考虑:

如果您将虚拟服务 hosts 值设置为 hello,那么根据文档,Istio 将根据规则的命名空间而不是服务来解释短名称。 default 命名空间中主机为hello 的规则将被解释为hello.default.svc.cluster.local

您还可以在主机中沿域引用服务。

因此,为避免潜在的错误配置,建议始终使用完全限定域名而不是短名称。

现在在destination.host旁边:

服务注册表中的服务名称。服务名称是 从平台的服务注册表(例如 Kubernetes 服务、Consul 服务等)以及来自声明的主机 ServiceEntry。 转发到在任何一个中都找不到的目的地的流量 二,将被丢弃。

这里还有一个good exmpale 如何在 minikube 中将 metallb 配置为负载均衡器和 istio。拥有 metallb 将使您的服务自动配置为负载均衡器。


在进一步检查您的情况后,我认为该问题可能与您的 gateway 根本没有被使用有关:

➜ kubectl get virtualservice/hello-k8s
NAME        GATEWAYS   HOSTS             AGE
hello-k8s              ["hello.local"]   20h ## Gateway describes a load balancer operating at the edge of the mesh receiving incoming or outgoing HTTP/TCP connections.

“网关描述了在网格边缘运行的负载均衡器 接收传入或传出的 HTTP/TCP 连接。使用 Istio 您可以通过网关配置绑定 VirtualService 具有特定网关的 virtualservice.spec.gateways(允许外部 istio 服务网格内部的流量或内部流量)“保留 词网格用于暗示网格中的所有边车。当这 字段被省略,将使用默认网关(网格)” https://istio.io/latest/docs/reference/config/networking/virtual-service/#VirtualService

确保您配置了GatewayVirtualService

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: my-gateway
spec:
  selector:
    istio: ingressgateway  ### this should be verified with istio ingress gateway pod labels
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*"   ### for specific hosts it can be f.e. hello.local
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: hello-k8s
spec:
  hosts:
    - hello.local
  gateways:
  - my-gateway
  http:
    - match:
        - uri:
            prefix: /
      route:
        - destination:
            host: hello-k8s.default.svc.cluster.local 
            port:
              number: 8080

【讨论】:

  • 我仍然无法完成这项工作。我会在一秒钟内发布更新。
  • 您的route.destination 不正确。当您引用 hello 时,您的 k8s 服务名称是 hello-k8s。此外,在示例中,您提供的应用程序似乎正在收听/hello。这需要您将uri.prefix 更改为/hello
  • 应用程序同时响应//hellogithub.com/behrangsa/hello-k8s/blob/master/cmd/api/…
  • @βξhrαng 我已经编辑了我的答案。可以看看吗?
猜你喜欢
  • 2021-11-19
  • 1970-01-01
  • 2020-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-22
  • 1970-01-01
  • 2021-07-06
相关资源
最近更新 更多