istio.io/archive/v1.2/zh/blog/2019/multicluster-version-routing/index.html

325 lines
38 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=多集群服务网格中的分版本路由><meta name=description content="在多集群服务网格环境中配置 Istio 的路由规则。"><meta name=author content="Frank Budinsky (IBM)"><meta name=keywords content=microservices,services,mesh,traffic-management,multicluster><meta property=og:title content=多集群服务网格中的分版本路由><meta property=og:type content=website><meta property=og:description content="在多集群服务网格环境中配置 Istio 的路由规则。"><meta property=og:url content=/v1.2/zh/blog/2019/multicluster-version-routing/><meta property=og:image content=/v1.2/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.2 / 多集群服务网格中的分版本路由</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.2/feed.xml><link rel="shortcut icon" href=/v1.2/favicons/favicon.ico><link rel=apple-touch-icon href=/v1.2/favicons/apple-touch-icon-180x180.png sizes=180x180><link rel=icon type=image/png href=/v1.2/favicons/favicon-16x16.png sizes=16x16><link rel=icon type=image/png href=/v1.2/favicons/favicon-32x32.png sizes=32x32><link rel=icon type=image/png href=/v1.2/favicons/android-36x36.png sizes=36x36><link rel=icon type=image/png href=/v1.2/favicons/android-48x48.png sizes=48x48><link rel=icon type=image/png href=/v1.2/favicons/android-72x72.png sizes=72x72><link rel=icon type=image/png href=/v1.2/favicons/android-96x96.png sizes=96xW96><link rel=icon type=image/png href=/v1.2/favicons/android-144x144.png sizes=144x144><link rel=icon type=image/png href=/v1.2/favicons/android-192x192.png sizes=192x192><link rel=manifest href=/v1.2/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.2/css/all.css><script src=/v1.2/js/themes_init.min.js></script></head><body class="language-unknown archive-site"><script>const branchName="release-1.2";const docTitle="多集群服务网格中的分版本路由";const iconFile="\/v1.2/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.2/js/all.min.js data-manual defer></script><header><nav><a id=brand href=/v1.2/zh/><span class=logo><svg viewBox="0 0 300 300"><circle cx="150" cy="150" r="146" stroke-width="2" /><path d="M65 240H225L125 270z"/><path d="M65 230l60-10V110z"/><path d="M135 220l90 10L135 30z"/></svg></span><span class=name>Istioldie 1.2</span></a><div id=hamburger><svg class="icon"><use xlink:href="/v1.2/img/icons.svg#hamburger"/></svg></div><div id=header-links><a title="了解如何部署、使用和运维 Istio。" href=/v1.2/zh/docs/>文档</a>
<span title="关于使用 Istio 的博客文章。">博客</span>
<a title=关于Istio的常见问题。 href=/v1.2/zh/faq/>FAQ</a>
<a title=关于Istio的说明。 href=/v1.2/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.2/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\/2019\/multicluster-version-routing\/');return false;">当前版本</a>
<a tabindex=-1 role=menuitem onclick="navigateToUrlOrRoot('https://preliminary.istio.io/blog\/2019\/multicluster-version-routing\/');return false;">下个版本</a>
<a tabindex=-1 role=menuitem href=https://archive.istio.io>旧版本</a></div></div><button id=search-show title=搜索istio.io aria-label=Search><svg class="icon"><use xlink:href="/v1.2/img/icons.svg#magnifier"/></svg></button></div><form id=search-form name=cse role=search><input type=hidden name=cx value=013699703217164175118: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.2/search.html>
<input id=search-textbox class=form-control name=q type=search aria-label=搜索istio.io>
<button id=search-close title="Cancel search" type=reset aria-label="Cancel search"><svg class="icon"><use xlink:href="/v1.2/img/icons.svg#cancel-x"/></svg></button></form></nav></header><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="2019 年的博客文章。" aria-controls=card0-body><svg class="icon"><use xlink:href="/v1.2/img/icons.svg#blog"/></svg>2019 年的博客文章</button><div class="body default" 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 1.1.3。" href=/v1.2/zh/blog/2019/announcing-1.1.3/>安全更新:发布 Istio 1.1.3</a></li><li role=none><a role=treeitem title="Istio 1.0.7 的补丁版本。" href=/v1.2/zh/blog/2019/announcing-1.0.7/>安全更新:发布 Istio 1.0.7</a></li><li role=none><a role=treeitem title="Istio 1.1.2 的补丁版本。" href=/v1.2/zh/blog/2019/announcing-1.1.2/>安全更新:发布 Istio 1.1.2</a></li><li role=none><a role=treeitem title="Istio 1.1 发布声明。" href=/v1.2/zh/blog/2019/announcing-1.1/>宣布 Istio 1.1 发布</a></li><li role=none><a role=treeitem title="Istio 1.1 性能概览." href=/v1.2/zh/blog/2019/istio1.1_perf/>面向性能而架构的 Istio 1.1</a></li><li role=none><a role=treeitem title="Istio 1.0.6 补丁版本。" href=/v1.2/zh/blog/2019/announcing-1.0.6/>Istio 1.0.6 发布</a></li><li role=none><span role=treeitem class=current title="在多集群服务网格环境中配置 Istio 的路由规则。">多集群服务网格中的分版本路由</span></li><li role=none><a role=treeitem title=宣布新的博客策略。 href=/v1.2/zh/blog/2019/sail-the-blog/>博客策略更新</a></li><li role=none><a role=treeitem title="评估加入 Egress gateway 对性能造成的影响。" href=/v1.2/zh/blog/2019/egress-performance/>Egress gateway 性能测试</a></li><li role=none><a role=treeitem title="Istio 将数据面组件注入到现存部署之中的过程。" href=/v1.2/zh/blog/2019/data-plane-setup/>Istio Sidecar 注入过程解密</a></li><li role=none><a role=treeitem title="使用 AppSwitch 解决应用程序启动顺序和启动延迟。" href=/v1.2/zh/blog/2019/appswitch/>使用 AppSwitch 进行 Sidestepping 依赖性排序</a></li><li role=none><a role=treeitem title="如何使用 cert-manager 手工部署一个自定义 Ingress 网关。" href=/v1.2/zh/blog/2019/custom-ingress-gateway/>使用 Cert-Manager 部署一个自定义 Ingress 网关</a></li><li role=none><a role=treeitem title="Istio 推出新的讨论板。" href=/v1.2/zh/blog/2019/announcing-discuss.istio.io/>宣布 discuss.istio.io</a></li></ul></div></div><div class=card><button class="header dynamic" id=card1 title="2017 年的博客文章。" aria-controls=card1-body><svg class="icon"><use xlink:href="/v1.2/img/icons.svg#blog"/></svg>2017 年的博客文章</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=提高可用,降低延迟。 href=/v1.2/zh/blog/2017/mixer-spof-myth/>Mixer 和 SPOF 神话</a></li><li role=none><a role=treeitem title="概要说明 Mixer 的插件架构。" href=/v1.2/zh/blog/2017/adapter-model/>Mixer 适配器模型</a></li><li role=none><a role=treeitem title="Istio 0.2 公告。" href=/v1.2/zh/blog/2017/0.2-announcement/>宣布 Istio 0.2</a></li><li role=none><a role=treeitem title="Istio 的策略如何关联 Kubernetes 的网络策略 。" href=/v1.2/zh/blog/2017/0.1-using-network-policy/>Istio 使用网络策略</a></li><li role=none><a role=treeitem title="使用 Istio 创建自动缩放的金丝雀部署。" href=/v1.2/zh/blog/2017/0.1-canary/>使用 Istio 进行金丝雀部署</a></li><li role=none><a role=treeitem title="Istio Auth 0.1 公告。" href=/v1.2/zh/blog/2017/0.1-auth/>使用 Istio 增强端到端安全</a></li><li role=none><a role=treeitem title="Istio 0.1 宣布。" href=/v1.2/zh/blog/2017/0.1-announcement/>初次了解 Istio</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.2/img/icons.svg#blog"/></svg>2018 年的博客文章</button><div class=body 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="Istio 1.0.5 补丁版本发布。" href=/v1.2/zh/blog/2018/announcing-1.0.5/>Istio 1.0.5 发布</a></li><li role=none><a role=treeitem title="Istio 1.0.4 补丁版本发布。" href=/v1.2/zh/blog/2018/announcing-1.0.4/>Istio 1.0.4 发布</a></li><li role=none><a role=treeitem title="如何在不部署 Sidecar 代理的情况下使用 Istio 进行流量管理。" href=/v1.2/zh/blog/2018/incremental-traffic-management/>增量式应用 Istio 第一部分,流量管理</a></li><li role=none><a role=treeitem title="描述了一个基于 Istio 的 Bookinfo 示例的简单场景。" href=/v1.2/zh/blog/2018/egress-mongo/>使用外部 MongoDB 服务</a></li><li role=none><a role=treeitem title="Istio 1.0.3 修补版本。" href=/v1.2/zh/blog/2018/announcing-1.0.3/>宣布 Istio 1.0.3</a></li><li role=none><a role=treeitem title="Istio 1.0.2 补丁版本." href=/v1.2/zh/blog/2018/announcing-1.0.2/>Istio 1.0.2 发布</a></li><li role=none><a role=treeitem title="Istio 1.0.1 补丁版本。" href=/v1.2/zh/blog/2018/announcing-1.0.1/>Istio 1.0.1 发布</a></li><li role=none><a role=treeitem title="Istio 在 Twitch 举办了为期一天的直播庆祝 1.0 的发布。" href=/v1.2/zh/blog/2018/istio-twitch-stream/>Istio 在 Twitch 上全天直播</a></li><li role=none><a role=treeitem title="惠普如何在 Istio 上构建其下一代鞋类个性化平台。" href=/v1.2/zh/blog/2018/hp/>Istio 是惠普 FitStation 平台的改变者</a></li><li role=none><a role=treeitem title="Istio 1.0 已生产就绪。" href=/v1.2/zh/blog/2018/announcing-1.0/>宣布 Istio 1.0</a></li><li role=none><a role=treeitem title="使用 AppSwitch 自动接入应用并降低延迟。" href=/v1.2/zh/blog/2018/delayering-istio/>使用 AppSwitch 精简 Istio 层次</a></li><li role=none><a role=treeitem title="描述 Istio 的授权功能以及如何在各种用例中使用它。" href=/v1.2/zh/blog/2018/istio-authorization/>基于 Istio 的 Micro-Segmentation 授权</a></li><li role=none><a role=treeitem title="如何通过 Stackdriver 将 Istio 访问日志导出到 BigQuery、GCS、Pub/Sub 等不同的接收器。" href=/v1.2/zh/blog/2018/export-logs-through-stackdriver/>通过 Stackdriver 将日志导出到 BigQuery、GCS、Pub/Sub</a></li><li role=none><a role=treeitem title="描述如何配置 Istio 进行 HTTP Egress 流量监控和访问策略。" href=/v1.2/zh/blog/2018/egress-monitoring-access-control/>HTTP Egress 流量监控和访问策略</a></li><li role=none><a role=treeitem title="Istio v1alpha3 路由 API 介绍,动机及其设计原则。" href=/v1.2/zh/blog/2018/v1alpha3-routing/>Istio v1aplha3 路由 API 介绍</a></li><li role=none><a role=treeitem title="描述如何在AWS上使用网络负载均衡器配置 Istio Ingress。" href=/v1.2/zh/blog/2018/aws-nlb/>使用AWS NLB 配置 Istio Ingress</a></li><li role=none><a role=treeitem title="使用 Kubernetes 命名空间和 RBAC 为 Istio 构建软性多租户环境。" href=/v1.2/zh/blog/2018/soft-multitenancy/>Istio 的软性多租户支持</a></li><li role=none><a role=treeitem title=介绍更安全,低风险的部署和发布到生产。 href=/v1.2/zh/blog/2018/traffic-mirroring/>用于在生产环境进行测试的 Istio 流量镜像功能</a></li><li role=none><a role=treeitem title="描述基于 Istio 的 Bookinfo 示例的简单场景。" href=/v1.2/zh/blog/2018/egress-tcp/>使用外部 TCP 服务</a></li><li role=none><a role=treeitem title="描述基于 Istio Bookinfo 示例的简单场景。" href=/v1.2/zh/blog/2018/egress-https/>使用外部 Web 服务</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.2/img/icons.svg#pull"/></svg></button><nav aria-label=Breadcrumb><ol><li><a href=/v1.2/zh/>Istio</a></li><li><a href=/v1.2/zh/blog/ title="关于使用 Istio 的博客文章。">博客</a></li><li><a href=/v1.2/zh/blog/2019/ title="2019 年的博客文章。">2019 年的博客文章</a></li><li>多集群服务网格中的分版本路由</li></ol></nav><article aria-labelledby=title><div class=title-area><div><h1 id=title>多集群服务网格中的分版本路由</h1><p class=byline><span>作者</span>
<span class=attribution>Frank Budinsky (IBM)</span><span> | </span><span><svg class="icon"><use xlink:href="/v1.2/img/icons.svg#calendar"/></svg><span>&nbsp;</span>2019年2月7日</span><span> | </span><span title="817 字"><svg class="icon"><use xlink:href="/v1.2/img/icons.svg#clock"/></svg><span>&nbsp;</span>阅读大约需要 4 分钟</span></p></div></div><nav class=toc-inlined aria-label="Table of Contents"><div><hr><ol><li role=none aria-label=集群部署><a href=#setup-clusters>集群部署</a><li role=none aria-label="在 cluster1 中部署 bookinfo 的 v1 版本"><a href=#%e5%9c%a8-cluster1-%e4%b8%ad%e9%83%a8%e7%bd%b2-bookinfo-%e7%9a%84-v1-%e7%89%88%e6%9c%ac><code>cluster1</code> 中部署 <code>bookinfo</code><code>v1</code> 版本</a><li role=none aria-label="在 cluster2 中部署 bookinfo 的 v2 和 v3"><a href=#%e5%9c%a8-cluster2-%e4%b8%ad%e9%83%a8%e7%bd%b2-bookinfo-%e7%9a%84-v2-%e5%92%8c-v3><code>cluster2</code> 中部署 <code>bookinfo</code><code>v2</code><code>v3</code></a><li role=none aria-label="访问 bookinfo 应用"><a href=#%e8%ae%bf%e9%97%ae-bookinfo-%e5%ba%94%e7%94%a8>访问 <code>bookinfo</code> 应用</a><li role=none aria-label="在 cluster1 上为远端的 reviews 服务创建 ServiceEntry 以及 DestinationRule"><a href=#%e5%9c%a8-cluster1-%e4%b8%8a%e4%b8%ba%e8%bf%9c%e7%ab%af%e7%9a%84-reviews-%e6%9c%8d%e5%8a%a1%e5%88%9b%e5%bb%ba-serviceentry-%e4%bb%a5%e5%8f%8a-destinationrule><code>cluster1</code> 上为远端的 <code>reviews</code> 服务创建 <code>ServiceEntry</code> 以及 <code>DestinationRule</code></a><li role=none aria-label="在所有集群上为本地 reviews 服务创建 DestinationRule"><a href=#%e5%9c%a8%e6%89%80%e6%9c%89%e9%9b%86%e7%be%a4%e4%b8%8a%e4%b8%ba%e6%9c%ac%e5%9c%b0-reviews-%e6%9c%8d%e5%8a%a1%e5%88%9b%e5%bb%ba-destinationrule>在所有集群上为本地 <code>reviews</code> 服务创建 <code>DestinationRule</code></a><li role=none aria-label="创建 VirtualService 来路由 reviews 服务的流量"><a href=#%e5%88%9b%e5%bb%ba-virtualservice-%e6%9d%a5%e8%b7%af%e7%94%b1-reviews-%e6%9c%8d%e5%8a%a1%e7%9a%84%e6%b5%81%e9%87%8f>创建 <code>VirtualService</code> 来路由 <code>reviews</code> 服务的流量</a><li role=none aria-label=总结><a href=#%e6%80%bb%e7%bb%93>总结</a><li role=none aria-label=相关内容><a href=#see-also>相关内容</a></li></ol><hr></div></nav><p>如果花一点时间对 Istio 进行了解,你可能会注意到,大量的功能都可以在单一的 Kubernetes 集群中,用简单的<a href=/v1.2/zh/docs/tasks>任务</a><a href=/v1.2/zh/docs/examples/>示例</a>所表达的方式来运行。但是真实世界中的云计算和基于微服务的应用往往不是这么简单的,会需要在不止一个地点分布运行,用户难免会产生怀疑,生产环境中是否还能这样运行?</p><p>幸运的是Istio 提供了多种服务网格的配置方式,应用能够用近乎透明的方式加入一个跨越多个集群运行的服务网格之中,也就是<a href=/v1.2/zh/docs/concepts/multicluster-deployments/#多集群服务网格>多集群服务网格</a>。最简单的设置多集群网格的方式,就是使用<a href=/v1.2/zh/docs/concepts/multicluster-deployments/#多控制平面拓扑>多控制平面拓扑</a>,这种方式不需要特别的网络依赖。在这种条件下,每个 Kubernetes 集群都有自己的控制平面,但是每个控制平面都是同步的,并接受统一的管理。</p><p>本文中,我们会在多控制平面拓扑形式的多集群网格中尝试一下 Istio 的<a href=/v1.2/zh/docs/concepts/traffic-management/>流量管理</a>功能。我们会展示如何配置 Istio 路由规则,在多集群服务网格中部署 <a href=https://github.com/istio/istio/tree/release-1.2/samples/bookinfo>Bookinfo 示例</a><code>reviews</code> 服务的 <code>v1</code> 版本运行在一个集群上,而 <code>v2</code><code>v3</code> 运行在另一个集群上,并完成远程服务调用。</p><h2 id=setup-clusters>集群部署</h2><p>首先需要部署两个 Kubernetes 集群,并各自运行一个做了轻度定制的 Istio。</p><ul><li><p>依照<a href=/v1.2/zh/docs/setup/kubernetes/install/multicluster/gateways/>使用 Gateway 连接多个集群</a>中提到的步骤设置一个多集群环境。</p></li><li><p><code>kubectl</code> 命令可以使用 <code>--context</code> 参数访问两个集群。
使用下面的命令列出所有 <code>context</code></p><pre><code class=language-bash data-expandlinks=true>$ kubectl config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* cluster1 cluster1 user@foo.com default
cluster2 cluster2 user@foo.com default
</code></pre></li><li><p>将配置文件中的 <code>context</code> 名称赋值给两个环境变量:</p><pre><code class=language-bash data-expandlinks=true>$ export CTX_CLUSTER1=&lt;cluster1 context name&gt;
$ export CTX_CLUSTER2=&lt;cluster2 context name&gt;
</code></pre></li></ul><h2 id=在-cluster1-中部署-bookinfo-的-v1-版本><code>cluster1</code> 中部署 <code>bookinfo</code><code>v1</code> 版本</h2><p><code>cluster1</code> 中运行 <code>productpage</code><code>details</code> 服务,以及 <code>reviews</code> 服务的 <code>v1</code> 版本。</p><pre><code class=language-bash data-expandlinks=true>$ kubectl label --context=$CTX_CLUSTER1 namespace default istio-injection=enabled
$ kubectl apply --context=$CTX_CLUSTER1 -f - &lt;&lt;EOF
apiVersion: v1
kind: Service
metadata:
name: productpage
labels:
app: productpage
spec:
ports:
- port: 9080
name: http
selector:
app: productpage
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: productpage-v1
spec:
replicas: 1
template:
metadata:
labels:
app: productpage
version: v1
spec:
containers:
- name: productpage
image: istio/examples-bookinfo-productpage-v1:1.10.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9080
---
apiVersion: v1
kind: Service
metadata:
name: details
labels:
app: details
spec:
ports:
- port: 9080
name: http
selector:
app: details
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: details-v1
spec:
replicas: 1
template:
metadata:
labels:
app: details
version: v1
spec:
containers:
- name: details
image: istio/examples-bookinfo-details-v1:1.10.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9080
---
apiVersion: v1
kind: Service
metadata:
name: reviews
labels:
app: reviews
spec:
ports:
- port: 9080
name: http
selector:
app: reviews
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: reviews-v1
spec:
replicas: 1
template:
metadata:
labels:
app: reviews
version: v1
spec:
containers:
- name: reviews
image: istio/examples-bookinfo-reviews-v1:1.10.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9080
EOF
</code></pre><h2 id=在-cluster2-中部署-bookinfo-的-v2-和-v3><code>cluster2</code> 中部署 <code>bookinfo</code><code>v2</code><code>v3</code></h2><p><code>cluster2</code> 中运行 <code>ratings</code> 服务以及 <code>reviews</code> 服务的 <code>v2</code><code>v3</code> 版本:</p><pre><code class=language-bash data-expandlinks=true>$ kubectl label --context=$CTX_CLUSTER2 namespace default istio-injection=enabled
$ kubectl apply --context=$CTX_CLUSTER2 -f - &lt;&lt;EOF
apiVersion: v1
kind: Service
metadata:
name: ratings
labels:
app: ratings
spec:
ports:
- port: 9080
name: http
selector:
app: ratings
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: ratings-v1
spec:
replicas: 1
template:
metadata:
labels:
app: ratings
version: v1
spec:
containers:
- name: ratings
image: istio/examples-bookinfo-ratings-v1:1.10.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9080
---
apiVersion: v1
kind: Service
metadata:
name: reviews
labels:
app: reviews
spec:
ports:
- port: 9080
name: http
selector:
app: reviews
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: reviews-v2
spec:
replicas: 1
template:
metadata:
labels:
app: reviews
version: v2
spec:
containers:
- name: reviews
image: istio/examples-bookinfo-reviews-v2:1.10.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9080
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: reviews-v3
spec:
replicas: 1
template:
metadata:
labels:
app: reviews
version: v3
spec:
containers:
- name: reviews
image: istio/examples-bookinfo-reviews-v3:1.10.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9080
EOF
</code></pre><h2 id=访问-bookinfo-应用>访问 <code>bookinfo</code> 应用</h2><p>和平常一样,我们需要使用一个 Istio gateway 来访问 <code>bookinfo</code> 应用。</p><ul><li><p><code>cluster1</code> 中创建 <code>bookinfo</code> 的网关:</p><div><a data-skipendnotes=true style=display:none href=https://raw.githubusercontent.com/istio/istio/release-1.2/samples/bookinfo/networking/bookinfo-gateway.yaml>Zip</a><pre><code class=language-bash data-expandlinks=true>$ kubectl apply --context=$CTX_CLUSTER1 -f @samples/bookinfo/networking/bookinfo-gateway.yaml@
</code></pre></div></li><li><p>遵循 <a href=/v1.2/zh/docs/examples/bookinfo/#确定-ingress-的-ip-和端口>Bookinfo 示例应用</a>中的步骤,确定 Ingress 的 IP 和端口,用浏览器打开 <code>http://$GATEWAY_URL/productpage</code></p></li></ul><p>这里会看到 <code>productpage</code>,其中包含了 <code>reviews</code> 的内容,但是没有出现 <code>ratings</code>,这是因为只有 <code>reviews</code> 服务的 <code>v1</code> 版本运行在 <code>cluster1</code> 上,我们还没有配置到 <code>cluster2</code> 的访问。</p><h2 id=在-cluster1-上为远端的-reviews-服务创建-serviceentry-以及-destinationrule><code>cluster1</code> 上为远端的 <code>reviews</code> 服务创建 <code>ServiceEntry</code> 以及 <code>DestinationRule</code></h2><p>根据<a href=/v1.2/zh/docs/setup/kubernetes/install/multicluster/gateways/#配置-dns>配置指南</a>中的介绍,远程服务可以用一个 <code>.global</code> 的 DNS 名称进行访问。在我们的案例中,就是 <code>reviews.default.global</code>,所以我们需要为这个主机创建 <code>ServiceEntry</code><code>DestinationRule</code><code>ServiceEntry</code> 会使用 <code>cluster2</code> 网关作为端点地址来访问服务。可以使用网关的 DNS 名称或者公共 IP</p><pre><code class=language-bash data-expandlinks=true>$ export CLUSTER2_GW_ADDR=$(kubectl get --context=$CTX_CLUSTER2 svc --selector=app=istio-ingressgateway \
-n istio-system -o jsonpath=&#34;{.items[0].status.loadBalancer.ingress[0].ip}&#34;)
</code></pre><p>用下面的命令来创建 <code>ServiceEntry</code><code>DestinationRule</code></p><pre><code class=language-bash data-expandlinks=true>$ kubectl apply --context=$CTX_CLUSTER1 -f - &lt;&lt;EOF
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
name: reviews-default
spec:
hosts:
- reviews.default.global
location: MESH_INTERNAL
ports:
- name: http1
number: 9080
protocol: http
resolution: DNS
addresses:
- 127.255.0.3
endpoints:
- address: ${CLUSTER2_GW_ADDR}
labels:
cluster: cluster2
ports:
http1: 15443 # 不要修改端口值
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: reviews-global
spec:
host: reviews.default.global
trafficPolicy:
tls:
mode: ISTIO_MUTUAL
subsets:
- name: v2
labels:
cluster: cluster2
- name: v3
labels:
cluster: cluster2
EOF
</code></pre><p><code>ServiceEntry</code> 的地址 <code>127.255.0.3</code> 可以是任意的未分配 IP。在 <code>127.0.0.0/8</code> 的范围里面进行选择是个不错的主意。阅读<a href=/v1.2/zh/docs/tasks/multicluster/gateways/#configure-the-example-services>通过网关进行连接的多集群</a>一文,能够获得更多相关信息。</p><p>注意 <code>DestinationRule</code> 中的 <code>subset</code> 的标签,<code>cluster: cluster2</code> 对应的是 <code>cluster2</code> 网关。一旦流量到达目标集群,就会由本地目的 <code>DestinationRule</code> 来鉴别实际的 Pod 标签(<code>version: v1</code> 或者 <code>version: v2</code></p><h2 id=在所有集群上为本地-reviews-服务创建-destinationrule>在所有集群上为本地 <code>reviews</code> 服务创建 <code>DestinationRule</code></h2><p>技术上来说,我们只需要为每个集群定义本地的 <code>subset</code> 即可(<code>cluster1</code> 中的 <code>v1</code><code>cluster2</code> 中的 <code>v2</code><code>v3</code>),但是定义一个用不到的并未部署的版本也没什么大碍,为了清晰一点,我们会在两个集群上都创建全部三个 <code>subset</code></p><pre><code class=language-bash data-expandlinks=true>$ kubectl apply --context=$CTX_CLUSTER1 -f - &lt;&lt;EOF
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews.default.svc.cluster.local
trafficPolicy:
tls:
mode: ISTIO_MUTUAL
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
- name: v3
labels:
version: v3
EOF
</code></pre><pre><code class=language-bash data-expandlinks=true>$ kubectl apply --context=$CTX_CLUSTER2 -f - &lt;&lt;EOF
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews.default.svc.cluster.local
trafficPolicy:
tls:
mode: ISTIO_MUTUAL
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
- name: v3
labels:
version: v3
EOF
</code></pre><h2 id=创建-virtualservice-来路由-reviews-服务的流量>创建 <code>VirtualService</code> 来路由 <code>reviews</code> 服务的流量</h2><p>目前所有调用 <code>reviews</code> 服务的流量都会进入本地的 <code>reviews</code> Pod也就是 <code>v1</code>,如果查看一下远吗,会发现 <code>productpage</code> 的实现只是简单的对 <code>http://reviews:9080</code> (也就是 <code>reviews.default.svc.cluster.local</code>)发起了请求,也就是本地版本。对应的远程服务名称为 <code>reviews.default.global</code>,所以需要用路由规则来把请求转发到远端集群。</p><div><aside class="callout tip"><div class=type><svg class="large-icon"><use xlink:href="/v1.2/img/icons.svg#callout-tip"/></svg></div><div class=content>注意如果所有版本的 <code>reviews</code> 服务都在远端,也就是说本地没有 <code>reviews</code> 服务,那么 DNS 就会把 <code>reviews</code> 直接解析到 <code>reviews.default.global</code>,在本文的环境里,无需定义任何路由规则就可以发起对远端集群的请求。</div></aside></div><p>创建下列的 <code>VirtualService</code>,把 <code>jason</code> 的流量转发给运行在 <code>cluster2</code> 上的 <code>v2</code><code>v3</code> 版本的 <code>reviews</code>,两个版本各负责一半流量。其他用户的流量还是会发给 <code>v1</code> 版本的 <code>reviews</code></p><pre><code class=language-bash data-expandlinks=true>$ kubectl apply --context=$CTX_CLUSTER1 -f - &lt;&lt;EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews.default.svc.cluster.local
http:
- match:
- headers:
end-user:
exact: jason
route:
- destination:
host: reviews.default.global
subset: v2
weight: 50
- destination:
host: reviews.default.global
subset: v3
weight: 50
- route:
- destination:
host: reviews.default.svc.cluster.local
subset: v1
EOF
</code></pre><div><aside class="callout tip"><div class=type><svg class="large-icon"><use xlink:href="/v1.2/img/icons.svg#callout-tip"/></svg></div><div class=content>这种平均分配的规则并不实际,只是一种用于演示远端服务多版本之间流量分配的方便手段。</div></aside></div><p>回到浏览器,用 <code>jason</code> 的身份登录。刷新页面几次,会看到星形图标在红黑两色之间切换(<code>v2</code><code>v3</code>)。如果登出,就只会看到没有 <code>ratings</code><code>reviews</code> 服务了。</p><h2 id=总结>总结</h2><p>本文中,我们看到在<a href=/v1.2/zh/docs/concepts/multicluster-deployments/#多控制平面拓扑>多控制平面拓扑</a>的多集群网格中,如何使用 Istio 路由规则进行跨集群的流量分配。
这里我们手工配置了 <code>.global</code><code>ServiceEntry</code> 以及 <code>DestinationRule</code>,用于进行对远端集群中 <code>reviews</code> 服务的访问。实际上如果我们想要的话,可以让任何服务都在远端或本地运行,当然需要为远端服务配置 <code>.global</code> 的相关资源。幸运的是,这个过程可以自动化,并且可能在 Istio 的未来版本中实现。</p><nav id=see-also><h2>相关内容</h2><div class=see-also><div class=entry><p class=link><a data-skipendnotes=true href=/v1.2/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.2/zh/blog/2019/data-plane-setup/>Istio Sidecar 注入过程解密</a></p><p class=desc>Istio 将数据面组件注入到现存部署之中的过程。</p></div><div class=entry><p class=link><a data-skipendnotes=true href=/v1.2/zh/blog/2019/custom-ingress-gateway/>使用 Cert-Manager 部署一个自定义 Ingress 网关</a></p><p class=desc>如何使用 cert-manager 手工部署一个自定义 Ingress 网关。</p></div><div class=entry><p class=link><a data-skipendnotes=true href=/v1.2/zh/blog/2018/incremental-traffic-management/>增量式应用 Istio 第一部分,流量管理</a></p><p class=desc>如何在不部署 Sidecar 代理的情况下使用 Istio 进行流量管理。</p></div><div class=entry><p class=link><a data-skipendnotes=true href=/v1.2/zh/blog/2018/egress-mongo/>使用外部 MongoDB 服务</a></p><p class=desc>描述了一个基于 Istio 的 Bookinfo 示例的简单场景。</p></div><div class=entry><p class=link><a data-skipendnotes=true href=/v1.2/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="Istio 1.0.6 补丁版本。" href=/v1.2/zh/blog/2019/announcing-1.0.6/><svg class="icon"><use xlink:href="/v1.2/img/icons.svg#left-arrow"/></svg>Istio 1.0.6 发布</a></div><div class=right><a title=宣布新的博客策略。 href=/v1.2/zh/blog/2019/sail-the-blog/>博客策略更新<svg class="icon"><use xlink:href="/v1.2/img/icons.svg#right-arrow"/></svg></a></div></nav><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=集群部署><a href=#setup-clusters>集群部署</a><li role=none aria-label="在 cluster1 中部署 bookinfo 的 v1 版本"><a href=#%e5%9c%a8-cluster1-%e4%b8%ad%e9%83%a8%e7%bd%b2-bookinfo-%e7%9a%84-v1-%e7%89%88%e6%9c%ac><code>cluster1</code> 中部署 <code>bookinfo</code><code>v1</code> 版本</a><li role=none aria-label="在 cluster2 中部署 bookinfo 的 v2 和 v3"><a href=#%e5%9c%a8-cluster2-%e4%b8%ad%e9%83%a8%e7%bd%b2-bookinfo-%e7%9a%84-v2-%e5%92%8c-v3><code>cluster2</code> 中部署 <code>bookinfo</code><code>v2</code><code>v3</code></a><li role=none aria-label="访问 bookinfo 应用"><a href=#%e8%ae%bf%e9%97%ae-bookinfo-%e5%ba%94%e7%94%a8>访问 <code>bookinfo</code> 应用</a><li role=none aria-label="在 cluster1 上为远端的 reviews 服务创建 ServiceEntry 以及 DestinationRule"><a href=#%e5%9c%a8-cluster1-%e4%b8%8a%e4%b8%ba%e8%bf%9c%e7%ab%af%e7%9a%84-reviews-%e6%9c%8d%e5%8a%a1%e5%88%9b%e5%bb%ba-serviceentry-%e4%bb%a5%e5%8f%8a-destinationrule><code>cluster1</code> 上为远端的 <code>reviews</code> 服务创建 <code>ServiceEntry</code> 以及 <code>DestinationRule</code></a><li role=none aria-label="在所有集群上为本地 reviews 服务创建 DestinationRule"><a href=#%e5%9c%a8%e6%89%80%e6%9c%89%e9%9b%86%e7%be%a4%e4%b8%8a%e4%b8%ba%e6%9c%ac%e5%9c%b0-reviews-%e6%9c%8d%e5%8a%a1%e5%88%9b%e5%bb%ba-destinationrule>在所有集群上为本地 <code>reviews</code> 服务创建 <code>DestinationRule</code></a><li role=none aria-label="创建 VirtualService 来路由 reviews 服务的流量"><a href=#%e5%88%9b%e5%bb%ba-virtualservice-%e6%9d%a5%e8%b7%af%e7%94%b1-reviews-%e6%9c%8d%e5%8a%a1%e7%9a%84%e6%b5%81%e9%87%8f>创建 <code>VirtualService</code> 来路由 <code>reviews</code> 服务的流量</a><li role=none aria-label=总结><a href=#%e6%80%bb%e7%bb%93>总结</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="Go download Istio 1.2.5 now" href=https://github.com/istio/istio/releases/tag/1.2.5 aria-label="Download Istio"><span>download</span><svg class="icon"><use xlink:href="/v1.2/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.2/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.2/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.2/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.2/img/icons.svg#twitter"/></svg></a><div class=tag>对于用户</div></div><div class=info><p class=copyright>中文内容由 ServiceMesher 社区维护,部分文档可能稍微滞后于英文版本,同步工作持续进行中<br>Istio 归档
1.2.5<br>&copy; 2019 Istio Authors, <a href=https://policies.google.com/privacy>隐私政策</a><br>归档于 2019年9月12日</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.2/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.2/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.2/img/icons.svg#working-groups"/></svg></a><div class=tag>对于开发者</div></div></footer><div id=scroll-to-top-container aria-hidden=true><button id=scroll-to-top title=回到顶部><svg class="icon"><use xlink:href="/v1.2/img/icons.svg#top"/></svg></button></div></body></html>