服务网格:创建VirtualService(3分)
将Bookinfo应用部署到default命名空间下,为Bookinfo应用创建一个名为reviews的VirtualService,要求来自名为Jason的用户的所有流量将被路由到reviews服务的v2版本。(需要用到的软件包:ServiceMesh.tar.gz)
# 上传解压
tar -xf ServiceMesh.tar.gz
# 导入镜像
docker load -i ServiceMesh/images/image.tar
# 初始化Kubernetes集群
init-cluster
# 查看集群状态
kubectl cluster-info
# 进入ServiceMesh目录
cd ServiceMesh/
# 执行bookinfo
kubectl apply -f bookinfo/bookinfo.yaml
# 查看pops状态
kubectl gcat 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:
prefix: /static
- uri:
exact: /login
- uri:
exact: /logout
- uri:
prefix: /api/v1/products
route:
- destination:
host: productpage
port:
number: 9080
cat 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:
prefix: /static
- uri:
exact: /login
- uri:
exact: /logout
- uri:
prefix: /api/v1/products
route:
- destination:
host: productpage
port:
number: 9080
# 使用Istio Gateway来实现外部访问
cat 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:
prefix: /static
- uri:
exact: /login
- uri:
exact: /logout
- uri:
prefix: /api/v1/products
route:
- destination:
host: productpage
port:
number: 9080
# 为应用程序定义Ingress网关
kubectl apply -f bookinfo-gateway.yaml
# 确认网关创建完成
kubectl get gateway
# 查看Ingress Gateway
kubectl get svc -n istio-system
# 可以看到Gateway 80端口对应的NodePort端口是22092,在浏览器上通过http://master_IP:22092/productpage访问Bookinfo应用
# 编写目标规则配置文件
cat 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
# 执行yaml
kubectl apply -f destination-rule-all.yaml
# 查询destinationrule
kubectl get destinationrule
# 重新部署productpage微服务,启用Istio
cat bookinfo/bookinfo.yaml | istioctl kube-inject -f - | kubectl apply -l app=productpage -f -
# 检查productpage的Pod并且查看每个副本的两个容器
kubectl get pods
# 在浏览器上登录Grafana(http://master_IP:33000)
# 依次点击左侧导航栏的“Dashboards” →“Manage”进入Dashboard管理界面
# 选择Istio Mesh Dashboard
# 切换到Istio Service Dashboard仪表盘,在Service中选择productpage
# 所有服务启用Istio
cat bookinfo/bookinfo.yaml | istioctl kube-inject -f - | kubectl apply -l app!=productpage -f -
# 查看应用程序Pod,现在每个Pod的两个容器
kubectl get pods
# 再次查看Istio Mesh Dashboard,会发现当前命名空间下所有服务都会出现在服务列表中
# 访问Kiali控住台(http://master_IP:20001)
# 通过可视化界面来查看应用程序的拓扑结构,点击“Graph”按钮,在Namespace下拉菜单中选择命名空间default,然后在Display下拉菜单中选中“Traffic Animation”和“Idle Nodes”复选框,就可以看到实时流量动画。
# reviews微服务v1版本不会调用ratings服务,所以图中ratings服务无流量通过。
# 访问Prometheus控制台(http://master_IP:30090)
# 在Expression输入框中输入要查询的参数,然后点击Execute按钮即可在Console中查看查询结果。
# 查询请求时采用istio_requests_total指标,这是一个标准的Istio指标。
# 如查询命名空间的所有请求(istio_requests_total{destination_service_namespace=“default”, reporter=“destination”})
# 查询reviews微服务的请求(istio_requests_total{destination_service_namespace=“default”,reporter=“destination”,destination_service_name=“reviews”})
# 部署v2版本的reviews微服务并开启Istio
cat bookinfo/reviews-v2.yaml | istioctl kube-inject -f - | kubectl apply -f -
cat bookinfo/reviews-v3.yaml | istioctl kube-inject -f - | kubectl apply -f -
# 查看Pod
kubectl get pods
# 设置Kubernetes HPA来管理三个版本Deployment的副本
kubectl autoscale deployment reviews-v1 --cpu-percent=50 --min=1 --max=10
kubectl autoscale deployment reviews-v2 --cpu-percent=50 --min=1 --max=10
kubectl autoscale deployment reviews-v3 --cpu-percent=50 --min=1 --max=10
# 为微服务设置默认版本的Virtual Service
vi virtual-service-all-v1.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: productpage
spec:
hosts:
- productpage
http:
- route:
- destination:
host: productpage
subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: ratings
spec:
hosts:
- ratings
http:
- route:
- destination:
host: ratings
subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: details
spec:
hosts:
- details
http:
- route:
- destination:
host: details
subset: v1
# 可以通过应用Virtual Service规则将100%的流量路由reviews:v3
vi virtual-service-reviews-v3.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v3
# 执行yaml
kubectl apply -f virtual-service-reviews-v3.yaml
# 初始化默认路由规则,将所有流量路由到服务的v1版本
kubectl apply -f virtual-service-all-v1.yaml
# 改变reviews服务的流量规则,将v1版本的流量镜像到v2版本
vi virtual-service-mirroring.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v1
weight: 100
mirror:
host: reviews
subset: v2
# 执行
kubectl apply -f virtual-service-mirroring.yaml
# 登录Jaeger控制台(http://master_IP:30686)
# 从仪表盘左边面板的Service下拉列表中选择“productpage.default”,然后点击“Find Traces”,
# 点击位于最上面的最近一次追踪,查看对应最近一次访问/productpage的详细信息,
# 追踪信息由一组Span组成,每个Span对应一个Bookinfo Service。这些Service在执行/productpage请求时被调用,或是Istio内部组件。