如何解决VirtualService 主机和 minikube
在 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
。
然后我验证了在 istio
中启用了 minikube
:
➜ 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
解决方法
我应该将请求发送到哪个域名? hello.local
?
答案是您可以使用任何您想要的域,但您需要在 /etc/hosts
中添加此域。那么/etc/hosts
域将是istio gateway loadbalancer的IP地址。
多亏了它,您将能够使用该域而不是入口网关 ip
继续讨论虚拟服务中的主机/主机,需要考虑以下几点:
如果您将虚拟服务 hosts
值设置为仅 hello
,那么根据文档,Istio 将根据规则的命名空间而不是服务来解释短名称。 default
命名空间中主机为 hello
的规则将被解释为 hello.default.svc.cluster.local
。
您还可以在主机中沿域引用服务。
因此,为了避免潜在的错误配置,建议始终使用完全限定域名而不是短名称。
现在靠近destination.host
:
来自服务注册中心的服务名称。服务名称是 从平台的服务注册表(例如 Kubernetes 服务、领事服务等)和来自声明的主机 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 服务网格内的流量或内部流量)“保留 词mesh用于暗示mesh中的所有sidecar。当这 省略字段,将使用默认网关(网状网络)” https://istio.io/latest/docs/reference/config/networking/virtual-service/#VirtualService
确保您配置了 Gateway
和 VirtualService
:
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。