istio.io/archive/v1.5/zh/blog/2018/egress-mongo/index.html

734 lines
81 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!doctype html><html lang=zh itemscope itemtype=https://schema.org/WebPage><head><meta charset=utf-8><meta http-equiv=x-ua-compatible content="IE=edge"><meta name=viewport content="width=device-width,initial-scale=1,shrink-to-fit=no"><meta name=theme-color content="#466BB0"><meta name=title content="使用外部 MongoDB 服务"><meta name=description content="描述了一个基于 Istio 的 Bookinfo 示例的简单场景。"><meta name=author content="Vadim Eisenberg"><meta name=keywords content="microservices,services,mesh,traffic-management,egress,tcp,mongo"><meta property="og:title" content="使用外部 MongoDB 服务"><meta property="og:type" content="website"><meta property="og:description" content="描述了一个基于 Istio 的 Bookinfo 示例的简单场景。"><meta property="og:url" content="/v1.5/zh/blog/2018/egress-mongo/"><meta property="og:image" content="/v1.5/img/istio-whitelogo-bluebackground-framed.svg"><meta property="og:image:alt" content="Istio Logo"><meta property="og:image:width" content="112"><meta property="og:image:height" content="150"><meta property="og:site_name" content="Istio"><meta name=twitter:card content="summary"><meta name=twitter:site content="@IstioMesh"><title>Istioldie 1.5 / 使用外部 MongoDB 服务</title><script async src="https://www.googletagmanager.com/gtag/js?id=UA-98480406-2"></script><script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}
gtag('js',new Date());gtag('config','UA-98480406-2');</script><link rel=alternate type=application/rss+xml title="Istio Blog" href=/v1.5/blog/feed.xml><link rel=alternate type=application/rss+xml title="Istio News" href=/v1.5/news/feed.xml><link rel=alternate type=application/rss+xml title="Istio Blog and News" href=/v1.5/feed.xml><link rel="shortcut icon" href=/v1.5/favicons/favicon.ico><link rel=apple-touch-icon href=/v1.5/favicons/apple-touch-icon-180x180.png sizes=180x180><link rel=icon type=image/png href=/v1.5/favicons/favicon-16x16.png sizes=16x16><link rel=icon type=image/png href=/v1.5/favicons/favicon-32x32.png sizes=32x32><link rel=icon type=image/png href=/v1.5/favicons/android-36x36.png sizes=36x36><link rel=icon type=image/png href=/v1.5/favicons/android-48x48.png sizes=48x48><link rel=icon type=image/png href=/v1.5/favicons/android-72x72.png sizes=72x72><link rel=icon type=image/png href=/v1.5/favicons/android-96x96.png sizes=96xW96><link rel=icon type=image/png href=/v1.5/favicons/android-144x144.png sizes=144x144><link rel=icon type=image/png href=/v1.5/favicons/android-192x192.png sizes=192x192><link rel=manifest href=/v1.5/manifest.json><meta name=apple-mobile-web-app-title content="Istio"><meta name=application-name content="Istio"><link rel=stylesheet href="https://fonts.googleapis.com/css?family=Work+Sans:400|Chivo:400|Work+Sans:500,300,600,300italic,400italic,500italic,600italic|Chivo:500,300,600,300italic,400italic,500italic,600italic"><link rel=stylesheet href=/v1.5/css/all.css><script src=/v1.5/js/themes_init.min.js></script></head><body class="language-unknown archive-site"><script>const branchName="release-1.5";const docTitle="使用外部 MongoDB 服务";const iconFile="\/v1.5/img/icons.svg";const buttonCopy='复制到剪切板';const buttonPrint='打印';const buttonDownload='下载';</script><script src="https://www.google.com/cse/brand?form=search-form" defer></script><script src=/v1.5/js/all.min.js data-manual defer></script><header><nav><a id=brand href=/v1.5/zh/><span class=logo><svg viewBox="0 0 300 300"><circle cx="150" cy="150" r="146" stroke-width="2"/><polygon points="65 240 225 240 125 270"/><polygon points="65 230 125 220 125 110"/><polygon points="135 220 225 230 135 30"/></svg></span><span class=name>Istioldie 1.5</span></a><div id=hamburger><svg class="icon"><use xlink:href="/v1.5/img/icons.svg#hamburger"/></svg></div><div id=header-links><a title="了解如何部署、使用和运维 Istio。" href=/v1.5/zh/docs/>文档</a>
<a class=current title="关于使用 Istio 的博客文章。" href=/v1.5/zh/blog/2020/>博客<i class=dot data-prefix=/blog></i></a>
<a title="关于 Istio 项目的最新报道。" href=/v1.5/zh/news/>新闻<i class=dot data-prefix=/news></i></a>
<a title="关于 Istio 的常见问题。" href=/v1.5/zh/faq/>FAQ</a>
<a title="关于 Istio 项目的说明。" href=/v1.5/zh/about/>关于</a><div class=menu><button id=gearDropdownButton class=menu-trigger title=选项和设置 aria-label="Options and Settings" aria-controls=gearDropdownContent><svg class="icon"><use xlink:href="/v1.5/img/icons.svg#gear"/></svg></button><div id=gearDropdownContent class=menu-content aria-labelledby=gearDropdownButton role=menu><a tabindex=-1 role=menuitem lang=en id=switch-lang-en>English</a>
<a tabindex=-1 role=menuitem lang=zh id=switch-lang-zh class=active>中文</a><div role=separator></div><a tabindex=-1 role=menuitem class=active id=light-theme-item>亮主题</a>
<a tabindex=-1 role=menuitem id=dark-theme-item>暗主题</a><div role=separator></div><a tabindex=-1 role=menuitem id=syntax-coloring-item>代码高亮</a><div role=separator></div><h6>本站的其它版本</h6><a tabindex=-1 role=menuitem onclick="navigateToUrlOrRoot('https://istio.io/blog\/2018\/egress-mongo\/');return false;">当前版本</a>
<a tabindex=-1 role=menuitem onclick="navigateToUrlOrRoot('https://preliminary.istio.io/blog\/2018\/egress-mongo\/');return false;">下个版本</a>
<a tabindex=-1 role=menuitem href=https://istio.io/archive>旧版本</a></div></div><button id=search-show title="搜索 istio.io" aria-label=搜索><svg class="icon"><use xlink:href="/v1.5/img/icons.svg#magnifier"/></svg></button></div><form id=search-form name=cse role=search><input type=hidden name=cx value=002184991200833970123:iwwf17ikgf4>
<input type=hidden name=ie value=utf-8>
<input type=hidden name=hl value=en>
<input type=hidden id=search-page-url value=/v1.5/search>
<input id=search-textbox class=form-control name=q type=search aria-label="搜索 istio.io">
<button id=search-close title=取消搜索 type=reset aria-label=取消搜索><svg class="icon"><use xlink:href="/v1.5/img/icons.svg#cancel-x"/></svg></button></form></nav></header><div class=banner-container></div><main class=primary><div id=sidebar-container class="sidebar-container sidebar-offcanvas"><nav id=sidebar aria-label="Section Navigation"><div class=directory><div class=card><button class="header dynamic" id=card0 title="2020 年的博客文章。" aria-controls=card0-body><svg class="icon"><use xlink:href="/v1.5/img/icons.svg#blog"/></svg>2020 年的博客文章</button><div class=body aria-labelledby=card0 role=region id=card0-body><ul role=tree aria-expanded=true class=leaf-section aria-labelledby=card0><li role=none><a role=treeitem title="Istio 的扩展中使用 WASM 的前景。 (2020年3月5日)" href=/v1.5/zh/blog/2020/wasm-announce/>重新定义代理的扩展性Envoy 和 Istio 引入 WebAssembly</a></li><li role=none><a role=treeitem title="Istio 在 2020 年的愿景声明及路线图。 (2020年3月3日)" href=/v1.5/zh/blog/2020/tradewinds-2020/>Istio 2020——为了商用</a></li><li role=none><a role=treeitem title="一种更安全的秘密管理方式。 (2020年2月20日)" href=/v1.5/zh/blog/2020/istio-agent/>移除跨 pod Unix domain socket</a></li><li role=none><a role=treeitem title="为 Istio deploymentcluster提供自动化 Istio 配置,并让其像单个网格一样工作。 (2020年1月5日)" href=/v1.5/zh/blog/2020/multi-cluster-mesh-automation/>使用 Admiral 管理 Istio 多集群的配置和服务发现</a></li></ul></div></div><div class=card><button class="header dynamic" id=card1 title="2019 年的博客文章。" aria-controls=card1-body><svg class="icon"><use xlink:href="/v1.5/img/icons.svg#blog"/></svg>2019 年的博客文章</button><div class=body aria-labelledby=card1 role=region id=card1-body><ul role=tree aria-expanded=true class=leaf-section aria-labelledby=card1><li role=none><a role=treeitem title="一种更安全管理 Istio webhook 的方法。 (2019年11月14日)" href=/v1.5/zh/blog/2019/webhook/>安全管理 Webhook</a></li><li role=none><a role=treeitem title="关于 Istio 基于 operator 的安装和控制平面管理特性的介绍。 (2019年11月14日)" href=/v1.5/zh/blog/2019/introducing-istio-operator/>Istio Operator 简介</a></li><li role=none><a role=treeitem title="Istio v1beta1 授权策略的设计原则、基本概述及迁移操作。 (2019年11月14日)" href=/v1.5/zh/blog/2019/v1beta1-authorization-policy/>Istio v1beta1 授权策略概述</a></li><li role=none><a role=treeitem title="通过分析 Istio 配置来发现潜在问题和一般问题。 (2019年11月14日)" href=/v1.5/zh/blog/2019/introducing-istioctl-analyze/>istioctl analyze 介绍</a></li><li role=none><a role=treeitem title="在 Istio 中配置和管理 DNS 证书。 (2019年11月14日)" href=/v1.5/zh/blog/2019/dns-cert/>DNS 证书管理</a></li><li role=none><a role=treeitem title="正式启用访问 Istio 资源。 (2019年11月14日)" href=/v1.5/zh/blog/2019/announcing-istio-client-go/>Istio client-go 发布公告</a></li><li role=none><a role=treeitem title="把 Istio 入口网关配置为外部服务的代理。 (2019年10月15日)" href=/v1.5/zh/blog/2019/proxy/>把 Istio 作为外部服务的代理</a></li><li role=none><a role=treeitem title="将需要隔离的环境部署到单独的网格中,并通过网格联邦启用网格间通信。 (2019年10月2日)" href=/v1.5/zh/blog/2019/isolated-clusters/>用于隔离和边界保护的多网格部署</a></li><li role=none><a role=treeitem title="如何使用 Istio 去监控被阻止的和透传的外部服务流量。 (2019年9月28日)" href=/v1.5/zh/blog/2019/monitoring-external-service-traffic/>监控被阻止的和透传的外部服务流量</a></li><li role=none><a role=treeitem title="本文演示 Mixer 进程外适配器实现 Knative scale-from-zero 逻辑的具体过程。 (2019年9月18日)" href=/v1.5/zh/blog/2019/knative-activator-adapter/>适用于 Knative 的 Mixer 适配器</a></li><li role=none><a role=treeitem title="使用 Istio 实现零代码改动保护多云 Kubernetes 应用。 (2019年9月18日)" href=/v1.5/zh/blog/2019/app-identity-and-access-adapter/>APP 身份和访问适配器</a></li><li role=none><a role=treeitem title="利用 Kubernetes 可信任的 JWT 来更安全地为工作负载实例颁发证书。 (2019年9月10日)" href=/v1.5/zh/blog/2019/trustworthy-jwt-sds/>Istio 1.3 Secret 服务发现的更改</a></li><li role=none><a role=treeitem title="Istio API 的设计原则和这些 API 是如何演变的。 (2019年8月5日)" href=/v1.5/zh/blog/2019/evolving-istios-apis/>Istio API 的演变</a></li><li role=none><a role=treeitem title="管控出口流量的备选方案比较,包括性能因素。 (2019年7月22日)" href=/v1.5/zh/blog/2019/egress-traffic-control-in-istio-part-3/>Istio 中安全管控出口流量,第三部分</a></li><li role=none><a role=treeitem title="使用 Istio 的出口流量管控来阻止相关出口流量攻击。 (2019年7月10日)" href=/v1.5/zh/blog/2019/egress-traffic-control-in-istio-part-2/>Istio 中的安全管控出口流量,第二部分</a></li><li role=none><a role=treeitem title="评估 Istio 数据平面性能的工具和指南。 (2019年7月9日)" href=/v1.5/zh/blog/2019/performance-best-practices/>最佳实践Service Mesh 基准性能测试</a></li><li role=none><a role=treeitem title="了解如何延长 Istio 自签名根证书的有效期。 (2019年6月7日)" href=/v1.5/zh/blog/2019/root-transition/>延长 Istio 自签名根证书的有效期</a></li><li role=none><a role=treeitem title="涉及出口流量攻击和出口流量管控要求。 (2019年5月22日)" href=/v1.5/zh/blog/2019/egress-traffic-control-in-istio-part-1/>Istio 中的安全管控出口流量,第一部分</a></li><li role=none><a role=treeitem title="Istio 1.1 性能概览。 (2019年3月19日)" href=/v1.5/zh/blog/2019/istio1.1_perf/>为性能而设计的 Istio 1.1</a></li><li role=none><a role=treeitem title="在多集群服务网格环境中配置 Istio 的路由规则。 (2019年2月7日)" href=/v1.5/zh/blog/2019/multicluster-version-routing/>多集群服务网格中的分版本路由</a></li><li role=none><a role=treeitem title="宣布新的博客策略。 (2019年2月5日)" href=/v1.5/zh/blog/2019/sail-the-blog/>博客策略更新!</a></li><li role=none><a role=treeitem title="揭秘 Istio 是如何将其数据平面组件添加到现有 deployment。 (2019年1月31日)" href=/v1.5/zh/blog/2019/data-plane-setup/>揭开 Istio Sidecar 注入模型的神秘面纱</a></li><li role=none><a role=treeitem title="评估加入 Egress gateway 对性能造成的影响。 (2019年1月31日)" href=/v1.5/zh/blog/2019/egress-performance/>Egress gateway 性能测试</a></li><li role=none><a role=treeitem title="使用 AppSwitch 解决应用程序启动顺序和启动延迟。 (2019年1月14日)" href=/v1.5/zh/blog/2019/appswitch/>使用 AppSwitch 进行 Sidestepping 依赖性排序</a></li><li role=none><a role=treeitem title="Istio 有了一个新的论坛。 (2019年1月10日)" href=/v1.5/zh/blog/2019/announcing-discuss.istio.io/>Istio 论坛成立</a></li><li role=none><a role=treeitem title="如何使用 cert-manager 手工部署一个自定义 Ingress 网关。 (2019年1月10日)" href=/v1.5/zh/blog/2019/custom-ingress-gateway/>使用 Cert-Manager 部署一个自定义 Ingress 网关</a></li></ul></div></div><div class=card><button class="header dynamic" id=card2 title="2018 年的博客文章。" aria-controls=card2-body><svg class="icon"><use xlink:href="/v1.5/img/icons.svg#blog"/></svg>2018 年的博客文章</button><div class="body default" aria-labelledby=card2 role=region id=card2-body><ul role=tree aria-expanded=true class=leaf-section aria-labelledby=card2><li role=none><a role=treeitem title="如何在不部署 Sidecar 代理的情况下使用 Istio 进行流量管理。 (2018年11月21日)" href=/v1.5/zh/blog/2018/incremental-traffic-management/>增量式应用 Istio 第一部分,流量管理</a></li><li role=none><span role=treeitem class=current title="描述了一个基于 Istio 的 Bookinfo 示例的简单场景。 (2018年11月16日)">使用外部 MongoDB 服务</span></li><li role=none><a role=treeitem title="Istio 在 Twitch 举办了为期一天的直播庆祝 1.0 的发布。 (2018年8月3日)" href=/v1.5/zh/blog/2018/istio-twitch-stream/>Istio 在 Twitch 上全天直播</a></li><li role=none><a role=treeitem title="惠普如何在 Istio 上构建其下一代鞋类个性化平台。 (2018年7月31日)" href=/v1.5/zh/blog/2018/hp/>Istio 是惠普 FitStation 平台的改变者</a></li><li role=none><a role=treeitem title="使用 AppSwitch 自动接入应用并降低延迟。 (2018年7月30日)" href=/v1.5/zh/blog/2018/delayering-istio/>使用 AppSwitch 精简 Istio 层次</a></li><li role=none><a role=treeitem title="描述 Istio 的授权功能以及如何在各种用例中使用它。 (2018年7月20日)" href=/v1.5/zh/blog/2018/istio-authorization/>基于 Istio 授权的 Micro-Segmentation</a></li><li role=none><a role=treeitem title="如何通过 Stackdriver 将 Istio 访问日志导出到 BigQuery、GCS、Pub/Sub 等不同的接收器。 (2018年7月9日)" href=/v1.5/zh/blog/2018/export-logs-through-stackdriver/>通过 Stackdriver 将日志导出到 BigQuery、GCS、Pub/Sub</a></li><li role=none><a role=treeitem title="描述如何配置 Istio 进行 HTTP Egress 流量监控和访问策略。 (2018年6月22日)" href=/v1.5/zh/blog/2018/egress-monitoring-access-control/>HTTP Egress 流量监控和访问策略</a></li><li role=none><a role=treeitem title="Istio v1alpha3 路由 API 介绍, 动机及其设计原则。 (2018年4月25日)" href=/v1.5/zh/blog/2018/v1alpha3-routing/>Istio v1aplha3 路由 API 介绍</a></li><li role=none><a role=treeitem title="描述如何在 AWS 上使用网络负载均衡器配置 Istio Ingress。 (2018年4月20日)" href=/v1.5/zh/blog/2018/aws-nlb/>使用 AWS NLB 配置 Istio Ingress</a></li><li role=none><a role=treeitem title="使用 Kubernetes 命名空间和 RBAC 为 Istio 构建软性多租户环境。 (2018年4月19日)" href=/v1.5/zh/blog/2018/soft-multitenancy/>Istio 的软性多租户支持</a></li><li role=none><a role=treeitem title="介绍更安全,低风险的部署和发布到生产。 (2018年2月8日)" href=/v1.5/zh/blog/2018/traffic-mirroring/>用于在生产环境进行测试的 Istio 流量镜像功能</a></li><li role=none><a role=treeitem title="描述基于 Istio 的 Bookinfo 示例的简单场景。 (2018年2月6日)" href=/v1.5/zh/blog/2018/egress-tcp/>使用外部 TCP 服务</a></li><li role=none><a role=treeitem title="描述基于 Istio Bookinfo 示例的简单场景。 (2018年1月31日)" href=/v1.5/zh/blog/2018/egress-https/>使用外部 Web 服务</a></li></ul></div></div><div class=card><button class="header dynamic" id=card3 title="2017 年的博客文章。" aria-controls=card3-body><svg class="icon"><use xlink:href="/v1.5/img/icons.svg#blog"/></svg>2017 年的博客文章</button><div class=body aria-labelledby=card3 role=region id=card3-body><ul role=tree aria-expanded=true class=leaf-section aria-labelledby=card3><li role=none><a role=treeitem title="提高可用,降低延迟。 (2017年12月7日)" href=/v1.5/zh/blog/2017/mixer-spof-myth/>Mixer 和 SPOF 神话</a></li><li role=none><a role=treeitem title="概要说明 Mixer 的插件架构。 (2017年11月3日)" href=/v1.5/zh/blog/2017/adapter-model/>Mixer 适配器模型</a></li><li role=none><a role=treeitem title="Istio 的策略如何关联 Kubernetes 的网络策略 。 (2017年8月10日)" href=/v1.5/zh/blog/2017/0.1-using-network-policy/>Istio 使用网络策略</a></li><li role=none><a role=treeitem title="使用 Istio 创建自动缩放的金丝雀部署。 (2017年6月14日)" href=/v1.5/zh/blog/2017/0.1-canary/>使用 Istio 进行金丝雀部署</a></li><li role=none><a role=treeitem title="Istio Auth 0.1 公告。 (2017年5月25日)" href=/v1.5/zh/blog/2017/0.1-auth/>使用 Istio 增强端到端安全</a></li></ul></div></div></div></nav></div><div class=article-container><button tabindex=-1 id=sidebar-toggler title=折叠导航栏><svg class="icon"><use xlink:href="/v1.5/img/icons.svg#pull"/></svg></button><nav aria-label=Breadcrumb><ol><li><a href=/v1.5/zh/ title=用于连接、保护、控制和观测服务。>Istio</a></li><li><a href=/v1.5/zh/blog/ title="关于使用 Istio 的博客文章。">博客</a></li><li><a href=/v1.5/zh/blog/2018/ title="2018 年的博客文章。">2018 年的博客文章</a></li><li>使用外部 MongoDB 服务</li></ol></nav><article aria-labelledby=title><div class=title-area><div style=width:100%><h1 id=title>使用外部 MongoDB 服务</h1><p class=subtitle>Istio Egress Control Options for MongoDB traffic</p><p class=byline><span>作者</span>
<span class=attribution>Vadim Eisenberg</span><span> | </span><span><svg class="icon"><use xlink:href="/v1.5/img/icons.svg#calendar"/></svg><span>&nbsp;</span>2018年11月16日<span>&nbsp;</span>(更新于 2019年11月12日</span><span> | </span><span title="2752 字"><svg class="icon"><use xlink:href="/v1.5/img/icons.svg#clock"/></svg><span>&nbsp;</span>阅读大约需要 13 分钟</span></p></div></div><nav class=toc-inlined aria-label="Table of Contents"><div><hr><ol><li role=none aria-label="使用外部 ratings 数据库的 Bookinfo"><a href=#Bookinfo-with-external-ratings-database>使用外部 ratings 数据库的 Bookinfo</a><ol><li role=none aria-label="建立 ratings 数据库"><a href=#setting-up-the-ratings-database>建立 ratings 数据库</a><li role=none aria-label="Bookinfo 应用程序的初始设置"><a href=#Initial-setting-of-Bookinfo-application>Bookinfo 应用程序的初始设置</a><li role=none aria-label="在 Bookinfo 应用程序中使用外部数据库"><a href=#use-the-external-database-in-Bookinfo-application>在 Bookinfo 应用程序中使用外部数据库</a><li role=none aria-label=访问网页><a href=#access-the-webpage>访问网页</a></ol></li><li role=none aria-label="TCP 的 egress 控制"><a href=#egress-control-for-TCP>TCP 的 egress 控制</a><ol><li role=none aria-label="在没有 gateway 的情况下控制 TCP egress 流量"><a href=#control-TCP-egress-traffic-without-a-gateway>在没有 gateway 的情况下控制 TCP egress 流量</a><li role=none aria-label="通过 egress gateway 定向 TCP Egress 流量"><a href=#direct-TCP-egress-traffic-through-an-egress-gateway>通过 egress gateway 定向 TCP Egress 流量</a><ol><li role=none aria-label="配置从 sidecar 到 egress gateway 的 TCP 流量"><a href=#configure-TCP-traffic-from-sidecars-to-the-egress-gateway>配置从 sidecar 到 egress gateway 的 TCP 流量</a><li role=none aria-label="Sidecar 代理和 egress gateway 之间的双向 TLS"><a href=#mutual-TLS-between-the-sidecar-proxies-and-the-egress-gateway>Sidecar 代理和 egress gateway 之间的双向 TLS</a><li role=none aria-label="验证 TCP egress 流量是否通过 egress gateway 定向"><a href=#verify-that-egress-traffic-is-directed-through-the-egress-gateway>验证 TCP egress 流量是否通过 egress gateway 定向</a></ol></li><li role=none aria-label="清理通过 egress gateway 定向 TCP egress 流量的配置"><a href=#cleanup-of-TCP-egress-traffic-control>清理通过 egress gateway 定向 TCP egress 流量的配置</a></ol></li><li role=none aria-label="TLS egress 控制"><a href=#egress-control-for-TLS>TLS egress 控制</a><ol><li role=none aria-label="无 gateway 情况下控制 TLS egress 流量"><a href=#control-TLS-egress-traffic-without-a-gateway>无 gateway 情况下控制 TLS egress 流量</a><ol><li role=none aria-label="清理 TLS 的 egress 配置"><a href=#cleanup-of-the-egress-configuration-for-TLS>清理 TLS 的 egress 配置</a></ol></li><li role=none aria-label="通过 egress gateway 定向 TLS Egress 流量"><a href=#direct-tcp-egress-traffic-through-an-egress-gateway>通过 egress gateway 定向 TLS Egress 流量</a><ol><li role=none aria-label="清除通过 egress gateway 定向 TLS Egress 流量的配置"><a href=#cleanup-directing-TLS-Egress-traffic-through-an-egress-gateway>清除通过 egress gateway 定向 TLS Egress 流量的配置</a></ol></li><li role=none aria-label="启用到任意通配符域名的 MongoDB TLS egress 流量"><a href=#enable-MongoDB-TLS-egress-traffic-to-arbitrary-wildcarded-domains>启用到任意通配符域名的 MongoDB TLS egress 流量</a><ol><li role=none aria-label="准备一个 SNI 代理使用新的 egress gateway"><a href=#prepare-a-new-egress-gateway-with-an-SNI-proxy>准备一个 SNI 代理使用新的 egress gateway</a><li role=none aria-label="使用新 egress gateway 配置到 *.com 的访问"><a href=#configure-access-to-com-using-the-new-egress-gateway>使用新 egress gateway 配置到 <code>*.com</code> 的访问</a><li role=none aria-label=理解原理><a href=#understanding-what-happened>理解原理</a><li role=none aria-label="清理到任意通配符域名的 MongoDB TLS egress 流量的配置"><a href=#cleanup-of-configuration-for-MongoDB-TLS-egress-traffic-to-arbitrary-wildcarded-domains>清理到任意通配符域名的 MongoDB TLS egress 流量的配置</a></ol></li></ol></li><li role=none aria-label=清理><a href=#cleanup>清理</a><li role=none aria-label=总结><a href=#conclusion>总结</a><li role=none aria-label=相关内容><a href=#see-also>相关内容</a></li></ol><hr></div></nav><div><aside class="callout warning"><div class=type><svg class="large-icon"><use xlink:href="/v1.5/img/icons.svg#callout-warning"/></svg></div><div class=content>该博客文章是在 Istio 1.1 的版本下编写的,因此其中某些内容现在可能已过时。</div></aside></div><p><a href=/v1.5/zh/blog/2018/egress-tcp/>使用外部 TCP 服务</a>博文中,我描述了网格内的 Istio 应用程序如何通过 TCP 使用外部服务。在本文中,我将演示如何使用外部 MongoDB
服务。您将使用 <a href=/v1.5/zh/docs/examples/bookinfo/>Istio Bookinfo 示例应用程序</a>,它的书籍评级数据保存在 MongoDB 数据库中。您会将此数据库部署在集群外部,并配置 <code>ratings</code>
微服务使用它。您将学习控制到外部 MongoDB 服务流量的多种选择及其利弊。</p><h2 id=Bookinfo-with-external-ratings-database>使用外部 ratings 数据库的 Bookinfo</h2><p>首先,在您的 Kubernetes 集群外部建立一个 MongoDB 数据库实例以保存书籍评级数据。然后修改 <a href=/v1.5/zh/docs/examples/bookinfo/>Bookinfo 示例应用程序</a>使用该数据库。</p><h3 id=setting-up-the-ratings-database>建立 ratings 数据库</h3><p>在这个任务中您将建立一个 <a href=https://www.mongodb.com>MongoDB</a> 实例。您可以使用任何 MongoDB 实例;我使用 <a href=https://www.ibm.com/cloud/compose/mongodb>Compose for MongoDB</a></p><ol><li><p><code>admin</code> 用户的密码设置一个环境变量。为了避免密码被保存在 Bash 历史记录中,在运行命令之后,请立即使用 <a href=https://www.gnu.org/software/bash/manual/html_node/Bash-History-Builtins.html#Bash-History-Builtins>history -d</a> 将其从历史记录中删除。</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ export MONGO_ADMIN_PASSWORD=&lt;your MongoDB admin password&gt;
</code></pre></li><li><p>为需要创建的新用户(即 <code>bookinfo</code>)的密码设置环境变量,并使用 <a href=https://www.gnu.org/software/bash/manual/html_node/Bash-History-Builtins.html#Bash-History-Builtins>history -d</a> 将其从历史记录中删除。</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ export BOOKINFO_PASSWORD=&lt;password&gt;
</code></pre></li><li><p>为您的 MongoDB 服务设置环境变量 <code>MONGODB_HOST</code><code>MONGODB_PORT</code></p></li><li><p>创建 <code>bookinfo</code> 用户:</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ cat &lt;&lt;EOF | mongo --ssl --sslAllowInvalidCertificates $MONGODB_HOST:$MONGODB_PORT -u admin -p $MONGO_ADMIN_PASSWORD --authenticationDatabase admin
use test
db.createUser(
{
user: &#34;bookinfo&#34;,
pwd: &#34;$BOOKINFO_PASSWORD&#34;,
roles: [ &#34;read&#34;]
}
);
EOF
</code></pre></li><li><p>创建一个 <em>collection</em> 来保存评级数据。以下命令将两个评级都设置为 <code>1</code>,以便在 Bookinfo <em>ratings</em> service 使用数据库时提供视觉验证(默认 Bookinfo <em>ratings</em>
<code>4</code><code>5</code></p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ cat &lt;&lt;EOF | mongo --ssl --sslAllowInvalidCertificates $MONGODB_HOST:$MONGODB_PORT -u admin -p $MONGO_ADMIN_PASSWORD --authenticationDatabase admin
use test
db.createCollection(&#34;ratings&#34;);
db.ratings.insert(
[{rating: 1},
{rating: 1}]
);
EOF
</code></pre></li><li><p>检查 <code>bookinfo</code> 用户是否可以获取评级数据:</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ cat &lt;&lt;EOF | mongo --ssl --sslAllowInvalidCertificates $MONGODB_HOST:$MONGODB_PORT -u bookinfo -p $BOOKINFO_PASSWORD --authenticationDatabase test
use test
db.ratings.find({});
EOF
</code></pre></li></ol><p>输出应该类似于:</p><pre><code><pre><code class=language-plain data-expandlinks=true data-repo=istio>MongoDB server version: 3.4.10
switched to db test
{ &#34;_id&#34; : ObjectId(&#34;5b7c29efd7596e65b6ed2572&#34;), &#34;rating&#34; : 1 }
{ &#34;_id&#34; : ObjectId(&#34;5b7c29efd7596e65b6ed2573&#34;), &#34;rating&#34; : 1 }
bye
</code></pre></code></pre><h3 id=Initial-setting-of-Bookinfo-application>Bookinfo 应用程序的初始设置</h3><p>为了演示使用外部数据库的场景,请首先运行一个<a href=/v1.5/zh/docs/setup/getting-started/>安装了 Istio</a> 的 Kubernetes 集群。然后部署
<a href=/v1.5/zh/docs/examples/bookinfo/>Istio Bookinfo 示例应用程序</a><a href=/v1.5/zh/docs/examples/bookinfo/#apply-default-destination-rules>应用默认 destination rules</a><a href=/v1.5/zh/docs/tasks/traffic-management/egress/egress-control/#change-to-the-blocking-by-default-policy>改变 Istio 到 blocking-egress-by-default 策略</a></p><p>此应用程序从 <code>ratings</code> 微服务获取书籍评级1 到 5 的数字)。评级以星标形式显示每条评论。<code>ratings</code> 微服务有几个版本。在下一小节中,请部署使用 <a href=https://www.mongodb.com>MongoDB</a>
作为 ratings 数据库的版本。</p><p>本博文中的示例命令适用于 Istio 1.0。</p><p>作为提醒,这是 <a href=/v1.5/zh/docs/examples/bookinfo/>Bookinfo 示例应用程序</a>的端到端架构。</p><figure style=width:80%><div class=wrapper-with-intrinsic-ratio style=padding-bottom:59.086918235567985%><a data-skipendnotes=true href=/v1.5/zh/docs/examples/bookinfo/withistio.svg title="The original Bookinfo application"><img class=element-to-stretch src=/v1.5/zh/docs/examples/bookinfo/withistio.svg alt="The original Bookinfo application"></a></div><figcaption>The original Bookinfo application</figcaption></figure><h3 id=use-the-external-database-in-Bookinfo-application>在 Bookinfo 应用程序中使用外部数据库</h3><ol><li><p>部署使用 MongoDB 数据库的 <em>ratings</em> 微服务_ratings v2_</p><div><a data-skipendnotes=true style=display:none href=https://raw.githubusercontent.com/istio/istio/release-1.5/samples/bookinfo/platform/kube/bookinfo-ratings-v2.yaml>Zip</a><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl apply -f @samples/bookinfo/platform/kube/bookinfo-ratings-v2.yaml@
serviceaccount &#34;bookinfo-ratings-v2&#34; created
deployment &#34;ratings-v2&#34; created
</code></pre></div></li><li><p>为你的 MongoDB 设置 <code>MONGO_DB_URL</code> 环境变量:</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl set env deployment/ratings-v2 &#34;MONGO_DB_URL=mongodb://bookinfo:$BOOKINFO_PASSWORD@$MONGODB_HOST:$MONGODB_PORT/test?authSource=test&amp;ssl=true&#34;
deployment.extensions/ratings-v2 env updated
</code></pre></li><li><p>将所有到 <em>reviews</em> service 的流量路由到它的 <em>v3</em> 版本,以确保 <em>reviews</em> service 总是调用 <em>ratings</em> service。此外将所有到 <code>ratings</code> service
的流量路由到使用外部数据库的 _ratings v2_。</p></li></ol><p>通过添加两个 <a href=/v1.5/zh/docs/reference/config/networking/virtual-service/>virtual services</a> 来为以上两个 services 指定路由。这些 virtual service
在 Istio 发布包中 <code>samples/bookinfo/networking/virtual-service-ratings-mongodb.yaml</code> 有指定 。
<strong><em>重要:</em></strong> 请确保在运行以下命令之前<a href=/v1.5/zh/docs/examples/bookinfo/#apply-default-destination-rules>应用了默认的 destination rules</a></p><pre><code><div><a data-skipendnotes=true style=display:none href=https://raw.githubusercontent.com/istio/istio/release-1.5/samples/bookinfo/networking/virtual-service-ratings-db.yaml>Zip</a><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl apply -f @samples/bookinfo/networking/virtual-service-ratings-db.yaml@
</code></pre></div></code></pre><p>更新的架构如下所示。请注意,网格内的蓝色箭头标记对应于我们添加的 virtual service。根据 virtual service流量将被发送到 <code>reviews v3</code><code>ratings v2</code></p><figure style=width:80%><div class=wrapper-with-intrinsic-ratio style=padding-bottom:59.314858206480224%><a data-skipendnotes=true href=/v1.5/zh/blog/2018/egress-mongo/bookinfo-ratings-v2-mongodb-external.svg title="The Bookinfo application with ratings v2 and an external MongoDB database"><img class=element-to-stretch src=/v1.5/zh/blog/2018/egress-mongo/bookinfo-ratings-v2-mongodb-external.svg alt="The Bookinfo application with ratings v2 and an external MongoDB database"></a></div><figcaption>The Bookinfo application with ratings v2 and an external MongoDB database</figcaption></figure><p>请注意MongoDB 数据库位于 Istio 服务网格之外,或者更确切地说是在 Kubernetes 集群之外。服务网格的边界使用虚线标记。</p><h3 id=access-the-webpage>访问网页</h3><p><a href=/v1.5/zh/docs/examples/bookinfo/#determine-the-ingress-IP-and-port>确认 ingress IP 和端口之后</a>,访问应用程序的网页。</p><p>由于您尚未配置 egress 流量控制,所以 Istio 会阻止到 MongoDB 服务的访问。这就是为什么您当前不能看到评级的星标,只能看到 <em>&ldquo;Ratings service is currently unavailable&rdquo;</em> 的信息:</p><figure style=width:80%><div class=wrapper-with-intrinsic-ratio style=padding-bottom:36.18705035971223%><a data-skipendnotes=true href=/v1.5/zh/blog/2018/egress-mongo/errorFetchingBookRating.png title="The Ratings service error messages"><img class=element-to-stretch src=/v1.5/zh/blog/2018/egress-mongo/errorFetchingBookRating.png alt="The Ratings service error messages"></a></div><figcaption>The Ratings service error messages</figcaption></figure><p>在以下部分中,您将使用不同的 Istio egress 控制选项,配置对外部 MongoDB 服务的访问。</p><h2 id=egress-control-for-TCP>TCP 的 egress 控制</h2><p>由于 <a href=https://zh/docs.mongodb.com/manual/reference/mongodb-wire-protocol/>MongoDB 协议</a>运行在 TCP 之上,您可以像控制到<a href=/v1.5/zh/blog/2018/egress-tcp/>其余 TCP 服务</a>的流量一样控制到 MongoDB 的 egress 流量。为了控制 TCP 流量,您必须指定一个 <a href=https://tools.ietf.org/html/rfc2317>CIDR</a> 表示的 IP 块,该 IP 块包含 MongoDB 的地址。需要注意的是,有时候 MongoDB 主机的 IP 并不稳定或无法事先得知。</p><p>在 MongoDB IP 不稳定的情况下,可以以 <a href=#egress-control-for-TLS>TLS 方式控制</a> egress 流量,或绕过 Istio sidecar <a href=/v1.5/zh/docs/tasks/traffic-management/egress/egress-control/#direct-access-to-external-services>直接</a>路由流量。</p><p>获取 MongoDB 数据库实例的 IP 地址。一种选择是使用 <a href=https://linux.die.net/man/1/host>host</a> 命令。</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ export MONGODB_IP=$(host $MONGODB_HOST | grep &#34; has address &#34; | cut -d&#34; &#34; -f4)
</code></pre><h3 id=control-TCP-egress-traffic-without-a-gateway>在没有 gateway 的情况下控制 TCP egress 流量</h3><p>如果您不用通过 <a href=/v1.5/zh/docs/tasks/traffic-management/egress/egress-gateway/#use-case>egress gateway</a> 定向流量,例如不要求所有流量都通过 gateway 流出网格时,请遵循以下部分的说明。或者,如果您确实希望通过 egress gateway 定向流量,请继续阅读<a href=#direct-tcp-egress-traffic-through-an-egress-gateway>通过 egress gateway 定向 TCP egress 流量</a></p><ol><li><p>定义一个网格外 TCP service entry</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl apply -f - &lt;&lt;EOF
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
name: mongo
spec:
hosts:
- my-mongo.tcp.svc
addresses:
- $MONGODB_IP/32
ports:
- number: $MONGODB_PORT
name: tcp
protocol: TCP
location: MESH_EXTERNAL
resolution: STATIC
endpoints:
- address: $MONGODB_IP
EOF
</code></pre></li></ol><p>请注意protocol 被指定为 <code>TCP</code> 而不是 <code>MONGO</code>,因为如果 <a href=https://zh/docs.mongodb.com/manual/tutorial/configure-ssl/>MongoDB 协议运行在 TLS 之上时</a>,流量可以加密。如果加密了流量,该加密的 MongoDB 协议就不能被 Istio 代理解析。</p><p>如果您知道使用的是未加密的 MongoDB 协议,可以指定 protocol 为 <code>MONGO</code>,从而使 Istio 代理产生 <a href=https://www.envoyproxy.io/docs/envoy/latest/configuration/listeners/network_filters/mongo_proxy_filter#statistics>MongoDB 相关的统计数据</a>。还要注意,当指定 protocol <code>TCP</code> 时,配置不是特定于 MongoDB 的,对于其余使用基于 TCP 协议的数据库同样适用。</p><ol><li><p>刷新应用程序的网页。应用程序现在应该显示评级数据而非错误:</p><figure style=width:80%><div class=wrapper-with-intrinsic-ratio style=padding-bottom:36.69064748201439%><a data-skipendnotes=true href=/v1.5/zh/blog/2018/egress-mongo/externalDBRatings.png title="Book Ratings Displayed Correctly"><img class=element-to-stretch src=/v1.5/zh/blog/2018/egress-mongo/externalDBRatings.png alt="Book Ratings Displayed Correctly"></a></div><figcaption>Book Ratings Displayed Correctly</figcaption></figure></li></ol><p>请注意,和预期的一样,您会看到两个显示评论的一星评级。您将评级设置为一星,以作为外部数据库确实被使用了的视觉证据。</p><ol><li>如果要通过出口网关引导流量,请继续下一节。否则,请执行 <a href=#cleanup-of-TCP-egress-traffic-control>cleanup</a>.</li></ol><h3 id=direct-TCP-egress-traffic-through-an-egress-gateway>通过 egress gateway 定向 TCP Egress 流量</h3><p>在本节中,您将处理通过 <a href=/v1.5/zh/docs/tasks/traffic-management/egress/egress-gateway/#use-case>egress gateway</a> 定向流量的情况。Sidecar 代理通过匹配 MongoDB 主机的 IP 地址(一个 32 位长度的 CIDR 块),将 TCP 连接从 MongoDB 客户端路由到 egress gateway。Egress gateway 按照其 hostname转发流量到 MongoDB 主机。</p><ol><li><p><a href=/v1.5/zh/docs/tasks/traffic-management/egress/egress-gateway/#deploy-Istio-egress-gateway>部署 Istio egress gateway</a>.</p></li><li><p>如果您未执行<a href=#control-TCP-egress-traffic-without-a-gateway>上一节</a>中的步骤,则立即执行这些步骤。</p></li><li><p>您可能希望启用 sidecar 代理和 MongoDB 客户端之间以及 egress gateway 的 <span class=term data-title="Mutual TLS Authentication" data-body='<p>双向 TLS 通过内置身份和凭证管理,提供强大的服务到服务身份验证。
<a href="/zh/docs/concepts/security/#mutual-TLS-authentication">了解更多关于双向 TLS 身份验证</a>。</p>'>mutual TLS Authentication</span>,以使 egress gateway 监控来源 pod 的身份并基于该 identity 启用 Mixer 策略。启用双向 TLS 时同样对流量进行了加密。
如果你不想开启双向 TLS参考 <a href=#mutual-TLS-between-the-sidecar-proxies-and-the-egress-gateway>Mutual TLS between the sidecar proxies and the egress gateway</a> 小节
否则,请继续以下部分。</p></li></ol><h4 id=configure-TCP-traffic-from-sidecars-to-the-egress-gateway>配置从 sidecar 到 egress gateway 的 TCP 流量</h4><ol><li><p>定义 <code>EGRESS_GATEWAY_MONGODB_PORT</code> 环境变量来保存用于通过 egress gateway 定向流量的端口,例如 <code>7777</code>。必须选择没有被网格中其余 service 使用的端口。</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ export EGRESS_GATEWAY_MONGODB_PORT=7777
</code></pre></li><li><p>添加选择的端口到 <code>istio-egressgateway</code> service。您需要使用和安装 Istio 时一样的端口,特别是必须指定前面配置 <code>istio-egressgateway</code> 的所有端口。</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ helm template install/kubernetes/helm/istio/ --name istio-egressgateway --namespace istio-system -x charts/gateways/templates/deployment.yaml -x charts/gateways/templates/service.yaml --set gateways.istio-ingressgateway.enabled=false --set gateways.istio-egressgateway.enabled=true --set gateways.istio-egressgateway.ports[0].port=80 --set gateways.istio-egressgateway.ports[0].name=http --set gateways.istio-egressgateway.ports[1].port=443 --set gateways.istio-egressgateway.ports[1].name=https --set gateways.istio-egressgateway.ports[2].port=$EGRESS_GATEWAY_MONGODB_PORT --set gateways.istio-egressgateway.ports[2].name=mongo | kubectl apply -f -
</code></pre></li><li><p>检查 <code>istio-egressgateway</code> service 确实有选择的端口:</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl get svc istio-egressgateway -n istio-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
istio-egressgateway ClusterIP 172.21.202.204 &lt;none&gt; 80/TCP,443/TCP,7777/TCP 34d
</code></pre></li><li><p><code>istio-egressgateway</code> 服务 关闭双向 TLS 认证</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl apply -f - &lt;&lt;EOF
apiVersion: authentication.istio.io/v1alpha1
kind: Policy
metadata:
name: istio-egressgateway
namespace: istio-system
spec:
targets:
- name: istio-egressgateway
EOF
</code></pre></li><li><p>为您的 MongoDB service 创建一个 egress <code>Gateway</code>、一个 destination rules 和 virtual services以定向流量到 egress gateway并从 egress gateway 发送到外部服务。</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl apply -f - &lt;&lt;EOF
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: istio-egressgateway
spec:
selector:
istio: egressgateway
servers:
- port:
number: $EGRESS_GATEWAY_MONGODB_PORT
name: tcp
protocol: TCP
hosts:
- my-mongo.tcp.svc
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: egressgateway-for-mongo
spec:
host: istio-egressgateway.istio-system.svc.cluster.local
subsets:
- name: mongo
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: mongo
spec:
host: my-mongo.tcp.svc
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: direct-mongo-through-egress-gateway
spec:
hosts:
- my-mongo.tcp.svc
gateways:
- mesh
- istio-egressgateway
tcp:
- match:
- gateways:
- mesh
destinationSubnets:
- $MONGODB_IP/32
port: $MONGODB_PORT
route:
- destination:
host: istio-egressgateway.istio-system.svc.cluster.local
subset: mongo
port:
number: $EGRESS_GATEWAY_MONGODB_PORT
- match:
- gateways:
- istio-egressgateway
port: $EGRESS_GATEWAY_MONGODB_PORT
route:
- destination:
host: my-mongo.tcp.svc
port:
number: $MONGODB_PORT
weight: 100
EOF
</code></pre></li><li><p><a href=#verify-that-egress-traffic-is-directed-through-the-egress-gateway>验证 TCP egress 流量是否被定向到 egress gateway</a>.</p></li></ol><h4 id=mutual-TLS-between-the-sidecar-proxies-and-the-egress-gateway>Sidecar 代理和 egress gateway 之间的双向 TLS</h4><ol><li><p>删除前面小节中的配置:</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl delete gateway istio-egressgateway --ignore-not-found=true
$ kubectl delete virtualservice direct-mongo-through-egress-gateway --ignore-not-found=true
$ kubectl delete destinationrule egressgateway-for-mongo mongo --ignore-not-found=true
$ kubectl delete policy istio-egressgateway -n istio-system --ignore-not-found=true
</code></pre></li><li><p>Enforce mutual TLS authentication for the <code>istio-egressgateway</code> service:</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl apply -f - &lt;&lt;EOF
apiVersion: authentication.istio.io/v1alpha1
kind: Policy
metadata:
name: istio-egressgateway
namespace: istio-system
spec:
targets:
- name: istio-egressgateway
peers:
- mtls: {}
EOF
</code></pre></li><li><p>为您的 MongoDB service 创建一个 egress <code>Gateway</code>、一个 destination rules 和 virtual services以定向流量到 egress gateway并从 egress gateway 发送到外部服务。</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl apply -f - &lt;&lt;EOF
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: istio-egressgateway
spec:
selector:
istio: egressgateway
servers:
- port:
number: 443
name: tls
protocol: TLS
hosts:
- my-mongo.tcp.svc
tls:
mode: MUTUAL
serverCertificate: /etc/certs/cert-chain.pem
privateKey: /etc/certs/key.pem
caCertificates: /etc/certs/root-cert.pem
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: egressgateway-for-mongo
spec:
host: istio-egressgateway.istio-system.svc.cluster.local
subsets:
- name: mongo
trafficPolicy:
loadBalancer:
simple: ROUND_ROBIN
portLevelSettings:
- port:
number: 443
tls:
mode: ISTIO_MUTUAL
sni: my-mongo.tcp.svc
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: mongo
spec:
host: my-mongo.tcp.svc
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: direct-mongo-through-egress-gateway
spec:
hosts:
- my-mongo.tcp.svc
gateways:
- mesh
- istio-egressgateway
tcp:
- match:
- gateways:
- mesh
destinationSubnets:
- $MONGODB_IP/32
port: $MONGODB_PORT
route:
- destination:
host: istio-egressgateway.istio-system.svc.cluster.local
subset: mongo
port:
number: 443
- match:
- gateways:
- istio-egressgateway
port: 443
route:
- destination:
host: my-mongo.tcp.svc
port:
number: $MONGODB_PORT
weight: 100
EOF
</code></pre></li><li><p>继续下一节。</p></li></ol><h4 id=verify-that-egress-traffic-is-directed-through-the-egress-gateway>验证 TCP egress 流量是否通过 egress gateway 定向</h4><ol><li><p>再次刷新应用程序的网页,并验证等级是否仍正确显示。</p></li><li><p><a href=/v1.5/zh/docs/tasks/observability/logs/access-log/#enable-envoy-s-access-logging>开启 Envoy 访问日志</a></p></li><li><p>检查 egress gateway 的 Envoy 的统计数据,找到对应请求 MongoDB service 的 counter。如果 Istio 步骤在 <code>istio-system</code> namespace 中,打印 counter 的命令为:</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl logs -l istio=egressgateway -n istio-system
[2019-04-14T06:12:07.636Z] &#34;- - -&#34; 0 - &#34;-&#34; 1591 4393 94 - &#34;-&#34; &#34;-&#34; &#34;-&#34; &#34;-&#34; &#34;&lt;Your MongoDB IP&gt;:&lt;your MongoDB port&gt;&#34; outbound|&lt;your MongoDB port&gt;||my-mongo.tcp.svc 172.30.146.119:59924 172.30.146.119:443 172.30.230.1:59206 -
</code></pre></li></ol><h3 id=cleanup-of-TCP-egress-traffic-control>清理通过 egress gateway 定向 TCP egress 流量的配置</h3><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl delete serviceentry mongo
$ kubectl delete gateway istio-egressgateway --ignore-not-found=true
$ kubectl delete virtualservice direct-mongo-through-egress-gateway --ignore-not-found=true
$ kubectl delete destinationrule egressgateway-for-mongo mongo --ignore-not-found=true
$ kubectl delete policy istio-egressgateway -n istio-system --ignore-not-found=true
</code></pre><h2 id=egress-control-for-TLS>TLS egress 控制</h2><p>在现实生活中,绝大多数到外部服务的通信都必须被加密,而 <a href=https://zh/docs.mongodb.com/manual/tutorial/configure-ssl/>MongoDB 协议在 TLS 之上运行</a>
并且TLS 客户端经常发送<a href=https://en.wikipedia.org/wiki/Server_Name_Indication>服务器名称指示</a>SNI作为握手的一部分。
如果您的 MongoDB 服务器运行 TLS 且 MongoDB 客户端发送 SNI 作为握手的一部分,您就可以像任何其余带有 SNI 的 TLS 流量一样控制 MongoDB egress 流量。
您不需要指定 MongoDB 服务器的 IP 地址,而只需指定他们的主机名称,这样会更加方便,因为您无需依赖 IP 地址的稳定性。
您还可以指定通配符为主机名的前缀,例如允许从 <code>*.com</code> 域访问任意服务器。</p><p>要想检查您的 MongoDB 服务器是否支持 TLS请运行</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ openssl s_client -connect $MONGODB_HOST:$MONGODB_PORT -servername $MONGODB_HOST
</code></pre><p>如果上述命令打印了一个服务器返回的证书,说明该服务器支持 TLS。如果没有您就需要像前面小节描述的一样在 TCP 层面控制 MongoDB egress 流量。</p><h3 id=control-TLS-egress-traffic-without-a-gateway>无 gateway 情况下控制 TLS egress 流量</h3><p>如果您<a href=/v1.5/zh/docs/tasks/traffic-management/egress/egress-gateway/#use-case>不需要 egress gateway</a>,请遵循本小节中的说明。
如果您需要通过 egress gateway 定向流量,请继续阅读<a href=#direct-tcp-egress-traffic-through-an-egress-gateway>通过 egress gateway 定向 TCP Egress 流量</a></p><ol><li><p>为 MongoDB service 创建一个 <code>ServiceEntry</code></p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl apply -f - &lt;&lt;EOF
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
name: mongo
spec:
hosts:
- $MONGODB_HOST
ports:
- number: $MONGODB_PORT
name: tls
protocol: TLS
resolution: DNS
EOF
</code></pre></li><li><p>刷新应用程序的网页。应用程序应该正确显示评级数据。</p></li></ol><h4 id=cleanup-of-the-egress-configuration-for-TLS>清理 TLS 的 egress 配置</h4><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl delete serviceentry mongo
</code></pre><h3 id=direct-tcp-egress-traffic-through-an-egress-gateway>通过 egress gateway 定向 TLS Egress 流量</h3><p>在本小节中,您将处理通过 <a href=/v1.5/zh/docs/tasks/traffic-management/egress/egress-gateway/#use-case>egress gateway</a> 定向流量的情况。
Sidecar 代理通过匹配 MongoDB 主机的 SNI将 TLS 连接从 MongoDB 客户端路由到 egress gateway。
Egress gateway 再将流量转发到 MongoDB 主机。请注意sidecar 代理会将目的端口重写为 443。
Egress gateway 在 443 端口上接受 MongoDB 流量,按照 SNI 匹配 MongoDB 主机,并再次将端口重写为 MongoDB 服务器的端口。</p><ol><li><p><a href=/v1.5/zh/docs/tasks/traffic-management/egress/egress-gateway/#deploy-Istio-egress-gateway>部署 Istio egress gateway</a>.</p></li><li><p>为 MongoDB service 创建一个 <code>ServiceEntry</code>:</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl apply -f - &lt;&lt;EOF
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
name: mongo
spec:
hosts:
- $MONGODB_HOST
ports:
- number: $MONGODB_PORT
name: tls
protocol: TLS
- number: 443
name: tls-port-for-egress-gateway
protocol: TLS
resolution: DNS
location: MESH_EXTERNAL
EOF
</code></pre></li><li><p>刷新应用程序的网页并验证评级数据是否显示正常。</p></li><li><p>为您的 MongoDB service 创建一个 egress <code>Gateway</code>、一个 destination rules 和 virtual services以将流量定向到 egress gateway并从 egress gateway 发送到外部服务。</p></li></ol><p>如果您希望启用 sidecar 代理和应用程序 pod 以及 egress gateway 之间的<a href=/v1.5/zh/docs/tasks/security/authentication/mutual-tls/>双向 TLS 认证</a>,可以使用下面的命令。(您可能希望启用双向 TLS 以使 egress gateway 监控来源 pod 的身份并基于该 identity 启用 Mixer 策略。)</p><pre><code><div id=tabset-zh-blog-2018-egress-mongo-1 role=tablist class=tabset>
<div class=tab-strip data-category-name=mtls><button aria-selected=true data-category-value=enabled aria-controls=tabset-zh-blog-2018-egress-mongo-1-0-panel id=tabset-zh-blog-2018-egress-mongo-1-0-tab role=tab><span>mutual TLS enabled</span>
</button><button tabindex=-1 data-category-value=disabled aria-controls=tabset-zh-blog-2018-egress-mongo-1-1-panel id=tabset-zh-blog-2018-egress-mongo-1-1-tab role=tab><span>mutual TLS disabled</span>
</button></div>
<div class=tab-content><div id=tabset-zh-blog-2018-egress-mongo-1-0-panel role=tabpanel tabindex=0 aria-labelledby=tabset-zh-blog-2018-egress-mongo-1-0-tab><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl apply -f - &lt;&lt;EOF
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: istio-egressgateway
spec:
selector:
istio: egressgateway
servers:
- port:
number: 443
name: tls
protocol: TLS
hosts:
- $MONGODB_HOST
tls:
mode: MUTUAL
serverCertificate: /etc/certs/cert-chain.pem
privateKey: /etc/certs/key.pem
caCertificates: /etc/certs/root-cert.pem
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: egressgateway-for-mongo
spec:
host: istio-egressgateway.istio-system.svc.cluster.local
subsets:
- name: mongo
trafficPolicy:
loadBalancer:
simple: ROUND_ROBIN
portLevelSettings:
- port:
number: 443
tls:
mode: ISTIO_MUTUAL
sni: $MONGODB_HOST
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: direct-mongo-through-egress-gateway
spec:
hosts:
- $MONGODB_HOST
gateways:
- mesh
- istio-egressgateway
tls:
- match:
- gateways:
- mesh
port: $MONGODB_PORT
sni_hosts:
- $MONGODB_HOST
route:
- destination:
host: istio-egressgateway.istio-system.svc.cluster.local
subset: mongo
port:
number: 443
tcp:
- match:
- gateways:
- istio-egressgateway
port: 443
route:
- destination:
host: $MONGODB_HOST
port:
number: $MONGODB_PORT
weight: 100
EOF
</code></pre></div><div hidden id=tabset-zh-blog-2018-egress-mongo-1-1-panel role=tabpanel tabindex=0 aria-labelledby=tabset-zh-blog-2018-egress-mongo-1-1-tab><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl apply -f - &lt;&lt;EOF
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: istio-egressgateway
spec:
selector:
istio: egressgateway
servers:
- port:
number: 443
name: tls
protocol: TLS
hosts:
- $MONGODB_HOST
tls:
mode: PASSTHROUGH
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: egressgateway-for-mongo
spec:
host: istio-egressgateway.istio-system.svc.cluster.local
subsets:
- name: mongo
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: direct-mongo-through-egress-gateway
spec:
hosts:
- $MONGODB_HOST
gateways:
- mesh
- istio-egressgateway
tls:
- match:
- gateways:
- mesh
port: $MONGODB_PORT
sni_hosts:
- $MONGODB_HOST
route:
- destination:
host: istio-egressgateway.istio-system.svc.cluster.local
subset: mongo
port:
number: 443
- match:
- gateways:
- istio-egressgateway
port: 443
sni_hosts:
- $MONGODB_HOST
route:
- destination:
host: $MONGODB_HOST
port:
number: $MONGODB_PORT
weight: 100
EOF
</code></pre></div></div></div></code></pre><ol><li><a href=#verify-that-egress-traffic-is-directed-through-the-egress-gateway>验证 TCP egress 流量是否通过 egress gateway 定向</a></li></ol><h4 id=cleanup-directing-TLS-Egress-traffic-through-an-egress-gateway>清除通过 egress gateway 定向 TLS Egress 流量的配置</h4><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl delete serviceentry mongo
$ kubectl delete gateway istio-egressgateway
$ kubectl delete virtualservice direct-mongo-through-egress-gateway
$ kubectl delete destinationrule egressgateway-for-mongo
</code></pre><h3 id=enable-MongoDB-TLS-egress-traffic-to-arbitrary-wildcarded-domains>启用到任意通配符域名的 MongoDB TLS egress 流量</h3><p>有时,您希望将 egress 流量配置为来自同一域的多个主机名,例如到 <code>*.&lt;your company domain>.com</code> 中的所有 MongoDB service。
您不希望创建多个配置项,而是一个用于公司中所有 MongoDB service 的通用配置项。
要想通过一个配置来控制到所有相同域中的外部服务的访问,您需要使用 <em>通配符</em> 主机。</p><p>在本节中,您将为通配符域名配置 egress gateway。我在 <code>composedb.com</code> 处使用了 MongoDB instance
因此为 <code>*.com</code> 配置出口流量对我有效(我也可以使用<code>*.composedb.com</code>)。
您可以根据 MongoDB 主机选择通配符域名。</p><p>要为通配符域名配置 egress gateway 流量,
您需要使用<a href=/v1.5/zh/docs/tasks/traffic-management/egress/wildcard-egress-hosts/#wildcard-configuration-for-arbitrary-domains>一个额外的 SNI 代理</a>来部署一个自定义的 egress gateway。由于 EnvoyIstio egress gateway 使用的标准代理)目前的限制,这是必须的。</p><h4 id=prepare-a-new-egress-gateway-with-an-SNI-proxy>准备一个 SNI 代理使用新的 egress gateway</h4><p>在本节中,除了标准的 Istio Envoy 代理之外,您还将部署具有 SNI 代理的 egress gateway。您可以使用任何能够根据任意未预先配置的 SNI 值路由流量的 SNI 代理;我们使用 <a href=http://nginx.org>Nginx</a> 来实现这一功能。</p><ol><li><p>为 Nginx SNI 代理创建配置文件。如果需要,您可以编辑该文件以指定其他 Nginx 设置。</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ cat &lt;&lt;EOF &gt; ./sni-proxy.conf
user www-data;
events {
}
stream {
log_format log_stream &#39;\$remote_addr [\$time_local] \$protocol [\$ssl_preread_server_name]&#39;
&#39;\$status \$bytes_sent \$bytes_received \$session_time&#39;;
access_log /var/log/nginx/access.log log_stream;
error_log /var/log/nginx/error.log;
# tcp forward proxy by SNI
server {
resolver 8.8.8.8 ipv6=off;
listen 127.0.0.1:$MONGODB_PORT;
proxy_pass \$ssl_preread_server_name:$MONGODB_PORT;
ssl_preread on;
}
}
EOF
</code></pre></li><li><p>创建一个 Kubernetes <a href=https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/>ConfigMap</a> 来保存 Nginx SNI 代理的配置:</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl create configmap egress-sni-proxy-configmap -n istio-system --from-file=nginx.conf=./sni-proxy.conf
</code></pre></li><li><p>下面的命令将产生用于编辑和部署的 <code>istio-egressgateway-with-sni-proxy.yaml</code> 文件。</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ cat &lt;&lt;EOF | helm template install/kubernetes/helm/istio/ --name istio-egressgateway-with-sni-proxy --namespace istio-system -x charts/gateways/templates/deployment.yaml -x charts/gateways/templates/service.yaml -x charts/gateways/templates/serviceaccount.yaml -x charts/gateways/templates/autoscale.yaml -x charts/gateways/templates/role.yaml -x charts/gateways/templates/rolebindings.yaml --set global.mtls.enabled=true --set global.istioNamespace=istio-system -f - &gt; ./istio-egressgateway-with-sni-proxy.yaml
gateways:
enabled: true
istio-ingressgateway:
enabled: false
istio-egressgateway:
enabled: false
istio-egressgateway-with-sni-proxy:
enabled: true
labels:
app: istio-egressgateway-with-sni-proxy
istio: egressgateway-with-sni-proxy
replicaCount: 1
autoscaleMin: 1
autoscaleMax: 5
cpu:
targetAverageUtilization: 80
serviceAnnotations: {}
type: ClusterIP
ports:
- port: 443
name: https
secretVolumes:
- name: egressgateway-certs
secretName: istio-egressgateway-certs
mountPath: /etc/istio/egressgateway-certs
- name: egressgateway-ca-certs
secretName: istio-egressgateway-ca-certs
mountPath: /etc/istio/egressgateway-ca-certs
configVolumes:
- name: sni-proxy-config
configMapName: egress-sni-proxy-configmap
additionalContainers:
- name: sni-proxy
image: nginx
volumeMounts:
- name: sni-proxy-config
mountPath: /etc/nginx
readOnly: true
EOF
</code></pre></li><li><p>部署新的 egress gateway</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl apply -f ./istio-egressgateway-with-sni-proxy.yaml
serviceaccount &#34;istio-egressgateway-with-sni-proxy-service-account&#34; created
role &#34;istio-egressgateway-with-sni-proxy-istio-system&#34; created
rolebinding &#34;istio-egressgateway-with-sni-proxy-istio-system&#34; created
service &#34;istio-egressgateway-with-sni-proxy&#34; created
deployment &#34;istio-egressgateway-with-sni-proxy&#34; created
horizontalpodautoscaler &#34;istio-egressgateway-with-sni-proxy&#34; created
</code></pre></li><li><p>验证新 egress gateway 是否工作正常。请注意 pod 有两个容器(一个是 Envoy 代理,另一个是 SNI 代理)。</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl get pod -l istio=egressgateway-with-sni-proxy -n istio-system
NAME READY STATUS RESTARTS AGE
istio-egressgateway-with-sni-proxy-79f6744569-pf9t2 2/2 Running 0 17s
</code></pre></li><li><p>创建一个使用静态地址 127.0.0.1 (<code>localhost</code>) 的 service entry并对定向到新 service entry 的流量禁用双向 TLS</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl apply -f - &lt;&lt;EOF
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
name: sni-proxy
spec:
hosts:
- sni-proxy.local
location: MESH_EXTERNAL
ports:
- number: $MONGODB_PORT
name: tcp
protocol: TCP
resolution: STATIC
endpoints:
- address: 127.0.0.1
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: disable-mtls-for-sni-proxy
spec:
host: sni-proxy.local
trafficPolicy:
tls:
mode: DISABLE
EOF
</code></pre></li></ol><h4 id=configure-access-to-com-using-the-new-egress-gateway>使用新 egress gateway 配置到 <code>*.com</code> 的访问</h4><ol><li><p><code>*.com</code> 定义一个 <code>ServiceEntry</code></p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ cat &lt;&lt;EOF | kubectl create -f -
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
name: mongo
spec:
hosts:
- &#34;*.com&#34;
ports:
- number: 443
name: tls
protocol: TLS
- number: $MONGODB_PORT
name: tls-mongodb
protocol: TLS
location: MESH_EXTERNAL
EOF
</code></pre></li><li><p>为 *.com 创建一个 egress Gateway使用 443 端口和 TLS 协议。创建一个 destination rule 来为 gateway 设置 <a href=https://en.wikipedia.org/wiki/Server_Name_Indication>SNI</a>
以及为 Envoy 过滤器,以防止恶意应用程序篡改 SNI (过滤器验证这个应用程序发布的 SNI 与报告给 Mixer 的 SNI 是否相同)</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl apply -f - &lt;&lt;EOF
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: istio-egressgateway-with-sni-proxy
spec:
selector:
istio: egressgateway-with-sni-proxy
servers:
- port:
number: 443
name: tls
protocol: TLS
hosts:
- &#34;*.com&#34;
tls:
mode: MUTUAL
serverCertificate: /etc/certs/cert-chain.pem
privateKey: /etc/certs/key.pem
caCertificates: /etc/certs/root-cert.pem
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: mtls-for-egress-gateway
spec:
host: istio-egressgateway-with-sni-proxy.istio-system.svc.cluster.local
subsets:
- name: mongo
trafficPolicy:
loadBalancer:
simple: ROUND_ROBIN
portLevelSettings:
- port:
number: 443
tls:
mode: ISTIO_MUTUAL
---
# The following filter is used to forward the original SNI (sent by the application) as the SNI of the mutual TLS
# connection.
# The forwarded SNI will be reported to Mixer so that policies will be enforced based on the original SNI value.
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: forward-downstream-sni
spec:
filters:
- listenerMatch:
portNumber: $MONGODB_PORT
listenerType: SIDECAR_OUTBOUND
filterName: forward_downstream_sni
filterType: NETWORK
filterConfig: {}
---
# The following filter verifies that the SNI of the mutual TLS connection (the SNI reported to Mixer) is
# identical to the original SNI issued by the application (the SNI used for routing by the SNI proxy).
# The filter prevents Mixer from being deceived by a malicious application: routing to one SNI while
# reporting some other value of SNI. If the original SNI does not match the SNI of the mutual TLS connection, the
# filter will block the connection to the external service.
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: egress-gateway-sni-verifier
spec:
workloadLabels:
app: istio-egressgateway-with-sni-proxy
filters:
- listenerMatch:
portNumber: 443
listenerType: GATEWAY
filterName: sni_verifier
filterType: NETWORK
filterConfig: {}
EOF
</code></pre></li><li><p>将目的为 <em>*.com</em> 的流量路由到 egress gateway并从 egress gateway 路由到 SNI 代理.</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl apply -f - &lt;&lt;EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: direct-mongo-through-egress-gateway
spec:
hosts:
- &#34;*.com&#34;
gateways:
- mesh
- istio-egressgateway-with-sni-proxy
tls:
- match:
- gateways:
- mesh
port: $MONGODB_PORT
sni_hosts:
- &#34;*.com&#34;
route:
- destination:
host: istio-egressgateway-with-sni-proxy.istio-system.svc.cluster.local
subset: mongo
port:
number: 443
weight: 100
tcp:
- match:
- gateways:
- istio-egressgateway-with-sni-proxy
port: 443
route:
- destination:
host: sni-proxy.local
port:
number: $MONGODB_PORT
weight: 100
EOF
</code></pre></li><li><p>再次刷新应用程序的网页,验证评级数据仍然显示正确。</p></li><li><p><a href=/v1.5/zh/docs/tasks/observability/logs/access-log/#enable-envoy-s-access-logging>开启 Envoy 访问日志</a></p></li><li><p>检查 egress gateway 的 Envoy 的日志。如果 Istio 部署在 <code>istio-system</code> namespace 中,打印日志的的命令为:</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl logs -l istio=egressgateway-with-sni-proxy -c istio-proxy -n istio-system
</code></pre><p>You should see lines similar to the following:</p><pre><code class=language-plain data-expandlinks=true data-repo=istio>[2019-01-02T17:22:04.602Z] &#34;- - -&#34; 0 - 768 1863 88 - &#34;-&#34; &#34;-&#34; &#34;-&#34; &#34;-&#34; &#34;127.0.0.1:28543&#34; outbound|28543||sni-proxy.local 127.0.0.1:49976 172.30.146.115:443 172.30.146.118:58510 &lt;your MongoDB host&gt;
[2019-01-02T17:22:04.713Z] &#34;- - -&#34; 0 - 1534 2590 85 - &#34;-&#34; &#34;-&#34; &#34;-&#34; &#34;-&#34; &#34;127.0.0.1:28543&#34; outbound|28543||sni-proxy.local 127.0.0.1:49988 172.30.146.115:443 172.30.146.118:58522 &lt;your MongoDB host&gt;
</code></pre></li><li><p>检查 SNI 代理的日志。如果 Istio 部署在 <code>istio-system</code> namespace 中,打印日志的命令为:</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl logs -l istio=egressgateway-with-sni-proxy -n istio-system -c sni-proxy
127.0.0.1 [23/Aug/2018:03:28:18 +0000] TCP [&lt;your MongoDB host&gt;]200 1863 482 0.089
127.0.0.1 [23/Aug/2018:03:28:18 +0000] TCP [&lt;your MongoDB host&gt;]200 2590 1248 0.095
</code></pre></li></ol><h4 id=understanding-what-happened>理解原理</h4><p>在本节中,您使用通配符域名为您的 MongoDB 主机配置了 egress 流量。对于单个 MongoDB 主机使用通配符域名没有任何好处(可以指定确切的主机名),
而当集群中的应用程序需要访问多个匹配某个通配符域名的 MongoDB 主机时可能有用。
例如,如果应用程序需要访问 <code>mongodb1.composedb.com</code><code>mongodb2.composedb.com</code><code>mongodb3.composedb.com</code> 时,
egress 流量可以使用针对泛域名 <code>*.composedb.com</code> 的单个配置实现。</p><p>当配置一个应用使用另一个主机名匹配本小节中的通配符域名的 MongoDB 实例时,不需要额外的 Istio 配置。
我将这留作一个练习,让读者自行验证。</p><h4 id=cleanup-of-configuration-for-MongoDB-TLS-egress-traffic-to-arbitrary-wildcarded-domains>清理到任意通配符域名的 MongoDB TLS egress 流量的配置</h4><ol><li><p>删除针对 <code>*.com</code> 的配置项:</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl delete serviceentry mongo
$ kubectl delete gateway istio-egressgateway-with-sni-proxy
$ kubectl delete virtualservice direct-mongo-through-egress-gateway
$ kubectl delete destinationrule mtls-for-egress-gateway
$ kubectl delete envoyfilter forward-downstream-sni egress-gateway-sni-verifier
</code></pre></li><li><p>删除 <code>egressgateway-with-sni-proxy</code> <code>Deployment</code> 的配置项:</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl delete serviceentry sni-proxy
$ kubectl delete destinationrule disable-mtls-for-sni-proxy
$ kubectl delete -f ./istio-egressgateway-with-sni-proxy.yaml
$ kubectl delete configmap egress-sni-proxy-configmap -n istio-system
</code></pre></li><li><p>删除您创建的配置文件:</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ rm ./istio-egressgateway-with-sni-proxy.yaml
$ rm ./nginx-sni-proxy.conf
</code></pre></li></ol><h2 id=cleanup>清理</h2><ol><li><p>删除<code>bookinfo</code>用户:</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ cat &lt;&lt;EOF | mongo --ssl --sslAllowInvalidCertificates $MONGODB_HOST:$MONGODB_PORT -u admin -p $MONGO_ADMIN_PASSWORD --authenticationDatabase admin
use test
db.dropUser(&#34;bookinfo&#34;);
EOF
</code></pre></li><li><p>删除 <code>ratings</code> 集合:</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ cat &lt;&lt;EOF | mongo --ssl --sslAllowInvalidCertificates $MONGODB_HOST:$MONGODB_PORT -u admin -p $MONGO_ADMIN_PASSWORD --authenticationDatabase admin
use test
db.ratings.drop();
EOF
</code></pre></li><li><p>取消您使用的环境变量:</p><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ unset MONGO_ADMIN_PASSWORD BOOKINFO_PASSWORD MONGODB_HOST MONGODB_PORT MONGODB_IP
</code></pre></li><li><p>删除 virtual services</p><div><a data-skipendnotes=true style=display:none href=https://raw.githubusercontent.com/istio/istio/release-1.5/samples/bookinfo/networking/virtual-service-ratings-db.yaml>Zip</a><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl delete -f @samples/bookinfo/networking/virtual-service-ratings-db.yaml@
Deleted config: virtual-service/default/reviews
Deleted config: virtual-service/default/ratings
</code></pre></div></li><li><p>删除 <code>ratings v2-mongodb</code> deployment</p><div><a data-skipendnotes=true style=display:none href=https://raw.githubusercontent.com/istio/istio/release-1.5/samples/bookinfo/platform/kube/bookinfo-ratings-v2.yaml>Zip</a><pre><code class=language-bash data-expandlinks=true data-repo=istio>$ kubectl delete -f @samples/bookinfo/platform/kube/bookinfo-ratings-v2.yaml@
deployment &#34;ratings-v2&#34; deleted
</code></pre></div></li></ol><h2 id=conclusion>总结</h2><p>在这篇博文中,我演示了 MongoDB egress 流量控制的各种选项。您可以在 TCP 或 TLS 层面上控制 MongoDB egress 流量。
根据您的组织的安全需求,在 TCP 和 TLS 场景下您都可以将流量从 sidecar 代理定向到外部 MongoDB 主机
,或者通过一个 egress gateway 进行转发。在后面一种场景中,您还可以决定是否禁用 sidecar 代理到 egress gateway 的双向 TLS 认证。
如果您想要通过指定类似 <code>*.com</code> 的通配符域名来从 TLS 层面控制 MongoDB 的 egress 流量,并且通过 egress gateway 定向流量时,
您必须部署一个使用 SNI 代理的自定义 egress gateway。</p><p>请注意,本博客文章中描述的 MongoDB 配置和注意事项与 TCP/TLS 之上的其他非 HTTP 协议相同。</p><nav id=see-also><h2>相关内容</h2><div class=see-also><div class=entry><p class=link><a data-skipendnotes=true href=/v1.5/zh/blog/2019/egress-performance/>Egress gateway 性能测试</a></p><p class=desc>评估加入 Egress gateway 对性能造成的影响。</p></div><div class=entry><p class=link><a data-skipendnotes=true href=/v1.5/zh/blog/2018/egress-tcp/>使用外部 TCP 服务</a></p><p class=desc>描述基于 Istio 的 Bookinfo 示例的简单场景。</p></div><div class=entry><p class=link><a data-skipendnotes=true href=/v1.5/zh/blog/2019/egress-traffic-control-in-istio-part-3/>Istio 中安全管控出口流量,第三部分</a></p><p class=desc>管控出口流量的备选方案比较,包括性能因素。</p></div><div class=entry><p class=link><a data-skipendnotes=true href=/v1.5/zh/blog/2019/egress-traffic-control-in-istio-part-2/>Istio 中的安全管控出口流量,第二部分</a></p><p class=desc>使用 Istio 的出口流量管控来阻止相关出口流量攻击。</p></div><div class=entry><p class=link><a data-skipendnotes=true href=/v1.5/zh/blog/2019/egress-traffic-control-in-istio-part-1/>Istio 中的安全管控出口流量,第一部分</a></p><p class=desc>涉及出口流量攻击和出口流量管控要求。</p></div><div class=entry><p class=link><a data-skipendnotes=true href=/v1.5/zh/blog/2018/egress-monitoring-access-control/>HTTP Egress 流量监控和访问策略</a></p><p class=desc>描述如何配置 Istio 进行 HTTP Egress 流量监控和访问策略。</p></div></div></nav></article><nav class=pagenav><div class=left><a title="如何在不部署 Sidecar 代理的情况下使用 Istio 进行流量管理。" href=/v1.5/zh/blog/2018/incremental-traffic-management/><svg class="icon"><use xlink:href="/v1.5/img/icons.svg#left-arrow"/></svg>增量式应用 Istio 第一部分,流量管理</a></div><div class=right><a title="Istio 在 Twitch 举办了为期一天的直播庆祝 1.0 的发布。" href=/v1.5/zh/blog/2018/istio-twitch-stream/>Istio 在 Twitch 上全天直播<svg class="icon"><use xlink:href="/v1.5/img/icons.svg#right-arrow"/></svg></a></div></nav><div id=feedback><div id=feedback-initial>这些信息有用吗?<br><button class="btn feedback" onclick="sendFeedback('zh',1)">是的</button>
<button class="btn feedback" onclick="sendFeedback('zh',0)">没有</button></div><div id=feedback-comment>Do you have any suggestions for improvement?<br><br><input id=feedback-textbox type=text placeholder="Help us improve..." data-lang=zh></div><div id=feedback-thankyou>Thanks for your feedback!</div></div><div id=endnotes-container aria-hidden=true><h2>链接</h2><ol id=endnotes></ol></div></div><div class=toc-container><nav class=toc aria-label="Table of Contents"><div id=toc><ol><li role=none aria-label="使用外部 ratings 数据库的 Bookinfo"><a href=#Bookinfo-with-external-ratings-database>使用外部 ratings 数据库的 Bookinfo</a><ol><li role=none aria-label="建立 ratings 数据库"><a href=#setting-up-the-ratings-database>建立 ratings 数据库</a><li role=none aria-label="Bookinfo 应用程序的初始设置"><a href=#Initial-setting-of-Bookinfo-application>Bookinfo 应用程序的初始设置</a><li role=none aria-label="在 Bookinfo 应用程序中使用外部数据库"><a href=#use-the-external-database-in-Bookinfo-application>在 Bookinfo 应用程序中使用外部数据库</a><li role=none aria-label=访问网页><a href=#access-the-webpage>访问网页</a></ol></li><li role=none aria-label="TCP 的 egress 控制"><a href=#egress-control-for-TCP>TCP 的 egress 控制</a><ol><li role=none aria-label="在没有 gateway 的情况下控制 TCP egress 流量"><a href=#control-TCP-egress-traffic-without-a-gateway>在没有 gateway 的情况下控制 TCP egress 流量</a><li role=none aria-label="通过 egress gateway 定向 TCP Egress 流量"><a href=#direct-TCP-egress-traffic-through-an-egress-gateway>通过 egress gateway 定向 TCP Egress 流量</a><ol><li role=none aria-label="配置从 sidecar 到 egress gateway 的 TCP 流量"><a href=#configure-TCP-traffic-from-sidecars-to-the-egress-gateway>配置从 sidecar 到 egress gateway 的 TCP 流量</a><li role=none aria-label="Sidecar 代理和 egress gateway 之间的双向 TLS"><a href=#mutual-TLS-between-the-sidecar-proxies-and-the-egress-gateway>Sidecar 代理和 egress gateway 之间的双向 TLS</a><li role=none aria-label="验证 TCP egress 流量是否通过 egress gateway 定向"><a href=#verify-that-egress-traffic-is-directed-through-the-egress-gateway>验证 TCP egress 流量是否通过 egress gateway 定向</a></ol></li><li role=none aria-label="清理通过 egress gateway 定向 TCP egress 流量的配置"><a href=#cleanup-of-TCP-egress-traffic-control>清理通过 egress gateway 定向 TCP egress 流量的配置</a></ol></li><li role=none aria-label="TLS egress 控制"><a href=#egress-control-for-TLS>TLS egress 控制</a><ol><li role=none aria-label="无 gateway 情况下控制 TLS egress 流量"><a href=#control-TLS-egress-traffic-without-a-gateway>无 gateway 情况下控制 TLS egress 流量</a><ol><li role=none aria-label="清理 TLS 的 egress 配置"><a href=#cleanup-of-the-egress-configuration-for-TLS>清理 TLS 的 egress 配置</a></ol></li><li role=none aria-label="通过 egress gateway 定向 TLS Egress 流量"><a href=#direct-tcp-egress-traffic-through-an-egress-gateway>通过 egress gateway 定向 TLS Egress 流量</a><ol><li role=none aria-label="清除通过 egress gateway 定向 TLS Egress 流量的配置"><a href=#cleanup-directing-TLS-Egress-traffic-through-an-egress-gateway>清除通过 egress gateway 定向 TLS Egress 流量的配置</a></ol></li><li role=none aria-label="启用到任意通配符域名的 MongoDB TLS egress 流量"><a href=#enable-MongoDB-TLS-egress-traffic-to-arbitrary-wildcarded-domains>启用到任意通配符域名的 MongoDB TLS egress 流量</a><ol><li role=none aria-label="准备一个 SNI 代理使用新的 egress gateway"><a href=#prepare-a-new-egress-gateway-with-an-SNI-proxy>准备一个 SNI 代理使用新的 egress gateway</a><li role=none aria-label="使用新 egress gateway 配置到 *.com 的访问"><a href=#configure-access-to-com-using-the-new-egress-gateway>使用新 egress gateway 配置到 <code>*.com</code> 的访问</a><li role=none aria-label=理解原理><a href=#understanding-what-happened>理解原理</a><li role=none aria-label="清理到任意通配符域名的 MongoDB TLS egress 流量的配置"><a href=#cleanup-of-configuration-for-MongoDB-TLS-egress-traffic-to-arbitrary-wildcarded-domains>清理到任意通配符域名的 MongoDB TLS egress 流量的配置</a></ol></li></ol></li><li role=none aria-label=清理><a href=#cleanup>清理</a><li role=none aria-label=总结><a href=#conclusion>总结</a><li role=none aria-label=相关内容><a href=#see-also>相关内容</a></li></ol></div></nav></div></main><footer><div class=user-links><a class=channel title="立刻下载 Istio 1.5.4" href=/v1.5/docs/setup/getting-started/#download aria-label="Download Istio"><span>download</span><svg class="icon"><use xlink:href="/v1.5/img/icons.svg#download"/></svg>
</a><a class=channel title="加入 Istio discussion board 参与讨论获取帮助" href=https://discuss.istio.io aria-label="Istio discussion board"><span>discuss</span><svg class="icon"><use xlink:href="/v1.5/img/icons.svg#discourse"/></svg></a>
<a class=channel title="Stack Overflow 中列举了针对实际问题以及部署、配置和使用 Istio 的各项回答" href=https://stackoverflow.com/questions/tagged/istio aria-label="Stack Overflow"><span>stack overflow</span><svg class="icon"><use xlink:href="/v1.5/img/icons.svg#stackoverflow"/></svg></a>
<a class=channel title="在 Slack 上与 Istio 社区交互讨论开发问题(仅限邀请)" href=https://istio.slack.com aria-label=slack><span>slack</span><svg class="icon"><use xlink:href="/v1.5/img/icons.svg#slack"/></svg></a>
<a class=channel title="关注我们的 Twitter 来获取最新信息" href=https://twitter.com/IstioMesh aria-label=Twitter><span>twitter</span><svg class="icon"><use xlink:href="/v1.5/img/icons.svg#twitter"/></svg></a><div class=tag>对于用户</div></div><div class=info><p class=copyright>中文内容由 ServiceMesher 社区维护,部分文档可能稍微滞后于英文版本,同步工作持续进行中<br>Istio 归档
1.5.4<br>&copy; 2020 Istio Authors, <a href=https://policies.google.com/privacy>隐私政策</a><br>归档于 2020年5月21日</p></div><div class=dev-links><a class=channel title="Istio 的代码在 GitHub 上开发" href=https://github.com/istio/community aria-label=GitHub><span>github</span><svg class="icon"><use xlink:href="/v1.5/img/icons.svg#github"/></svg></a>
<a class=channel title="如果您想深入了解 Istio 的技术细节,请查看我们日益完善的设计文档" href=https://groups.google.com/forum/#!forum/istio-team-drive-access aria-label="team drive"><span>drive</span><svg class="icon"><use xlink:href="/v1.5/img/icons.svg#drive"/></svg></a>
<a class=channel title="如果您想为 Istio 项目做出贡献,请考虑加入我们的工作组" href=https://github.com/istio/community/blob/master/WORKING-GROUPS.md aria-label="working groups"><span>working groups</span><svg class="icon"><use xlink:href="/v1.5/img/icons.svg#working-groups"/></svg></a><div class=tag>对于开发者</div></div></footer><script src=https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js defer></script><div id=scroll-to-top-container aria-hidden=true><button id=scroll-to-top title=回到顶部><svg class="icon"><use xlink:href="/v1.5/img/icons.svg#top"/></svg></button></div></body></html>