Istio 流量拦截理论及调试分析-Istio商业环境实战

1,574 阅读3分钟

专注于大数据及容器云核心技术解密,如有任何学术交流,可随时联系。更多内容请关注《数据云技术社区》公众号,或请转发邮件至1120746959@qq.com

1 Istio 常用运维技巧

  • pilot-discovery 为数据面(运行在 sidecar 中的 Envoy 等 proxy 组件)提供控制信息服务,也就是所谓的 discovery service 或者 xds 服务。这里的 x 是一个代词,类似云计算里的 XaaS 可以指代 IaaS、PaaS、SaaS 等。

  • 在 Istio 中,xds 包括 cds(cluster discovery service)、

  • lds(listener discovery service)、

  • rds(route discovery service)、

  • eds(endpoint discovery service),

  • 而 ads(aggregated discovery service) 是对这些服务的一个统一封装。

获取 ingress IP 地址:
$ export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].nodePort}')
$ export SECURE_INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="https")].nodePort}')
$ export INGRESS_HOST=$(kubectl get po -l istio=ingressgateway -n istio-system -o 'jsonpath={.items[0].status.hostIP}')

服务网关设置:
kubectl get configmap istio -n istio-system -o yaml | sed 's/mode: ALLOW_ANY/mode: REGISTRY_ONLY/g'|kubectl replace -n istio-system -f -
kubectl get configmap istio -n istio-system -o yaml | sed 's/mode: REGISTRY_ONLY/mode: ALLOW_ANY/g'|kubectl replace -n istio-system -f -

手动注入代理:
istioctl kube-inject -f nginx-istio-test.yaml | kubectl apply -f - -n jiuxi


hub配置:

{
   "registry-mirrors": ["http://10.180.210.37","http://10.180.210.196","https://docker.mirrors.ustc.edu.cn"],
   "insecure-registries": ["10.180.210.37","10.180.210.196","docker.mirrors.ustc.edu.cn"]
}

sudo systemctl daemon-reload
sudo systemctl restart docker

Envoy 是 Istio 数据面的核心组件,已Sidecar的模式同应用部署在一个Pod中。
下图展示了Envoy配置流程:
Pilot-agent负责Envoy生命周期管理。它根据启动参数和K8S API Server中的配置信息生成Envoy的初始配置文件envoy-rev0.json,该文件告诉Envoy从xDS server中获取动态配置信息,并配置了xDS server的地址信息,即控制面的Pilot。
Pilot-agent使用envoy-rev0.json启动Envoy进程。
Envoy根据初始配置获得Pilot地址,采用xDS接口从Pilot获取到Listener,Cluster,Route等动态配置信息。
Envoy根据获取到的动态配置启动Listener,并根据Listener的配置,结合Route和Cluster对拦截到的流量进行处理。

Envoy通过xDS API进行动态配置。xDS是一类发现服务的总称,包含LDS、RDS、CDS、EDS及SDS。

LDS:Listener发现服务。Listener监听器控制Envoy启动端口监听(目前只支持TCP),并配置 L3或L4层过滤器,在网络连接到达后,由网络过滤器堆栈开始处理。
RDS:Route发现服务,用于Envoy HTTP连接管理器动态获取路由配置。路由配置包含HTTP头修改(增加、删除HTTP头键值)、Virtual Hosts(虚拟主机)及Virtual Hosts定义的各个路由条目。
CDS:Cluster发现服务,用于动态获取Cluster信息。Envoy Cluster管理器管理着所有的上游Cluster。Envoy从Listener(针对TCP协议)或Route(针对HTTP)中抽象出上游Cluster,作为流量转发目标。
EDS:Endpoint发现服务。对于每个Cluster,Envoy通过EDS API动态获取Endpoint。
SDS:Secret发现服务,用于在运行时动态获取TLS证书。

2 Istio 流量改写原理分析

2.1 Istio 流量拦截理论

2.2 Istio inbound 与outbound流量转移

3 Envoy

配置了一个 envoy实例,监听 127.0.0.1:10000,支持 http 协议访问,http 访问域名为:http://example.com。接收到的所有http流量,
转发给 127.0.0.2:1234 的服务。这个例子中 some_service 这个cluster中 hosts 是固定的(127.0.0.2:1234),不利于扩展。
admin:
  access_log_path: /tmp/admin_access.log
  address:
    socket_address: { address: 127.0.0.1, port_value: 9901 }

static_resources:
  listeners:
  - name: listener_0
    address:
      socket_address: { address: 127.0.0.1, port_value: 10000 }
    filter_chains:
    - filters:
      - name: envoy.http_connection_manager
        config:
          stat_prefix: ingress_http
          route_config:
            name: local_route
            virtual_hosts:
            - name: local_service
              domains: ["example.com"]
              routes:
              - match: { prefix: "/" }
                route: { cluster: some_service }
          http_filters:
          - name: envoy.router
  clusters:
  - name: some_service
    connect_timeout: 0.25s
    type: STATIC
    lb_policy: ROUND_ROBIN
    hosts: [{ socket_address: { address: 127.0.0.2, port_value: 1234 }}]

admin:
  access_log_path: /tmp/admin_access.log
  address:
    socket_address: { address: 127.0.0.1, port_value: 9901 }

dynamic_resources:
  lds_config:
    api_config_source:
      api_type: GRPC
      cluster_names: [xds_cluster]
  cds_config:
    api_config_source:
      api_type: GRPC
      cluster_names: [xds_cluster]

static_resources:
  clusters:
  - name: xds_cluster
    connect_timeout: 0.25s
    type: STATIC
    lb_policy: ROUND_ROBIN
    http2_protocol_options: {}
    hosts: [{ socket_address: { address: 127.0.0.3, port_value: 5678 }}]
    
LDS服务的响应格式如下:
version_info: "0"
resources:
- "@type": type.googleapis.com/envoy.api.v2.Listener
  name: listener_0
  address:
    socket_address:
      address: 127.0.0.1
      port_value: 10000
  filter_chains:
  - filters:
    - name: envoy.http_connection_manager
      config:
        stat_prefix: ingress_http
        codec_type: AUTO
        rds:
          route_config_name: local_route
          config_source:
            api_config_source:
              api_type: GRPC
              cluster_names: [xds_cluster]
        http_filters:
        - name: envoy.router
        
RDS 服务的响应格式如下:        
version_info: "0"
resources:
- "@type": type.googleapis.com/envoy.api.v2.RouteConfiguration
  name: local_route
  virtual_hosts:
  - name: local_service
    domains: ["*"]
    routes:
    - match: { prefix: "/" }
      route: { cluster: some_service }
      
CDS 服务的响应如下:  
version_info: "0"
resources:
- "@type": type.googleapis.com/envoy.api.v2.Cluster
  name: some_service
  connect_timeout: 0.25s
  lb_policy: ROUND_ROBIN
  type: EDS
  eds_cluster_config:
    eds_config:
      api_config_source:
        api_type: GRPC
        cluster_names: [xds_cluster]
        
EDS 服务的响应如下:
version_info: "0"
resources:
- "@type": type.googleapis.com/envoy.api.v2.ClusterLoadAssignment
  cluster_name: some_service
  endpoints:
  - lb_endpoints:
    - endpoint:
        address:
          socket_address:
            address: 127.0.0.2
            port_value: 1234
            
https://www.jianshu.com/p/73b8ba769274 Envoy 学习笔记之创建 EDS 动态配置
https://www.jianshu.com/p/90f9ee98ce70  Envoy 实践
https://www.jianshu.com/p/ac9b99b37cd1

专注于大数据及容器云核心技术解密,如有任何学术交流,可随时联系。更多内容请关注《数据云技术社区》公众号,或请转发邮件至1120746959@qq.com