Istio实战(七)- Bookinfo 部署

1. Istio Bookinfo示例

1.1 部署Bookinfo

复制代码
# kubectl apply -f /apps/istio/samples/bookinfo/platform/kube/bookinfo.yaml -n hr

1.2 确认Bookinfo已经部署正常

先确认以下pod和service已经被正确创建

复制代码
# kubectl get pods -n hr
NAME                              READY   STATUS    RESTARTS   AGE
details-v1-79f774bdb9-k7mqb       2/2     Running   0          7m45s
productpage-v1-6b746f74dc-bx8ml   2/2     Running   0          7m44s
ratings-v1-b6994bb9-p8794         2/2     Running   0          7m45s
reviews-v1-545db77b95-vftz4       2/2     Running   0          7m44s
reviews-v2-7bf8c9648f-gxknq       2/2     Running   0          7m44s
reviews-v3-84779c7bbc-lghnn       2/2     Running   0          7m44s
sleep-557747455f-xrt55            2/2     Running   0          154m
# kubectl get -n hr svc
NAME          TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
details       ClusterIP   10.200.210.141   <none>        9080/TCP   4m7s
productpage   ClusterIP   10.200.180.58    <none>        9080/TCP   4m6s
ratings       ClusterIP   10.200.158.218   <none>        9080/TCP   4m7s
reviews       ClusterIP   10.200.82.81     <none>        9080/TCP   4m7s
sleep         ClusterIP   10.200.210.162   <none>        80/TCP     6d19h

再确认Bookinfo工作已经正常

由于我将Bookinfo放在hr的namespace中,如果使用default命名空间或者其他命名空间,将-n hr部分替换即可

复制代码
# kubectl exec -n hr "$(kubectl get pod -n hr -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>

1.3 定义gateway

复制代码
# kubectl apply -f samples/bookinfo/networking/bookinfo-gateway.yaml -n hr
gateway.networking.istio.io/bookinfo-gateway created
virtualservice.networking.istio.io/bookinfo created

配置分析

复制代码
# istioctl analyze -n hr

✔ No validation issues found when analyzing namespace: hr.

1.4 访问测试

访问 192.168.31.163/productpage

补充:192.168.31.163的地址来自于之前配置的ingress,具体设置步骤就不在这里赘述了.

复制代码
# kubectl get svc -n istio-system |grep ingress
istio-ingressgateway   LoadBalancer   10.200.116.152   192.168.31.163   15021:36408/TCP,80:32291/TCP,443:46749/TCP,31400:60601/TCP,15443:57185/TCP,20001:52713/TCP   5d4h

1.5 kiali

2. Request Routing测试

2.1 划分子集

复制代码
# kubectl apply -f samples/bookinfo/networking/destination-rule-all.yaml -n hr

reviews,ratings,details分别划分了v1,v2,v3三个子集

复制代码
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
---

2.2 将所有流量转到v1子集

samples/bookinfo/networking/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
---

# kubectl apply -f samples/bookinfo/networking/virtual-service-all-v1.yaml -n hr
virtualservice.networking.istio.io/productpage created
virtualservice.networking.istio.io/reviews created
virtualservice.networking.istio.io/ratings created
virtualservice.networking.istio.io/details created

3. Route based on user identity

定义一个vs,名字是reviews.

如果标头是end-user,值是jason,那么就路由给reviews的v2

否则调度给reviews的v1

复制代码
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
    - reviews
  http:
  - match:
    - headers:
        end-user:
          exact: jason
    route:
    - destination:
        host: reviews
        subset: v2
  - route:
    - destination:
        host: reviews
        subset: v1

# kubectl apply -f samples/bookinfo/networking/virtual-service-reviews-test-v2.yaml -n hr
virtualservice.networking.istio.io/reviews created

测试访问

默认访问任然是v1

当用jason用户名sign in后就显示v2

这部分用jason登录的流量就走了v2,其他不是jason header的流量任然走的v1

4. Traffic Shifting

有50%的流量交给v1,其余50%的流量交给v3

复制代码
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
    - reviews
  http:
  - route:
    - destination:
        host: reviews
        subset: v1
      weight: 50
    - destination:
        host: reviews
        subset: v3
      weight: 50

# kubectl apply -f samples/bookinfo/networking/virtual-service-reviews-50-v3.yaml -n hr
virtualservice.networking.istio.io/reviews configured

多次刷新时,有50%概率刷新出来是v1,50%概率刷新到v3

将流量设置成v1 20%,v3 80%后,v1和v3的流量比逐渐变为1:4

复制代码
# vi samples/bookinfo/networking/virtual-service-reviews-50-v3.yaml
root@k8s-master-01:/apps/istio# kubectl apply -f samples/bookinfo/networking/virtual-service-reviews-50-v3.yaml -n hr
virtualservice.networking.istio.io/reviews configured

如果实际生产中,在两个版本并行一段时间后,可以将所有流量切换到v3上.

相关推荐
晚霞的不甘7 小时前
Flutter for OpenHarmony天气卡片应用:用枚举与动画打造沉浸式多城市天气浏览体验
前端·flutter·云原生·前端框架
Tadas-Gao7 小时前
TCP粘包现象的深度解析:从协议本质到工程实践
网络·网络协议·云原生·架构·tcp
切糕师学AI7 小时前
Helm Chart 是什么?
云原生·kubernetes·helm chart
陈桴浮海8 小时前
【Linux&Ansible】学习笔记合集三
linux·运维·云原生·ansible
研究司马懿10 小时前
【云原生】Gateway API高级功能
云原生·go·gateway·k8s·gateway api
陈桴浮海1 天前
Kustomize实战:从0到1实现K8s多环境配置管理与资源部署
云原生·容器·kubernetes
ShiLiu_mtx1 天前
k8s - 7
云原生·容器·kubernetes
匀泪1 天前
云原生(LVS NAT模式集群实验)
服务器·云原生·lvs
DolitD1 天前
云流技术深度剖析:国内云渲染主流技术与开源和海外厂商技术实测对比
功能测试·云原生·开源·云计算·实时云渲染
ghostwritten1 天前
春节前夕,运维的「年关」:用 Kubeowler 给集群做一次「年终体检」
运维·云原生·kubernetes