banner
欢迎观看

通过Istio实现灰度发布

Scroll down

本文作者:丁辉

通过Istio实现灰度发布

部署测试示例 Nginx Deployment v1和v2

  1. 部署 v1 版本

    1
    kubectl apply -f https://gitee.com/offends/Kubernetes/raw/main/File/Yaml/nginx-deployment-v1.yaml
  2. 部署 v2 版本

    1
    kubectl apply -f https://gitee.com/offends/Kubernetes/raw/main/File/Yaml/nginx-deployment-v2.yaml

配合 Istio APIs 实现灰度发布

  1. 创建 Nginx Service

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    cat <<EOF | kubectl apply -f -
    apiVersion: v1
    kind: Service
    metadata:
    name: nginx-deployment-service
    namespace: default
    spec:
    selector:
    app: nginx
    ports:
    - name: http
    port: 80
    targetPort: 80
    EOF
  2. 创建 Nginx Gateway 资源

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    cat <<EOF | kubectl apply -f -
    apiVersion: networking.istio.io/v1
    kind: Gateway
    metadata:
    name: nginx-gateway
    namespace: default
    spec:
    selector:
    istio: ingressgateway
    servers:
    - port:
    number: 443
    name: https
    protocol: HTTPS
    hosts:
    - example.com # 替换为你的域名
    tls:
    mode: SIMPLE
    credentialName: example-tls # 替换为你的证书,这个 secret 必须在 istio-system 命名空间
    EOF
  3. 创建 DestinationRule 资源

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    cat <<EOF | kubectl apply -f -
    apiVersion: networking.istio.io/v1
    kind: DestinationRule
    metadata:
    name: nginx-destination
    spec:
    host: nginx-deployment-service #对应 service 名称
    subsets:
    - name: v1
    labels:
    version: v1 # Pod标签
    - name: v2
    labels:
    version: v2 # Pod标签
    EOF
  4. 创建 VirtualService 资源(限制流量全部开放给 v1)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    cat <<EOF | kubectl apply -f -
    apiVersion: networking.istio.io/v1
    kind: VirtualService
    metadata:
    name: nginx-virtualservice
    namespace: default
    spec:
    hosts:
    - example.com # 替换为你的域名
    gateways:
    - nginx-gateway
    http:
    - route:
    - destination:
    host: nginx-deployment-service.default.svc.cluster.local # 指对应的 service 名称
    port:
    number: 80
    subset: v1 # 对应 DestinationRule 中的 v1
    weight: 100 # 100%流量分发到 v1
    - destination:
    host: nginx-deployment-service.default.svc.cluster.local # 指对应的 service 名称
    port:
    number: 80
    subset: v2 # 对应 DestinationRule 中的 v2
    EOF
  5. 更新 VirtualService 资源(将 20% 流量分给 v2)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    cat <<EOF | kubectl apply -f -
    apiVersion: networking.istio.io/v1
    kind: VirtualService
    metadata:
    name: nginx-virtualservice
    namespace: default
    spec:
    hosts:
    - example.com # 替换为你的域名
    gateways:
    - nginx-gateway
    http:
    - route:
    - destination:
    host: nginx-deployment-service.default.svc.cluster.local # 指对应的 service 名称
    port:
    number: 80
    subset: v1 # 对应 DestinationRule 中的 v1
    weight: 80 # 80%流量分发到 v1
    - destination:
    host: nginx-deployment-service.default.svc.cluster.local # 指对应的 service 名称
    port:
    number: 80
    subset: v2 # 对应 DestinationRule 中的 v2
    weight: 20 # 20%流量分发到 v2
    EOF
  6. 最后将全部流量开放给 v2

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    cat <<EOF | kubectl apply -f -
    apiVersion: networking.istio.io/v1
    kind: VirtualService
    metadata:
    name: nginx-virtualservice
    namespace: default
    spec:
    hosts:
    - example.com # 替换为你的域名
    gateways:
    - nginx-gateway
    http:
    - route:
    - destination:
    host: nginx-deployment-service.default.svc.cluster.local # 指对应的 service 名称
    port:
    number: 80
    subset: v1 # 对应 DestinationRule 中的 v1
    - destination:
    host: nginx-deployment-service.default.svc.cluster.local # 指对应的 service 名称
    port:
    number: 80
    subset: v2 # 对应 DestinationRule 中的 v2
    weight: 100 # 100%流量分发到 v2
    EOF
  7. 完成灰度发布(HTTPS同理)。

要是这篇文章为您解了惑、带来了帮助,不妨用小小的打赏来支持下我的创作吧,您的鼓励就是我持续分享的最大动力哦,感谢您啦!

其他文章
cover
官方Istio使用示例
  • 70/01/01
  • 00:00
  • Kubernetes-网关
cover
Kubeasz部署Kubernetes集群
  • 70/01/01
  • 00:00
  • Kubernetes-部署文档
目录导航 置顶
  1. 1. 通过Istio实现灰度发布
    1. 1.1. 配合 Istio APIs 实现灰度发布
请输入关键词进行搜索