該指南部署了一個(gè)由四個(gè)單獨(dú)的微服務(wù)組成的 DEMO 應(yīng)用程序 Bookinfo ,其將用于演示 Istio Service Mesh 的各種特性。
1. 概述
在本指南中,我們將部署一個(gè)簡單的應(yīng)用程序,它顯示關(guān)于圖書的信息,類似于在線書店的目錄。在頁面上顯示書籍的描述、詳細(xì)細(xì)節(jié)(ISBN,頁數(shù)...)和書評。
BookInfo應(yīng)用程序分為四個(gè)獨(dú)立的微服務(wù):
productpage:(產(chǎn)品頁面)微服務(wù),調(diào)用 details 和 reviews 微服務(wù)來填充頁面。
details:微服務(wù)包含書籍的詳細(xì)信息。
reviews:微服務(wù)包含書籍的點(diǎn)評。它也調(diào)用 ratings 微服務(wù)。
ratings:微服務(wù)包含隨書評一起出現(xiàn)的評分信息。
有3個(gè)版本的 reviews 微服務(wù):
版本v1不調(diào)用 ratings 服務(wù)。
版本v2調(diào)用 ratings ,并將每個(gè)評級顯示為1到5個(gè)黑色星。
版本v3調(diào)用 ratings ,并將每個(gè)評級顯示為1到5個(gè)紅色星。

2. 安裝 Bookinfo 應(yīng)用
2.1 為 Bookinfo 創(chuàng)建 project
$ oc new-project myproject
2.2 通過將 bookinfo 使用的 service account 添加到 "myproject" 命名空間中的 anyuid 和 priveledged SCCs 來更新 Security Context Constraints (SCC,安全上下文約束) :
$ oc adm policy add-scc-to-user anyuid -z default -n myproject
$ oc adm policy add-scc-to-user privileged -z default -n myproject
2.3 在 "myproject" 命名空間中通過 bookinfo.yaml 文件來部署 Bookinfo 應(yīng)用
$ oc apply -n myproject -f bookinfo.yaml
bookinfo.yaml:
# Copyright 2017 Istio Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##################################################################################################
# Details service
##################################################################################################
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:
annotations:
sidecar.istio.io/inject: "true"
labels:
app: details
version: v1
spec:
containers:
- name: details
image: istio/examples-bookinfo-details-v1:1.8.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9080
---
##################################################################################################
# Ratings service
##################################################################################################
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:
annotations:
sidecar.istio.io/inject: "true"
labels:
app: ratings
version: v1
spec:
containers:
- name: ratings
image: istio/examples-bookinfo-ratings-v1:1.8.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9080
---
##################################################################################################
# Reviews service
##################################################################################################
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:
annotations:
sidecar.istio.io/inject: "true"
labels:
app: reviews
version: v1
spec:
containers:
- name: reviews
image: istio/examples-bookinfo-reviews-v1:1.8.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9080
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: reviews-v2
spec:
replicas: 1
template:
metadata:
annotations:
sidecar.istio.io/inject: "true"
labels:
app: reviews
version: v2
spec:
containers:
- name: reviews
image: istio/examples-bookinfo-reviews-v2:1.8.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9080
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: reviews-v3
spec:
replicas: 1
template:
metadata:
annotations:
sidecar.istio.io/inject: "true"
labels:
app: reviews
version: v3
spec:
containers:
- name: reviews
image: istio/examples-bookinfo-reviews-v3:1.8.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9080
---
##################################################################################################
# Productpage services
##################################################################################################
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:
annotations:
sidecar.istio.io/inject: "true"
labels:
app: productpage
version: v1
spec:
containers:
- name: productpage
image: istio/examples-bookinfo-productpage-v1:1.8.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9080
---
2.4 通過 bookinfo-gateway.yaml 為Bookinfo 創(chuàng)建 ingress gateway
$ oc apply -n myproject -f bookinfo-gateway.yaml
bookinfo-gateway.yaml:
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: bookinfo-gateway
spec:
selector:
istio: ingressgateway # use istio default controller
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: bookinfo
spec:
hosts:
- "*"
gateways:
- bookinfo-gateway
http:
- match:
- uri:
exact: /productpage
- uri:
exact: /login
- uri:
exact: /logout
- uri:
prefix: /api/v1/products
route:
- destination:
host: productpage
port:
number: 9080
2.5 為 ingress gateway 添加環(huán)境變量
$ export GATEWAY_URL=$(oc get route -n istio-system istio-ingressgateway -o jsonpath='{.spec.host}')
3 驗(yàn)證 Bookinfo 是否部署成功
執(zhí)行以下命令來驗(yàn)證
$ curl -o /dev/null -s -w "%{http_code}\n" http://$GATEWAY_URL/productpage
或者,在瀏覽器中打開http://$GATEWAY_URL/productpage
4 添加默認(rèn)的遠(yuǎn)端通信規(guī)則
4.1 如果不使用 mutual TLS:
$ oc apply -f destination-rule-all.yaml
destination-rule-all.yaml:
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: productpage
spec:
host: productpage
subsets:
- name: v1
labels:
version: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
- name: v3
labels:
version: v3
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: ratings
spec:
host: ratings
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
- name: v2-mysql
labels:
version: v2-mysql
- name: v2-mysql-vm
labels:
version: v2-mysql-vm
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: details
spec:
host: details
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
---
4.2 如果使用 mutual TLS:
$ oc apply -f destination-rule-all-mtls.yaml
destination-rule-all-mtls.yaml:
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: productpage
spec:
host: productpage
trafficPolicy:
tls:
mode: ISTIO_MUTUAL
subsets:
- name: v1
labels:
version: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews
trafficPolicy:
tls:
mode: ISTIO_MUTUAL
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
- name: v3
labels:
version: v3
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: ratings
spec:
host: ratings
trafficPolicy:
tls:
mode: ISTIO_MUTUAL
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
- name: v2-mysql
labels:
version: v2-mysql
- name: v2-mysql-vm
labels:
version: v2-mysql-vm
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: details
spec:
host: details
trafficPolicy:
tls:
mode: ISTIO_MUTUAL
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
---
4.3 列出所有可用的目標(biāo)規(guī)則
$ oc get destinationrules -o yaml
5. 移除 Bookinfo 應(yīng)用
5.1 下載清理腳本
$ curl -o cleanup.sh https://raw.githubusercontent.com/Maistra/bookinfo/master/cleanup.sh && chmod +x ./cleanup.sh
5.2 通過 cleanup 腳本刪除 Bookinfo 的 virtualservice, gateway, 并終止 pods:
$ ./cleanup.sh
namespace ? [default] myproject
5.3 執(zhí)行以下命令確認(rèn) Bookinfo 已經(jīng)被完全移除
$ oc get virtualservices -n myproject
No resources found.
$ oc get gateway -n myproject
No resources found.
$ oc get pods -n myproject
No resources found.