一、簡介
Bookinfo是Istio官方提供的一個示例應用,主要模仿在線書店的一個分類,顯示一本書的信息。 頁面上會顯示一本書的描述,書籍的細節(ISBN、頁數等),以及關於這本書的一些評論。以下內容在官網都有介紹,詳細信息請點擊這里。
Bookinfo 應用分為四個單獨的微服務:
- productpage. 這個微服務會調用 details 和 reviews 兩個微服務,用來生成頁面。
- details. 這個微服務中包含了書籍的信息。
- reviews. 這個微服務中包含了書籍相關的評論。它還會調用 ratings 微服務。
- ratings. 這個微服務中包含了由書籍評價組成的評級信息。
reviews 微服務有 3 個版本:
- v1 版本不會調用 ratings 服務。
- v2 版本會調用 ratings 服務,並使用 1 到 5 個黑色星形圖標來顯示評分信息。
- v3 版本會調用 ratings 服務,並使用 1 到 5 個紅色星形圖標來顯示評分信息。
下圖展示了這個應用的端到端架構。

二、安裝部署Bookinfo
安裝Bookinfo
[root@k8s-master istio-1.11.3]# kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml service/details created serviceaccount/bookinfo-details created deployment.apps/details-v1 created service/ratings created serviceaccount/bookinfo-ratings created deployment.apps/ratings-v1 created service/reviews created serviceaccount/bookinfo-reviews created deployment.apps/reviews-v1 created deployment.apps/reviews-v2 created deployment.apps/reviews-v3 created service/productpage created serviceaccount/bookinfo-productpage created deployment.apps/productpage-v1 created
應用很快會啟動起來。當每個 Pod 准備就緒時,Istio 邊車代理將伴隨它們一起部署。
[root@k8s-master istio-1.11.3]# kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE details ClusterIP 10.10.153.101 <none> 9080/TCP 14s kubernetes ClusterIP 10.10.0.1 <none> 443/TCP 123m productpage ClusterIP 10.10.96.177 <none> 9080/TCP 14s ratings ClusterIP 10.10.48.175 <none> 9080/TCP 14s reviews ClusterIP 10.10.178.197 <none> 9080/TCP 14s
過幾分鍾后查看pod狀態,按照官方的說法,要等待並確保所有的 Pod 達到此狀態: 就緒狀態(READY)的值為 2/2 、狀態(STATUS)的值為 Running 。 這個操作過程可能會花費幾分鍾的時間。
[root@k8s-master istio-1.11.3]# kubectl get pods NAME READY STATUS RESTARTS AGE details-v1-79f774bdb9-nqvx6 2/2 Running 0 12m productpage-v1-6b746f74dc-wktpj 2/2 Running 0 12m ratings-v1-b6994bb9-pfr8h 2/2 Running 0 12m reviews-v1-545db77b95-vgpzt 2/2 Running 0 12m reviews-v2-7bf8c9648f-twlcz 2/2 Running 0 12m reviews-v3-84779c7bbc-ggvpw 2/2 Running 0 12m
全部狀態都為Running后,也可以登錄k8s dashboard查看pods狀態。

確認狀態都正常之后,運行下面命令,通過檢查返回的頁面標題,來驗證應用是否已在集群中運行,並已提供網頁服務:
[root@k8s-master istio-1.11.3]# kubectl exec "$(kubectl get pod -l app=ratings -o jsonpath='{.items[0].metadata.name}')" -c ratings -- curl -s productpage:9080/productpage | grep -o "<title>.*</title>" <title>Simple Bookstore App</title> [root@k8s-master istio-1.11.3]#
檢查istio的CRD和API資源
[root@k8s-master istio-1.11.3]# kubectl get crd |grep istio authorizationpolicies.security.istio.io 2021-09-28T10:09:42Z destinationrules.networking.istio.io 2021-09-28T10:09:42Z envoyfilters.networking.istio.io 2021-09-28T10:09:42Z gateways.networking.istio.io 2021-09-28T10:09:42Z istiooperators.install.istio.io 2021-09-28T10:09:42Z peerauthentications.security.istio.io 2021-09-28T10:09:42Z requestauthentications.security.istio.io 2021-09-28T10:09:42Z serviceentries.networking.istio.io 2021-09-28T10:09:42Z sidecars.networking.istio.io 2021-09-28T10:09:42Z telemetries.telemetry.istio.io 2021-09-28T10:09:42Z virtualservices.networking.istio.io 2021-09-28T10:09:42Z workloadentries.networking.istio.io 2021-09-28T10:09:42Z workloadgroups.networking.istio.io 2021-09-28T10:09:42Z [root@k8s-master istio-1.11.3]# ^C [root@k8s-master istio-1.11.3]# kubectl api-resources |grep istio istiooperators iop,io install.istio.io/v1alpha1 true IstioOperator destinationrules dr networking.istio.io/v1beta1 true DestinationRule envoyfilters networking.istio.io/v1alpha3 true EnvoyFilter gateways gw networking.istio.io/v1beta1 true Gateway serviceentries se networking.istio.io/v1beta1 true ServiceEntry sidecars networking.istio.io/v1beta1 true Sidecar virtualservices vs networking.istio.io/v1beta1 true VirtualService workloadentries we networking.istio.io/v1beta1 true WorkloadEntry workloadgroups wg networking.istio.io/v1alpha3 true WorkloadGroup authorizationpolicies security.istio.io/v1beta1 true AuthorizationPolicy peerauthentications pa security.istio.io/v1beta1 true PeerAuthentication requestauthentications ra security.istio.io/v1beta1 true RequestAuthentication telemetries telemetry telemetry.istio.io/v1alpha1 true Telemetry
三、組件安裝
1.安裝 dashboard 插件
[root@k8s-master istio-1.11.3]# kubectl apply -f samples/addons/ -n istio-system serviceaccount/grafana unchanged configmap/grafana unchanged service/grafana unchanged deployment.apps/grafana configured configmap/istio-grafana-dashboards configured configmap/istio-services-grafana-dashboards configured deployment.apps/jaeger unchanged service/tracing unchanged service/zipkin unchanged service/jaeger-collector unchanged serviceaccount/kiali unchanged configmap/kiali unchanged clusterrole.rbac.authorization.k8s.io/kiali-viewer unchanged clusterrole.rbac.authorization.k8s.io/kiali unchanged clusterrolebinding.rbac.authorization.k8s.io/kiali unchanged role.rbac.authorization.k8s.io/kiali-controlplane unchanged rolebinding.rbac.authorization.k8s.io/kiali-controlplane unchanged service/kiali unchanged deployment.apps/kiali unchanged serviceaccount/prometheus unchanged configmap/prometheus unchanged clusterrole.rbac.authorization.k8s.io/prometheus unchanged clusterrolebinding.rbac.authorization.k8s.io/prometheus unchanged service/prometheus unchanged deployment.apps/prometheus configured
將istio-ingressgateway改為NodePort方式,方便訪問
[root@k8s-master istio-1.11.3]# kubectl patch service istio-ingressgateway -n istio-system -p '{"spec":{"type":"NodePort"}}' service/istio-ingressgateway patched [root@k8s-master istio-1.11.3]#
查看kiali pod和service
[root@k8s-master istio-1.11.3]# kubectl get pods -n istio-system|grep kiali kiali-fd9f88575-spk4m 1/1 Running 1 (84m ago) 18h [root@k8s-master istio-1.11.3]# kubectl get svc -n istio-system|grep kiali kiali ClusterIP 10.10.218.189 <none> 20001/TCP,9090/TCP 18h [root@k8s-master istio-1.11.3]#
將 service 的服務類型設置為 nodeport
[root@k8s-master istio-1.11.3]# kubectl patch svc -n istio-system kiali -p '{"spec": {"type": "NodePort"}}' service/kiali patched [root@k8s-master istio-1.11.3]#
再次查看service
[root@k8s-master istio-1.11.3]# kubectl get svc -n istio-system|grep kiali kiali NodePort 10.10.218.189 <none> 20001:30668/TCP,9090:30094/TCP 18h [root@k8s-master istio-1.11.3]#
可以看到20001對應的端口是30668,通過瀏覽器訪問http://192.168.186.132:30668/,如下圖:

默認用戶名密碼: admin/admin,由於是第一次訪問,默認是以anonymous用戶登錄,無需輸入用戶名和密碼。
2、對外開放應用程序
前面雖然已經部署了BookInfo ,但還不能被外界訪問。 要開放訪問,還需要創建 Istio 入站網關(Ingress Gateway), 它會在網格邊緣把一個路徑映射到路由。步驟如下:
把應用關聯到 Istio 網關:
[root@k8s-master istio-1.11.3]# kubectl apply -f samples/bookinfo/networking/bookinfo-gateway.yaml gateway.networking.istio.io/bookinfo-gateway created virtualservice.networking.istio.io/bookinfo created
確保配置文件沒有問題
[root@k8s-master istio-1.11.3]# istioctl analyze ✔ No validation issues found when analyzing namespace: default.
確定入站 IP 和端口
執行下面命令以判斷你的 Kubernetes 集群環境是否支持外部負載均衡: 如果EXTERNAL-IP 的值為 <none> (或者一直是 <pending> 狀態), 則說明環境則沒有提供可作為入站流量網關的外部負載均衡。 在這個情況下可以用服務(Service)的 節點端口訪問網關。如果是已經設置了外部負載均衡,請參考官網的設置。
[root@k8s-master istio-1.11.3]# kubectl get svc istio-ingressgateway -n istio-system NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE istio-ingressgateway LoadBalancer 10.10.137.204 <pending> 15021:30786/TCP,80:31575/TCP,443:31297/TCP,31400:30450/TCP,15443:304
這里環境中沒有外部負載均衡,那就選擇一個節點端口來代替.
設置入站的端口:
[root@k8s-master istio-1.11.3]# export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].nodePort}') [root@k8s-master istio-1.11.3]# export SECURE_INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="https")].nodePort}')
創建一個防火牆規則,放行發往 ingressgateway 的 TCP 流量。
運行下面的命令,單獨放行發往 HTTP 端口或 HTTPS 端口的流量,或者都放行。
[root@k8s-master istio-1.11.3]# export INGRESS_HOST=$(kubectl get po -l istio=ingressgateway -n istio-system -o jsonpath='{.items[0].status.hostIP}')
設置環境變量 GATEWAY_URL
[root@k8s-master istio-1.11.3]# export GATEWAY_URL=$INGRESS_HOST:$INGRESS_PORT
確保 IP 地址和端口均成功的賦值給了環境變量
[root@k8s-master istio-1.11.3]# echo "$GATEWAY_URL" 192.168.186.130:31575
運行下面的命令,獲取Bookinfo應用的外部訪問地址
[root@k8s-master istio-1.11.3]# echo "http://$GATEWAY_URL/productpage" http://192.168.186.130:31575/productpage
把上面命令的輸出地址復制粘貼到瀏覽器並訪問,確認 Bookinfo 應用的產品頁面是否可以打開,如果一切正常如下圖:

如果多刷新幾次頁面,還會看到 productpage 頁面中會隨機展示 reviews 服務的不同版本的效果(紅色、黑色的星形或者沒有顯示)。
Bookinfo示例部署完成,接下來繼續進一步的研究。
~~~未完待續
