istio流量劫持總結(jié)

envoy 啟動流程

數(shù)據(jù)面組件啟動流程

  1. initContainer執(zhí)行初始化腳本偏螺,為Pod添加iptables規(guī)則
  2. Pilot-agent根據(jù)啟動參數(shù)和K8S API Server中的配置信息生成Envoy的初始配置文件envoy-rev0.json唧躲,該文件告訴Envoy從xDS server中獲取動態(tài)配置信息,并配置了xDS server的地址信息帖渠,即控制面的Pilot艺谆。
  3. Pilot-agent使用envoy-rev0.json啟動Envoy進程棵红。
  4. Envoy根據(jù)初始配置獲得Pilot地址胀糜,采用xDS接口從Pilot獲取到Listener,Cluster缕坎,Route等d動態(tài)配置信息怖侦。
  5. Envoy根據(jù)獲取到的動態(tài)配置啟動Listener,并根據(jù)Listener的配置谜叹,結(jié)合Route和Cluster對攔截到的流量進行處理匾寝。

istio劫持流量總流程

進流量:
downstream -> iptables -> envoy Inbound -> app
出流量:
app -> iptables -> envoy Outbound -> upstream

image.png

1. IPTABLES 劫持轉(zhuǎn)發(fā)流量的過程

  1. 啟動流程中有講到第一步是envoy執(zhí)行初始化腳本,添加iptables規(guī)則
    參考文章
    istio sidecar中創(chuàng)建iptables流程

2. envoy Inbound 和 Outbound 流程

istio 的監(jiān)聽端口

$ nsenter -t 20533 -n netstat -nl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:15090           0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:15000         0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:15001           0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:15006           0.0.0.0:*               LISTEN
tcp6       0      0 :::15020                :::*                    LISTEN
tcp6       0      0 :::8889                 :::*                    LISTEN

由上圖可以看到envoy有多個端口在監(jiān)聽
15000 端口: 管理端口
15001 端口:Outbound流量重定向端口
15006 端口:Inbound流量重定向端口
15020 端口:健康檢查端口
15090 端口: 普羅米修斯端口

應(yīng)用端口:8899

envoy Inbound處理流量過程

Inbound 流程是將 iptables 攔截到的 downstream 的流量轉(zhuǎn)交給 localhost荷腊,與 Pod 內(nèi)的應(yīng)用程序容器建立連接旗吁。

下面以進入calculator的流量為例子,來分析一下Inbound流程

calculator Inbound流程圖

calculator

Inbound配置

  1. listener
  • virtual listener
{
    "version_info": "2020-02-11T01:52:11Z/5",
    "listener": {
        "name": "virtualInbound",
        "address": {
            "socket_address": {
                "address": "0.0.0.0",
                "port_value": 15006
            }
        },
        "filter_chains": [{
            "filter_chain_match": {
                "prefix_ranges": [{
                    "address_prefix": "::0",
                    "prefix_len": 0
                }]
            },
            "filters": [{
                "name": "envoy.tcp_proxy",
                "typed_config": {
                    "@type": "type.googleapis.com/envoy.config.filter.network.tcp_proxy.v2.TcpProxy",
                    "stat_prefix": "InboundPassthroughClusterIpv6",
                    "cluster": "InboundPassthroughClusterIpv6"
                }
            }],
            "filter_chain_match": {
                "prefix_ranges": [{
                    "address_prefix": "192.xxx.xx.163",
                    "prefix_len": 32
                }],
                "destination_port": 8888
            },
            "filters": [{
                "name": "envoy.http_connection_manager",
                "typed_config": {
                    "@type": "type.googleapis.com/envoy.config.filter.network.http_connection_manager.v2.HttpConnectionManager",
                    "stat_prefix": "inbound_192.xxx.xx.163_8888",
                    "http_filters": [{
                            "name": "istio_authn"
                        },
                        {
                            "name": "mixer"
                        },
                        {
                            "name": "envoy.router"
                        }
                    ],
                    "route_config": {
                        "name": "inbound|8888|grpc|calculator.vm.svc.cluster.local",
                        "virtual_hosts": [{
                            "name": "inbound|http|8888",
                            "domains": [
                                "*"
                            ],
                            "routes": [{
                                "match": {
                                    "prefix": "/"
                                },
                                "decorator": {
                                    "operation": "calculator.vm.svc.cluster.local:8888/*"
                                },
                                "name": "default",
                                "route": {
                                    "timeout": "0s",
                                    "max_grpc_timeout": "0s",
                                    "cluster": "inbound|8888|grpc|calculator.vm.svc.cluster.local"
                                }
                            }]
                        }],
                        "validate_clusters": false
                    }
                }
            }]
        }]
    }
}
  • 該Listener中第三個filterchain用于處理Calculator服務(wù)的入向請求停局。
  • 該filterchain的匹配條件為Calculator服務(wù)的 IP和8888端口很钓,配置了一個http_connection_manager filter
  • http_connection_manager 中又嵌入了istio_auth,Mixer董栽,envoy.router等http filter码倦,經(jīng)過這些filter進行處理后,請求最終將被轉(zhuǎn)發(fā)給""cluster": "inbound|8888|grpc|calculator.vm.svc.cluster.local""

接下來我們看下inbound cluster的配置

  1. cluster
  • inbound cluster
    {
     "version_info": "2020-02-10T08:16:25Z/3",
     "cluster": {
      "name": "inbound|8888|grpc|calculator.vm.svc.cluster.local",
      "type": "STATIC",
      "connect_timeout": "10s",
      "circuit_breakers": {
       "thresholds": [
        {
         "max_connections": 4294967295,
         "max_pending_requests": 4294967295,
         "max_requests": 4294967295,
         "max_retries": 4294967295
        }
       ]
      },
      "http2_protocol_options": {
       "max_concurrent_streams": 1073741824
      },
      "load_assignment": {
       "cluster_name": "inbound|8888|grpc|calculator.vm.svc.cluster.local",
       "endpoints": [
        {
         "lb_endpoints": [
          {
           "endpoint": {
            "address": {
             "socket_address": {
              "address": "127.0.0.1",
              "port_value": 8888
             }
            }
           }
          }
         ]
        }
       ]
      }
     }
    }
  • 這個Inbound Cluster锭碳,由于該Inbound Cluster中配置的Upstream為127.0.0.1:8888(?如何查看其upstream)
  • 由于iptable設(shè)置中127.0.0.1不會被攔截,該請求將發(fā)送到Calculator服務(wù)的8888端口上進行業(yè)務(wù)處理袁稽。

Inbound流程代碼

//todo

envoy Outbound處理流量過程

webapp outbound流程圖

webapp

outbound配置

  • virtual outbound istener
{
    "listener": {
        "name": "virtualOutbound",
        "address": {
            "socket_address": {
                "address": "0.0.0.0",
                "port_value": 15001
            }
        },
        "filter_chains": [{
                "filter_chain_match": {
                    "prefix_ranges": [{
                        "address_prefix": "10.244.2.73",
                        "prefix_len": 32
                    }]
                },
                "filters": [{
                    "name": "envoy.tcp_proxy",
                    "typed_config": {
                        "@type": "type.googleapis.com/envoy.config.filter.network.tcp_proxy.v2.TcpProxy",
                        "stat_prefix": "BlackHoleCluster",
                        "cluster": "BlackHoleCluster"
                    }
                }]
            },
            {
                "filters": [{
                        "name": "mixer"
                    },
                    {
                        "name": "envoy.tcp_proxy",
                        "typed_config": {
                            "@type": "type.googleapis.com/envoy.config.filter.network.tcp_proxy.v2.TcpProxy",
                            "stat_prefix": "PassthroughCluster",
                            "cluster": "PassthroughCluster"
                        }
                    }
                ]
            }
        ],
        "use_original_dst": true
    }
}

改listener的最后一行有個配置 "use_original_dst": true,表示用原有地址擒抛,不進行路由推汽,這樣就會轉(zhuǎn)發(fā)到0.0.0.0_8888這個監(jiān)聽器上

下面看一下0.0.0.0_8888這個監(jiān)聽器的配置

  • outbound listener
{
    "listener": {
        "name": "0.0.0.0_8888",
        "address": {
            "socket_address": {
                "address": "0.0.0.0",
                "port_value": 8888
            }
        },
        "filter_chains": [{
                "filter_chain_match": {
                    "prefix_ranges": [{
                        "address_prefix": "10.244.2.73",
                        "prefix_len": 32
                    }]
                },
                "filters": [{
                    "name": "envoy.tcp_proxy",
                    "typed_config": {
                        "@type": "type.googleapis.com/envoy.config.filter.network.tcp_proxy.v2.TcpProxy",
                        "stat_prefix": "BlackHoleCluster",
                        "cluster": "BlackHoleCluster"
                    }
                }]
            },
            {
                "filters": [{
                    "name": "envoy.http_connection_manager",
                    "typed_config": {
                        "@type": "type.googleapis.com/envoy.config.filter.network.http_connection_manager.v2.HttpConnectionManager",
                        "stat_prefix": "outbound_0.0.0.0_8888",
                        "http_filters": [{
                                "name": "mixer"
                            },
                            {
                                "name": "envoy.cors"
                            },
                            {
                                "name": "envoy.fault"
                            },
                            {
                                "name": "envoy.router"
                            }
                        ],
                        "use_remote_address": false,
                        "generate_request_id": true,
                        "upgrade_configs": [{
                            "upgrade_type": "websocket"
                        }],
                        "stream_idle_timeout": "0s",
                        "normalize_path": true,
                        "rds": {
                            "config_source": {
                                "ads": {}
                            },
                            "route_config_name": "8888"
                        }
                    }
                }]
            }
        ],
        "deprecated_v1": {
            "bind_to_port": false
        },
        "listener_filters_timeout": "0.100s",
        "traffic_direction": "OUTBOUND",
        "continue_on_listener_filters_timeout": true
    }
}

同理最后有一個envoy.router filter,這個filter中route_config_name:"8888"歧沪,所以我們可以找到名為"8888"的Route配置

  • outbound Route
{
    "route_config": {
        "name": "8888",
        "virtual_hosts": [{
                "name": "calculator.vm.svc.cluster.local:8888",
                "domains": [
                    "calculator.vm.svc.cluster.local",
                    "calculator.vm.svc.cluster.local:8888",
                    "calculator.vm",
                    "calculator.vm:8888",
                    "calculator.vm.svc.cluster",
                    "calculator.vm.svc.cluster:8888",
                    "calculator.vm.svc",
                    "calculator.vm.svc:8888",
                    "10.106.45.142",
                    "10.106.45.142:8888"
                ],
                "routes": [{
                    "match": {
                        "prefix": "/"
                    },
                    "route": {
                        "weighted_clusters": {
                            "clusters": [{
                                    "name": "outbound|8888|v1|calculator.vm.svc.cluster.local",
                                    "weight": 50
                                },
                                {
                                    "name": "outbound|8888|v2|calculator.vm.svc.cluster.local",
                                    "weight": 50
                                }
                            ]
                        }
                    },
                    "decorator": {
                        "operation": "calculator:8888/*"
                    }
                }]
            },
            {
                "name": "allow_any",
                "domains": [
                    "*"
                ],
                "routes": [{
                    "match": {
                        "prefix": "/"
                    },
                    "route": {
                        "cluster": "PassthroughCluster"
                    }
                }]
            }
        ],
        "validate_clusters": false
    }
}

根據(jù)Route 8888的配置可以找到outbound cluster

  • outbound cluster
    {
     "version_info": "2020-02-07T10:38:17Z/1847",
     "cluster": {
      "name": "outbound|8888|v2|calculator.vm.svc.cluster.local",
      "type": "EDS",
      "eds_cluster_config": {
       "eds_config": {
        "ads": {}
       },
       "service_name": "outbound|8888|v2|calculator.vm.svc.cluster.local"
      },
      "connect_timeout": "10s",
      "circuit_breakers": {
       "thresholds": [
        {
         "max_connections": 4294967295,
         "max_pending_requests": 4294967295,
         "max_requests": 4294967295,
         "max_retries": 4294967295
        }
       ]
      },
      "http2_protocol_options": {
       "max_concurrent_streams": 1073741824
      },
      "metadata": {
       "filter_metadata": {
        "istio": {
         "config": "/apis/networking/v1alpha3/namespaces/vm/destination-rule/calculator"
        }
       }
      }
     },
     "last_updated": "2020-02-07T10:38:17.893Z"
    }

我們會發(fā)現(xiàn)這個cluster沒有直接的endpoint,因為這是動態(tài)資源歹撒,
可以通過Pilot的調(diào)試接口獲取該Cluster的endpoint:

$ curl http://xxx.xxx.xxx.xxx.108:15014/debug/edsz > pilot_eds_dump

最后發(fā)現(xiàn)endpoint為192.xxx.xxx.163:8888
然后就把流量轉(zhuǎn)發(fā)到calculator服務(wù)了

  • BlackHoleCluster
    這是一個特殊的Cluster,并沒有配置后端處理請求的Host诊胞。如其名字所暗示的一樣暖夭,請求進入后將被直接丟棄掉。如果一個請求沒有找到其對的目的服務(wù)撵孤,則被發(fā)到cluste迈着。
{
    "cluster": {
        "name": "BlackHoleCluster",
        "type": "STATIC",
        "connect_timeout": "1s"
    }
}

*PassthroughCluster
和BlackHoleCluter相反,發(fā)向PassthroughCluster的請求會被直接發(fā)送到其請求中要求的原始目地的邪码,Envoy不會對請求進行重新路由裕菠。

{
    "cluster": {
        "name": "PassthroughCluster",
        "type": "ORIGINAL_DST",
        "connect_timeout": "1s",
        "lb_policy": "CLUSTER_PROVIDED",
        "circuit_breakers": {
            "thresholds": [
                {
                    "max_connections": 4294967295,
                    "max_pending_requests": 4294967295,
                    "max_requests": 4294967295,
                    "max_retries": 4294967295
                }
            ]
        }
    }
}

outbound流程代碼

//todo

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市闭专,隨后出現(xiàn)的幾起案子奴潘,更是在濱河造成了極大的恐慌旧烧,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件萤彩,死亡現(xiàn)場離奇詭異粪滤,居然都是意外死亡斧拍,警方通過查閱死者的電腦和手機雀扶,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來肆汹,“玉大人愚墓,你說我怎么就攤上這事“好悖” “怎么了浪册?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長岗照。 經(jīng)常有香客問我村象,道長,這世上最難降的妖魔是什么攒至? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任厚者,我火速辦了婚禮,結(jié)果婚禮上迫吐,老公的妹妹穿的比我還像新娘库菲。我一直安慰自己,他們只是感情好志膀,可當我...
    茶點故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布熙宇。 她就那樣靜靜地躺著,像睡著了一般溉浙。 火紅的嫁衣襯著肌膚如雪烫止。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天戳稽,我揣著相機與錄音烈拒,去河邊找鬼。 笑死广鳍,一個胖子當著我的面吹牛荆几,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播赊时,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼吨铸,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了祖秒?” 一聲冷哼從身側(cè)響起诞吱,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤舟奠,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后房维,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體沼瘫,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年咙俩,在試婚紗的時候發(fā)現(xiàn)自己被綠了耿戚。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡阿趁,死狀恐怖膜蛔,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情脖阵,我是刑警寧澤皂股,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站命黔,受9級特大地震影響呜呐,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜悍募,卻給世界環(huán)境...
    茶點故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一蘑辑、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧搜立,春花似錦以躯、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至颠通,卻和暖如春址晕,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背顿锰。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工谨垃, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人硼控。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓刘陶,卻偏偏與公主長得像,于是被迫代替她去往敵國和親牢撼。 傳聞我的和親對象是個殘疾皇子匙隔,可洞房花燭夜當晚...
    茶點故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內(nèi)容