官网 文档位置
相关 yaml 资源下载
Bookinfo 应用分为四个单独的微服务:
- productpage:这个微服务会调用 details 和 reviews 两个微服务,用来生成页面
- details:这个微服务中包含了书籍的信息
- reviews:这个微服务中包含了书籍相关的评论。它还会调用 ratings 微服务
- ratings:这个微服务中包含了由书籍评价组成的评级信息
reviews 微服务有 3 个版本:
- v1 版本不会调用 ratings 服务
- v2 版本会调用 ratings 服务,并使用 1 到 5 个黑色星形图标来显示评分信息
- v3 版本会调用 ratings 服务,并使用 1 到 5 个红色星形图标来显示评分信息
开始部署
-
创建 yaml 文件,注意修改 service、serviceaccount 和 deployment 的 namespace
# Copyright 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.################################################################################################## # This file defines the services, service accounts, and deployments for the Bookinfo sample. # # To apply all 4 Bookinfo services, their corresponding service accounts, and deployments: # # kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml # # Alternatively, you can deploy any resource separately: # # kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml -l service=reviews # reviews Service # kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml -l account=reviews # reviews ServiceAccount # kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml -l app=reviews,version=v3 # reviews-v3 Deployment #################################################################################################################################################################################################### # Details service ################################################################################################## apiVersion: v1 kind: Service metadata:name: detailsnamespace: mm-xianliulabels:app: detailsservice: details spec:ports:- port: 9080name: httpselector:app: details --- apiVersion: v1 kind: ServiceAccount metadata:name: bookinfo-detailsnamespace: mm-xianliulabels:account: details --- apiVersion: apps/v1 kind: Deployment metadata:name: details-v1namespace: mm-xianliulabels:app: detailsversion: v1 spec:replicas: 1selector:matchLabels:app: detailsversion: v1template:metadata:labels:app: detailsversion: v1spec:serviceAccountName: bookinfo-detailscontainers:- name: detailsimage: docker.io/istio/examples-bookinfo-details-v1:1.18.0imagePullPolicy: IfNotPresentports:- containerPort: 9080 --- ################################################################################################## # Ratings service ################################################################################################## apiVersion: v1 kind: Service metadata:name: ratingsnamespace: mm-xianliulabels:app: ratingsservice: ratings spec:ports:- port: 9080name: httpselector:app: ratings --- apiVersion: v1 kind: ServiceAccount metadata:name: bookinfo-ratingsnamespace: mm-xianliulabels:account: ratings --- apiVersion: apps/v1 kind: Deployment metadata:name: ratings-v1namespace: mm-xianliulabels:app: ratingsversion: v1 spec:replicas: 1selector:matchLabels:app: ratingsversion: v1template:metadata:labels:app: ratingsversion: v1spec:serviceAccountName: bookinfo-ratingscontainers:- name: ratingsimage: docker.io/istio/examples-bookinfo-ratings-v1:1.18.0imagePullPolicy: IfNotPresentports:- containerPort: 9080 --- ################################################################################################## # Reviews service ################################################################################################## apiVersion: v1 kind: Service metadata:name: reviewsnamespace: mm-xianliulabels:app: reviewsservice: reviews spec:ports:- port: 9080name: httpselector:app: reviews --- apiVersion: v1 kind: ServiceAccount metadata:name: bookinfo-reviewsnamespace: mm-xianliulabels:account: reviews --- apiVersion: apps/v1 kind: Deployment metadata:name: reviews-v1namespace: mm-xianliulabels:app: reviewsversion: v1 spec:replicas: 1selector:matchLabels:app: reviewsversion: v1template:metadata:labels:app: reviewsversion: v1spec:serviceAccountName: bookinfo-reviewscontainers:- name: reviewsimage: docker.io/istio/examples-bookinfo-reviews-v1:1.18.0imagePullPolicy: IfNotPresentenv:- name: LOG_DIRvalue: "/tmp/logs"ports:- containerPort: 9080volumeMounts:- name: tmpmountPath: /tmp- name: wlp-outputmountPath: /opt/ibm/wlp/outputvolumes:- name: wlp-outputemptyDir: {}- name: tmpemptyDir: {} --- apiVersion: apps/v1 kind: Deployment metadata:name: reviews-v2namespace: mm-xianliulabels:app: reviewsversion: v2 spec:replicas: 1selector:matchLabels:app: reviewsversion: v2template:metadata:labels:app: reviewsversion: v2spec:serviceAccountName: bookinfo-reviewscontainers:- name: reviewsimage: docker.io/istio/examples-bookinfo-reviews-v2:1.18.0imagePullPolicy: IfNotPresentenv:- name: LOG_DIRvalue: "/tmp/logs"ports:- containerPort: 9080volumeMounts:- name: tmpmountPath: /tmp- name: wlp-outputmountPath: /opt/ibm/wlp/outputvolumes:- name: wlp-outputemptyDir: {}- name: tmpemptyDir: {} --- apiVersion: apps/v1 kind: Deployment metadata:name: reviews-v3namespace: mm-xianliulabels:app: reviewsversion: v3 spec:replicas: 1selector:matchLabels:app: reviewsversion: v3template:metadata:labels:app: reviewsversion: v3spec:serviceAccountName: bookinfo-reviewscontainers:- name: reviewsimage: docker.io/istio/examples-bookinfo-reviews-v3:1.18.0imagePullPolicy: IfNotPresentenv:- name: LOG_DIRvalue: "/tmp/logs"ports:- containerPort: 9080volumeMounts:- name: tmpmountPath: /tmp- name: wlp-outputmountPath: /opt/ibm/wlp/outputvolumes:- name: wlp-outputemptyDir: {}- name: tmpemptyDir: {} --- ################################################################################################## # Productpage services ################################################################################################## apiVersion: v1 kind: Service metadata:name: productpagenamespace: mm-xianliulabels:app: productpageservice: productpage spec:ports:- port: 9080name: httpselector:app: productpage --- apiVersion: v1 kind: ServiceAccount metadata:name: bookinfo-productpagenamespace: mm-xianliulabels:account: productpage --- apiVersion: apps/v1 kind: Deployment metadata:name: productpage-v1namespace: mm-xianliulabels:app: productpageversion: v1 spec:replicas: 1selector:matchLabels:app: productpageversion: v1template:metadata:annotations:prometheus.io/scrape: "true"prometheus.io/port: "9080"prometheus.io/path: "/metrics"labels:app: productpageversion: v1spec:serviceAccountName: bookinfo-productpagecontainers:- name: productpageimage: docker.io/istio/examples-bookinfo-productpage-v1:1.18.0imagePullPolicy: IfNotPresentports:- containerPort: 9080volumeMounts:- name: tmpmountPath: /tmpvolumes:- name: tmpemptyDir: {} ---
-
查看命名空间资源是否创建成功并正常运行
-
测试 bookinfo 是否运行(只需替换命名空间)
kubectl exec -n mm-xianliu "$(kubectl -n mm-xianliu get pod -l app=ratings -o jsonpath='{.items[0].metadata.name}')" -c ratings -- curl -sS productpage:9080/productpage | grep -o "<title>.*</title>"
输出<title>Simple Bookstore App</title>
表示正常
外部访问
-
添加网关 gateway 和虚拟服务 vs(注意修改 namespace 和 hosts)
apiVersion: networking.istio.io/v1alpha3 kind: Gateway metadata:name: bookinfo-gatewaynamespace: mm-xianliu spec:# The selector matches the ingress gateway pod labels.# If you installed Istio using Helm following the standard documentation, this would be "istio=ingress"selector:istio: ingressgateway # use istio default controllerservers:- port:number: 8080name: httpprotocol: HTTPhosts:- "my.bookinfo.com" --- apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata:name: bookinfonamespace: mm-xianliu spec:hosts:- "my.bookinfo.com"gateways:- bookinfo-gatewayhttp:- match:- uri:exact: /productpage- uri:prefix: /static- uri:exact: /login- uri:exact: /logout- uri:prefix: /api/v1/productsroute:- destination:host: productpageport:number: 9080
-
修改主机 hosts 文件
-
开始测试
curl -s "http://my.bookinfo.com/productpage" | grep -o "<title>.*</title>"
定义服务版本
-
创建目标规则(注意修改 namespace)
apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata:name: productpagenamespace: mm-xianliu spec:host: productpagesubsets:- name: v1labels:version: v1 --- apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata:name: reviewsnamespace: mm-xianliu spec:host: reviewssubsets:- name: v1labels:version: v1- name: v2labels:version: v2- name: v3labels:version: v3 --- apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata:name: ratingsnamespace: mm-xianliu spec:host: ratingssubsets:- name: v1labels:version: v1- name: v2labels:version: v2- name: v2-mysqllabels:version: v2-mysql- name: v2-mysql-vmlabels:version: v2-mysql-vm --- apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata:name: detailsnamespace: mm-xianliu spec:host: detailssubsets:- name: v1labels:version: v1- name: v2labels:version: v2 ---