k8s多集群的流量治理(3、通过 karmada + istio + argo-rollout 进行多集群编排(2023-11-12) )

本文介绍了如何使用Karmada、Istio和ArgoRollout在四个集群中进行多集群编排,包括金丝雀发布策略,如何部署Rollout、Services、IstioVirtualService和Gateway,并实现前端和后端的流量路由控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过 karmada + istio + argo-rollout 进行多集群编排(2023-11-12)

后端发布方案:

四个集群,每个集群都部署 Rollout、Services

mkdir -p ~/helloworld-rollout-yml
cat > helloworld-stable-rollout.yml << 'EOF'
apiVersion: argoproj.io/v1alpha1
kind: Rollout
metadata:
  name: helloworld-stable
  namespace: helloworld-rollout
spec:
  replicas: 4 # 副本数
  strategy:
    canary:
      steps:
      - setWeight: 20
      - pause: {} # 人工卡点
      - setWeight: 40
      - pause: {duration: 10}
      - setWeight: 60
      - pause: {duration: 10}
      - setWeight: 80
      - pause: {duration: 10}
      - setWeight: 100
      - pause: {} # 人工卡点
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      app: helloworld
  template:
    metadata:
      labels:
        app: helloworld
    spec:
      containers:
      - name: helloworld
        image: ccr.ccs.tencentyun.com/huanghuanhui/helloworld:stable
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 80
          protocol: TCP
EOF
cat > helloworld-stable-svc.yml << 'EOF'
apiVersion: v1
kind: Service
metadata:
  name: helloworld
  namespace: helloworld-rollout
  labels:
    app: helloworld
    service: helloworld
spec:
  ports:
  - port: 80
    name: http
  selector:
    app: helloworld
EOF
cat > helloworld-stable-policy.yml << 'EOF'
apiVersion: policy.karmada.io/v1alpha1
kind: PropagationPolicy
metadata:
 name: helloworld-stable-propagation
 namespace: helloworld-rollout
spec:
 resourceSelectors:
   - apiVersion: argoproj.io/v1alpha1  # 修正为 Argo Rollout 的 apiVersion
     kind: Rollout
     name: helloworld-stable
   - apiVersion: v1
     kind: Service
     name: helloworld
 placement:
   clusterAffinity:
     clusterNames:
       - k8s-master-beijing
       - k8s-master-shanghai
       - k8s-master-guangzhou
       - k8s-master-shenzhen
   replicaScheduling:
     replicaDivisionPreference: Weighted
     replicaSchedulingType: Divided
     weightPreference:
       staticWeightList:
         - targetCluster:
             clusterNames:
               - k8s-master-beijing
           weight: 1
         - targetCluster:
             clusterNames:
               - k8s-master-shanghai
           weight: 1
         - targetCluster:
             clusterNames:
               - k8s-master-guangzhou
           weight: 1
         - targetCluster:
             clusterNames:
               - k8s-master-shenzhen
           weight: 1
EOF

共部署4个副本,每个集群分配一个副本(这里以四个集群为例!!!)

前端发布方案(无金丝雀):

四个集群,每个集群都部署 Rollout、Services、Istio VirtualService 和 Istio Gateway

mkdir -p ~/helloworld-rollout-yml
cat > helloworld-stable-rollout.yml << 'EOF'
apiVersion: argoproj.io/v1alpha1
kind: Rollout
metadata:
  name: helloworld-stable
  namespace: helloworld-rollout
spec:
  replicas: 4
  strategy:
    canary:
      steps:
      - setWeight: 20
      - pause: {} # 人工卡点
      - setWeight: 40
      - pause: {duration: 10}
      - setWeight: 60
      - pause: {duration: 10}
      - setWeight: 80
      - pause: {duration: 10}
      - setWeight: 100
      - pause: {} # 人工卡点
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      app: helloworld
  template:
    metadata:
      labels:
        app: helloworld
    spec:
      containers:
      - name: helloworld
        image: ccr.ccs.tencentyun.com/huanghuanhui/helloworld:stable
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 80
          protocol: TCP
EOF
cat > helloworld-stable-svc.yml << 'EOF'
apiVersion: v1
kind: Service
metadata:
  name: helloworld
  namespace: helloworld-rollout
  labels:
    app: helloworld
    service: helloworld
spec:
  ports:
  - port: 80
    name: http
  selector:
    app: helloworld
EOF
cat > helloworld-stable-vsvc.yml << 'EOF'
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: helloworld-vsvc
  namespace: helloworld-rollout
spec:
  gateways:
  - helloworld-gateway
  hosts:
  - "helloworld.huanghuanhui.cloud"
  http:
  - name: primary
    route:
    - destination:
        host: prd-vue-svc-stable
      weight: 100
    - destination:
        host: prd-vue-svc-canary
      weight: 0
EOF
cat > helloworld-stable-gateway.yml << 'EOF'
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: helloworld-gateway
  namespace: helloworld-rollout
spec:
  selector:
    istio: ingressgateway # 默认创建的 istio ingressgateway pod 有这个 Label
  servers:
  - port:
      number: 443
      name: https
      protocol: HTTPS
    hosts:
    - "helloworld.huanghuanhui.cloud" # 匹配所有 host
    tls:
      mode: SIMPLE
      credentialName: helloworld-rollout-tls-secret
EOF
# 所有的istio的证书都放在(istio-system)命名空间下
kubectl create secret -n istio-system \
tls prd-vue-tls-secret \
--key=/root/ssl/huanghuanhui.cloud_nginx/huanghuanhui.cloud.key \
--cert=/root/ssl/huanghuanhui.cloud_nginx/huanghuanhui.cloud_bundle.crt
cat > helloworld-stable-policy.yml << 'EOF'
apiVersion: policy.karmada.io/v1alpha1
kind: PropagationPolicy
metadata:
 name: helloworld-stable-propagation
 namespace: helloworld-rollout
spec:
 resourceSelectors:
   - apiVersion: argoproj.io/v1alpha1  # 修正为 Argo Rollout 的 apiVersion
     kind: Rollout
     name: helloworld-stable
   - apiVersion: v1
     kind: Service
     name: helloworld
 placement:
   clusterAffinity:
     clusterNames:
       - k8s-master-beijing
       - k8s-master-shanghai
       - k8s-master-guangzhou
       - k8s-master-shenzhen
   replicaScheduling:
     replicaDivisionPreference: Weighted
     replicaSchedulingType: Divided
     weightPreference:
       staticWeightList:
         - targetCluster:
             clusterNames:
               - k8s-master-beijing
           weight: 1
         - targetCluster:
             clusterNames:
               - k8s-master-shanghai
           weight: 1
         - targetCluster:
             clusterNames:
               - k8s-master-guangzhou
           weight: 1
         - targetCluster:
             clusterNames:
               - k8s-master-shenzhen
           weight: 1
EOF

共部署4个副本,每个集群分配一个副本(这里以四个集群为例!!!)

前端发布方案(金丝雀):

四个集群,每个集群都部署 Rollout、Services、Istio VirtualService 和 Istio Gateway

mkdir -p ~/helloworld-rollout-yml
cat > helloworld-rollout.yml << 'EOF'
apiVersion: argoproj.io/v1alpha1
kind: Rollout
metadata:
  name: helloworld
  namespace: helloworld-rollout
spec:
  replicas: 4
  strategy:
    canary:
      canaryService: helloworld-svc-canary # 关联 canary Service
      stableService: helloworld-svc-stable # 关联 stable Service
      trafficRouting:
        managedRoutes:
          - name: "header-route-1"
        istio:
          virtualServices:
          - name: helloworld-vsvc # 关联的 Istio virtualService
            routes:
            - primary
      steps:
      - setHeaderRoute:
          name: "header-route-1"
          match:
            - headerName: "X-canary"
              headerValue:
                exact: "test-user"
      - pause: {duration: 10}
      - setCanaryScale:
          weight: 20
      - pause: {} # 人工卡点(当有新版本上线的时候,给canary版本20%流量,并且暂停新版本继续更新,当测试人员完成测试可以继续更新,从而达到前端可以一直触发构建并且直接上到金丝雀上)
      - setCanaryScale:
          weight: 40
      - pause: {duration: 10}
      - setCanaryScale:
          weight: 60
      - pause: {duration: 10}
      - setCanaryScale:
          weight: 80
      - pause: {duration: 10}
  revisionHistoryLimit: 5
  selector:
    matchLabels:
      app: helloworld
  template:
    metadata:
      labels:
        app: helloworld
    spec:
      containers:
      - name: helloworld
        image: ccr.ccs.tencentyun.com/huanghuanhui/helloworld:canary
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 80
          protocol: TCP
EOF
cat > helloworld-rollout-svc.yml << 'EOF'
apiVersion: v1
kind: Service
metadata:
  name: helloworld-svc-stable
  namespace: helloworld-rollout
  labels:
    app: helloworld
spec:
  type: ClusterIP
  ports:
  - port: 80
    targetPort: http
    protocol: TCP
    name: http
  selector:
    app: helloworld

---
apiVersion: v1
kind: Service
metadata:
  name: helloworld-svc-canary
  namespace: helloworld-rollout
  labels:
    app: helloworld
spec:
  type: ClusterIP
  ports:
  - port: 80
    targetPort: http
    protocol: TCP
    name: http
  selector:
    app: helloworld
EOF
cat > helloworld-vsvc.yml << 'EOF'
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: helloworld-vsvc
  namespace: helloworld-rollout
spec:
  gateways:
  - helloworld-gateway
  hosts:
  - "helloworld.huanghuanhui.cloud"
  http:
  - name: primary
    route:
    - destination:
        host: helloworld-svc-stable
      weight: 100
    - destination:
        host: helloworld-svc-canary
      weight: 0
EOF
cat > helloworld-gateway.yml << 'EOF'
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: helloworld-gateway
  namespace: helloworld-rollout
spec:
  selector:
    istio: ingressgateway # 默认创建的 istio ingressgateway pod 有这个 Label
  servers:
  - port:
      number: 443
      name: https
      protocol: HTTPS
    hosts:
    - "helloworld.huanghuanhui.cloud" # 匹配所有 host
    tls:
      mode: SIMPLE
      credentialName: helloworld-rollout-tls-secret
EOF
# 所有的istio的证书都放在(istio-system)命名空间下
kubectl create secret -n istio-system \
tls prd-vue-tls-secret \
--key=/root/ssl/huanghuanhui.cloud_nginx/huanghuanhui.cloud.key \
--cert=/root/ssl/huanghuanhui.cloud_nginx/huanghuanhui.cloud_bundle.crt
cat > helloworld-stable-policy.yml << 'EOF'
apiVersion: policy.karmada.io/v1alpha1
kind: PropagationPolicy
metadata:
 name: helloworld-stable-propagation
 namespace: helloworld-rollout
spec:
 resourceSelectors:
   - apiVersion: argoproj.io/v1alpha1  # 修正为 Argo Rollout 的 apiVersion
     kind: Rollout
     name: helloworld-stable
   - apiVersion: v1
     kind: Service
     name: helloworld
 placement:
   clusterAffinity:
     clusterNames:
       - k8s-master-beijing
       - k8s-master-shanghai
       - k8s-master-guangzhou
       - k8s-master-shenzhen
   replicaScheduling:
     replicaDivisionPreference: Weighted
     replicaSchedulingType: Divided
     weightPreference:
       staticWeightList:
         - targetCluster:
             clusterNames:
               - k8s-master-beijing
           weight: 1
         - targetCluster:
             clusterNames:
               - k8s-master-shanghai
           weight: 1
         - targetCluster:
             clusterNames:
               - k8s-master-guangzhou
           weight: 1
         - targetCluster:
             clusterNames:
               - k8s-master-shenzhen
           weight: 1
EOF

共部署4个副本,每个集群分配一个副本(这里以四个集群为例!!!)

===

1、ruoyi-gateway(部署 Rollout、Services、Istio VirtualService 和 Istio Gateway)

2、ruoyi-auth(部署 Rollout)

3、ruoyi-system(部署 Rollout)

4、ruoyi-vue(部署 Rollout、Services、Istio VirtualService 和 Istio Gateway)

正常的流量走vue的stable版本,连接stable的gateway

带请求头的流量走vue的canary版本,连接canary版本的gateway

访问前端canary版本,后端也走canary版本;访问前端stable版本,后端也走stable版本;

问题1:ruoyi-auth、ruoyi-system怎样区分流量???

04-03
### Karmada 多云编排 Kubernetes Karmada 是一个多云和混合云场景下的开源 Kubernetes 管理平台,专注于多集群应用的自动化管理和编排。它允许用户在多个 Kubernetes 集群上进行高效的应用程序部署和管理,而无需对现有应用程序做任何改动[^1]。 #### 功能概述 Karmada 提供的核心功能包括但不限于以下几个方面: - **集中式多云管理**:通过统一的控制平面,使用户可以像操作单个集群那样轻松管理分布在不同云环境中的多个 Kubernetes 集群[^5]。 - **高级调度能力**:支持复杂的调度策略,可以根据资源利用率、地理位置或其他自定义条件动态分配工作负载。 - **高可用性和故障恢复**:当某个目标集群不可用时,Karmada 能够自动将工作负载迁移到其他健康的集群中,从而提高系统的整体可靠性[^4]。 - **流量调度**:提供了灵活的服务网格集成选项,帮助优化跨集群服务之间的通信效率。 #### 技术架构组成 为了达成上述目标,Karmada 的设计采用了模块化的方式构建整个系统框架,主要组成部分如下: - **云平台适配器**:用于连接并桥接各种异构型态下的基础计算设施接口差异问题,确保兼容性的同时简化运维复杂度[^3]。 - **多集群管理工具**:承担起协调各个成员节点间关系的任务,并维护它们之间的一致状态同步机制。 - **联邦 API**:对外暴露标准化的操作入口以便于开发者或者管理员更便捷地完成日常管理工作流处理需求。 以下是创建一个简单的 Karmada 配置文件示例: ```yaml apiVersion: cluster.karmada.io/v1alpha1 kind: PropagationPolicy metadata: name: sample-propagation-policy spec: resourceSelectors: - apiVersion: apps/v1 kind: Deployment namespace: default name: nginx-deployment placement: clusterAffinities: operator: In clusterNames: - member-cluster-a - member-cluster-b ``` 此 YAML 文件定义了一种传播策略,指定 `nginx-deployment` 将被分发至名为 `member-cluster-a` 和 `member-cluster-b` 的两个子集当中去执行实例化过程[^2]。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值