【istio系列--实验一】

1.3 Istio实验

1.3.1 Gateway和VirtualService实验

  • 开启Istio sidecar功能

    root@k8s-master-1:~# kubectl label namespace default istio-injection=enabled
    
  • 部署helloworld app

    root@k8s-master-1:~# export VERSION=$(ls ~ | grep istio)
    root@k8s-master-1:~# cd ~/$VERSION/samples/helloworld/
    root@k8s-master-1:~# kubectl apply -f helloworld.yaml
    service/helloworld created
    deployment.apps/helloworld-v1 created
    deployment.apps/helloworld-v2 created
    root@k8s-master-1:/app/istio/samples/helloworld# kubectl get pod
    NAME                                      READY   STATUS      RESTARTS        AGE
    helloworld-v1-7459d7b54b-6r85q            2/2     Running     0               65s
    helloworld-v2-654d97458-6mff5             2/2     Running     0               65s
    root@k8s-master-1:/app/istio/samples/helloworld# kubectl get svc helloworld -L app
    NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE     APP
    helloworld   ClusterIP   10.10.226.198   <none>        5000/TCP   3m25s   helloworld
    
  • 部署Gateway和VirtualService

    root@k8s-master-1:/app/istio/samples/helloworld# cat helloworld-gateway.yaml
    apiVersion: networking.istio.io/v1alpha3
    kind: Gateway
    metadata:
      name: helloworld-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: helloworld
    spec:
      hosts:
      - "*"
      gateways:
      - helloworld-gateway
      http:
      - match:
        - uri:
            exact: /hello
        route:
        - destination:
            host: helloworld
            port:
              number: 5000
    
    root@k8s-master-1:/app/istio/samples/helloworld# kubectl apply -f  helloworld-gateway.yaml
    gateway.networking.istio.io/helloworld-gateway created
    virtualservice.networking.istio.io/helloworld created
    root@k8s-master-1:/app/istio/samples/helloworld# kubectl get gw,vs
    NAME                                             AGE
    gateway.networking.istio.io/helloworld-gateway   6s
    
    NAME                                            GATEWAYS                 HOSTS   AGE
    virtualservice.networking.istio.io/helloworld   ["helloworld-gateway"]   ["*"]   6s
    
    # 注释:当访问 istio-ingressgateway SVC 的 80/tcp 端口/hello,流量转发到 helloword SVC 的 5000/tcp
    
  • 测试

    root@k8s-master-1:/app/istio/samples/helloworld#  while true; do curl -s -o /dev/null "http://192.168.4.18:31181/hello"; done
    

    在这里插入图片描述

1.3.2 DestinationRule实验

  • 通过 DestinationRule 把流量调度到 helloworld-v1 版本

    root@k8s-master-1:/app/istio/samples/helloworld# cat helloworld-dr-all-v1.yaml
    ---
    apiVersion: networking.istio.io/v1beta1
    kind: DestinationRule
    metadata:
      name: helloworld-destination
    spec:
      host: helloworld
      subsets:
      - name: v1
        labels:
          version: v1
      - name: v2
        labels:
          version: v2
    ---
    apiVersion: networking.istio.io/v1beta1
    kind: VirtualService
    metadata:
      name: helloworld
    spec:
      hosts:
      - "*"
      gateways:
      - helloworld-gateway
      http:
      - match:
        - uri:
            exact: /hello
        route:
        - destination:
            host: helloworld
            port:
              number: 5000
            subset: v1
    
  • 验证访问测试

    root@k8s-master-1:/app/istio/samples/helloworld# while true; do curl -s -o /dev/null "http://192.168.4.18:31181/hello"; done
    

在这里插入图片描述

  • 通过 DestinationRule 把 80%的访问请求调度到 helloworld-v1 版本,20%的访问请求调度到 helloworld-v2 版本

    root@k8s-master-1:/app/istio/samples/helloworld# cat helloworld-dr-20-v2.yaml
    ---
    apiVersion: networking.istio.io/v1beta1
    kind: VirtualService
    metadata:
      name: helloworld
    spec:
      hosts:
      - "*"
      gateways:
      - helloworld-gateway
      http:
      - match:
        - uri:
            exact: /hello
        route:
        - destination:
            host: helloworld
            port:
              number: 5000
            subset: v1
          weight: 80
        - destination:
            host: helloworld
            port:
              number: 5000
            subset: v2
          weight: 20
    

在这里插入图片描述

1.3.3 使用Istio进行金丝雀部署

  • 应用 Istio 规则将 10%流量交 v2 版本

    root@k8s-master-1:~# cat  helloworld-canary-allin1-10v2.yaml
    apiVersion: networking.istio.io/v1beta1
    kind: Gateway
    metadata:
      name: helloworld-gateway
    spec:
      selector:
        istio: ingressgateway # use istio default controller
      servers:
      - port:
          number: 80
          name: http
          protocol: HTTP
        hosts:
        - "*"
    ---
    apiVersion: networking.istio.io/v1beta1
    kind: DestinationRule
    metadata:
      name: helloworld-destination
    spec:
      host: helloworld
      subsets:
      - name: v1
        labels:
          version: v1
      - name: v2
        labels:
          version: v2
    ---
    apiVersion: networking.istio.io/v1beta1
    kind: VirtualService
    metadata:
      name: helloworld
    spec:
      hosts:
      - "*"
      gateways:
      - helloworld-gateway
      http:
      - match:
        - uri:
            exact: /hello
        route:
        - destination:
            host: helloworld
            port:
              number: 5000
            subset: v1
          weight: 90
        - destination:
            host: helloworld
            port:
              number: 5000
            subset: v2
          weight: 10
    root@k8s-master-1:~# kubectl apply -f helloworld-canary-allin1-10v2.yaml
    

在这里插入图片描述

  • 部署中的自动缩放

    root@k8s-master-1:~# kubectl autoscale deployment helloworld-v1 --cpu-percent=50 --min=1 --max=10
    root@k8s-master-1:~# kubectl autoscale deployment helloworld-v2 --cpu-percent=50 --min=1 --max=10
    root@k8s-master-1:~# kubectl get hpa -w
    
  • 调整 v1 和 v2 版本的权重各为 50%

root@k8s-master-1:~# cat helloworld-canary-vs-50v2.yaml
---
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: helloworld
spec:
  hosts:
  - "*"
  gateways:
  - helloworld-gateway
  http:
  - match:
    - uri:
        exact: /hello
    route:
    - destination:
        host: helloworld
        port:
          number: 5000
        subset: v1
      weight: 50
    - destination:
        host: helloworld
        port:
          number: 5000
        subset: v2
      weight: 50

在这里插入图片描述

  • 聚焦金丝雀测试

    root@k8s-master-1:~# cat specific-vs.yaml
    apiVersion: networking.istio.io/v1beta1
    kind: VirtualService
    metadata:
      name: helloworld
    spec:
      hosts:
      - "*"
      gateways:
      - helloworld-gateway
      http:
      - match:
        - headers:
            cookie:
              regex: "^(.*?;)?(email=[^;]*@some-company-name.com)(;.*)?$"
            end-user:
              exact: jason
          uri:
            exact: /hello
        route:
        - destination:
            host: helloworld
            port:
              number: 5000
            subset: v1
          weight: 50
        - destination:
            host: helloworld
            port:
              number: 5000
            subset: v2
          weight: 50
      - route:
        - destination:
            host: helloworld
            port:
              number: 5000
            subset: v1
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值