istio-ingress-gateway

  1. 在 Kubernetes 环境中,使用 Kubernetes Ingress 资源来指定需要暴露到集群外的服务。 在 Istio 服务网格中,更好的选择(同样适用于 Kubernetes 及其他环境)是使用一种新的配置模型,名为 Istio GatewayGateway 允许应用一些诸如监控和路由规则的 Istio 特性来管理进入集群的流量。

本任务描述了如何配置 Istio,以使用 Istio Gateway 来将服务暴露至服务网格之外。

  1. 如果您启用了 sidecar 自动注入,通过以下命令部署 httpbin 服务:
[root@master istio-1.6.3]# kubectl apply -f samples/httpbin/httpbin.yaml
serviceaccount/httpbin created
service/httpbin created
deployment.apps/httpbin created
[root@master istio-1.6.3]#
  1. 确定 ingress IP 和端口
    执行如下指令,明确自身 Kubernetes 集群环境支持外部负载均衡:
[root@master istio-1.6.3]# kubectl get svc istio-ingressgateway -n istio-system
NAME                   TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)                                                                      AGE
istio-ingressgateway   LoadBalancer   10.101.95.195   <pending>     15020:30335/TCP,80:32709/TCP,443:30416/TCP,31400:31385/TCP,15443:31982/TCP   2d16h
[root@master istio-1.6.3]#

如果 EXTERNAL-IP 值已设置,说明环境正在使用外部负载均衡,可以用其为 ingress gateway 提供服务。 如果 EXTERNAL-IP 值为 <none> (或持续显示 <pending>),说明环境没有提供外部负载均衡,无法使用 ingress gateway。 在这种情况下,你可以使用服务的 node port 访问网关。

  1. 使用 Istio Gateway 配置 ingress
    ingress Gateway 描述运行在网格边界的负载均衡器,负责接收入口 HTTP/TCP 连接。 其中配置了对外暴露的端口、协议等。 但是,不像 Kubernetes Ingress 资源,ingress Gateway 不包含任何流量路由配置。Ingress 流量的路由使用 Istio 路由规则来配置,和内部服务请求完全一样
    让我们一起来看如何为 HTTP 流量在 80 端口上配置 Gateway:
    4.1. 创建 Istio Gateway:
[root@master istio-1.6.3]# kubectl apply -f - <<EOF
> apiVersion: networking.istio.io/v1alpha3
> kind: Gateway
> metadata:
>   name: httpbin-gateway
> spec:
>   selector:
>     istio: ingressgateway # use Istio default gateway implementation
>   servers:
>   - port:
>       number: 80
>       name: http
>       protocol: HTTP
>     hosts:
>     - "httpbin.example.com"
> EOF
gateway.networking.istio.io/httpbin-gateway created
[root@master istio-1.6.3]#

4.2 为通过 Gateway 的入口流量配置路由:

[root@master istio-1.6.3]# kubectl apply -f - <<EOF
> apiVersion: networking.istio.io/v1alpha3
> kind: VirtualService
> metadata:
>   name: httpbin
> spec:
>   hosts:
>   - "httpbin.example.com"
>   gateways:
>   - httpbin-gateway
>   http:
>   - match:
>     - uri:
>         prefix: /status
>     - uri:
>         prefix: /delay
>     route:
>     - destination:
>         port:
>           number: 8000
>         host: httpbin
> EOF
virtualservice.networking.istio.io/httpbin created
[root@master istio-1.6.3]#

已为 httpbin 服务创建了虚拟服务配置,包含两个路由规则,允许流量流向路径 /status/delay

gateways 列表规约了哪些请求允许通过 httpbin-gateway 网关。 所有其他外部请求均被拒绝并返回 404 响应。

  1. 使用 curl 访问 httpbin 服务:
[root@master istio-1.6.3]# curl -I -HHost:httpbin.example.com http://192.168.14.130:32709/status/200        
HTTP/1.1 200 OK
server: istio-envoy
date: Mon, 29 Jun 2020 06:50:15 GMT
content-type: text/html; charset=utf-8
access-control-allow-origin: *
access-control-allow-credentials: true
content-length: 0
x-envoy-upstream-service-time: 100

[root@master istio-1.6.3]#

注意上文命令使用 -H 标识将 HTTP 头部参数 Host 设置为 “httpbin.example.com”。 该操作为必须操作,因为 ingress Gateway 已被配置用来处理 “httpbin.example.com” 的服务请求,而在测试环境中并没有为该主机绑定 DNS 而是简单直接地向 ingress IP 发送请求

  1. 通过浏览器访问 ingress 服务:
    在浏览器中输入 httpbin 服务的 URL 不能获得有效的响应,因为无法像 curl 那样,将请求头部参数 Host 传给浏览器。在现实场景中,这并不是问题,因为你需要合理配置被请求的主机及可解析的 DNS,从而在 URL 中使用主机的域名,譬如:https://httpbin.example.com/status/200
    为了在简单的测试和演示中绕过这个问题,请在 Gateway 和 VirtualService 配置中使用通配符 *。譬如,修改 ingress 配置如下:
[root@master istio-1.6.3]# kubectl apply -f - <<EOF
> apiVersion: networking.istio.io/v1alpha3
> kind: Gateway
> metadata:
>   name: httpbin-gateway
> spec:
>   selector:
>     istio: ingressgateway # use Istio default gateway implementation
>   servers:
>   - port:
>       number: 80
>       name: http
>       protocol: HTTP
>     hosts:
>     - "*"
> ---
> apiVersion: networking.istio.io/v1alpha3
> kind: VirtualService
> metadata:
>   name: httpbin
> spec:
>   hosts:
>   - "*"
>   gateways:
>   - httpbin-gateway
>   http:
>   - match:
>     - uri:
>         prefix: /headers
>     route:
>     - destination:
>         port:
>           number: 8000
>         host: httpbin
> EOF
gateway.networking.istio.io/httpbin-gateway configured
virtualservice.networking.istio.io/httpbin configured
[root@master istio-1.6.3]# 
  1. 此时,便可以在浏览器中输入包含


    image.png
  2. 理解原理:
    Gateway 配置资源允许外部流量进入 Istio 服务网格,并对边界服务实施流量管理和 Istio 可用的策略特性。

事先,在服务网格中创建一个服务并向外部流量暴露该服务的 HTTP 端点

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。