tencent cloud

Feedback

Locality Load Balancing

Last updated: 2023-12-26 11:36:12

    Overview

    This document describes how to deploy another set of website businesses in a cluster in another AZ to increase availability when the business expands. In particular, two sets of the same website businesses are available in two clusters at the same time. Locality load balancing is as shown below:
    
    

    Directions

    When the two sets of website businesses run normally, the ingress gateway will preferentially route traffic to the frontend service in the local region or AZ, even though the other cluster also has the frontend service. In addition, the frontend service will access the user, product, order, and cart services in the same AZ, and the order and cart services will access the stock service in the same AZ.
    In Kubernetes, the Pod region is determined by the region and zone labels of the deployed node. The zone label is set for a workload in the YAML content of the demo application. First, deploy all the website services to the cluster (sub-cluster) in another AZ:
    apiVersion: v1
    kind: Namespace
    metadata:
    name: base
    labels:
    istio.io/rev: 1-6-9
    spec:
    finalizers:
    - kubernetes
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: frontend
    namespace: base
    labels:
    app: frontend
    spec:
    replicas: 1
    selector:
    matchLabels:
    app: frontend
    template:
    metadata:
    labels:
    app: frontend
    spec:
    containers:
    - name: frontend
    image: ccr.ccs.tencentyun.com/chloeyhuang/demo:v202007101540
    imagePullPolicy: Always
    env:
    - name: POD_NAME
    valueFrom:
    fieldRef:
    fieldPath: metadata.name
    - name: REGION
    value: "guangzhou-zoneB"
    ports:
    - containerPort: 80
    ---
    
    apiVersion: v1
    kind: Service
    metadata:
    name: frontend
    namespace: base
    labels:
    app: frontend
    spec:
    ports:
    - port: 80
    name: http
    selector:
    app: frontend
    ---
    
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: product-v1
    namespace: base
    labels:
    app: product
    version: v1
    spec:
    replicas: 1
    selector:
    matchLabels:
    app: product
    version: v1
    template:
    metadata:
    labels:
    app: product
    version: v1
    spec:
    containers:
    - name: product
    image: ccr.ccs.tencentyun.com/zhulei/testproduct1:v1
    imagePullPolicy: Always
    env:
    - name: POD_NAME
    valueFrom:
    fieldRef:
    fieldPath: metadata.name
    - name: REGION
    value: "guangzhou-zoneB"
    ports:
    - containerPort: 7000
    ---
    
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: product-v2
    namespace: base
    labels:
    app: product
    version: v2
    spec:
    replicas: 1
    selector:
    matchLabels:
    app: product
    version: v2
    template:
    metadata:
    labels:
    app: product
    version: v2
    spec:
    containers:
    - name: product
    image: ccr.ccs.tencentyun.com/zhulei/testproduct2:v1
    imagePullPolicy: Always
    env:
    - name: POD_NAME
    valueFrom:
    fieldRef:
    fieldPath: metadata.name
    - name: REGION
    value: "guangzhou-zoneB"
    ports:
    - containerPort: 7000
    ---
    
    apiVersion: v1
    kind: Service
    metadata:
    name: product
    namespace: base
    labels:
    app: product
    spec:
    ports:
    - port: 7000
    name: http
    selector:
    app: product
    ---
    
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: user
    namespace: base
    labels:
    app: user
    spec:
    replicas: 1
    selector:
    matchLabels:
    app: user
    template:
    metadata:
    labels:
    app: user
    spec:
    containers:
    - name: user
    image: ccr.ccs.tencentyun.com/zhulei/testuser:v1
    imagePullPolicy: Always
    env:
    - name: POD_NAME
    valueFrom:
    fieldRef:
    fieldPath: metadata.name
    - name: REGION
    value: "guangzhou-zoneB"
    ports:
    - containerPort: 7000
    ---
    
    apiVersion: v1
    kind: Service
    metadata:
    name: user
    namespace: base
    labels:
    app: user
    spec:
    ports:
    - port: 7000
    name: http
    selector:
    app: user
    ---
    
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: stock
    namespace: base
    labels:
    app: stock
    spec:
    replicas: 1
    selector:
    matchLabels:
    app: stock
    template:
    metadata:
    labels:
    app: stock
    spec:
    containers:
    - name: stock
    image: ccr.ccs.tencentyun.com/zhulei/teststock:v1
    imagePullPolicy: Always
    env:
    - name: POD_NAME
    valueFrom:
    fieldRef:
    fieldPath: metadata.name
    - name: REGION
    value: "guangzhou-zoneB"
    ports:
    - containerPort: 7000
    ---
    
    apiVersion: v1
    kind: Service
    metadata:
    name: stock
    namespace: base
    labels:
    app: stock
    spec:
    ports:
    - port: 7000
    name: http
    selector:
    app: stock
    ---
    
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: cart
    namespace: base
    labels:
    app: cart
    spec:
    replicas: 3
    selector:
    matchLabels:
    app: cart
    template:
    metadata:
    labels:
    app: cart
    spec:
    containers:
    - name: cart
    image: ccr.ccs.tencentyun.com/zhulei/testcart:v1
    imagePullPolicy: Always
    env:
    - name: POD_NAME
    valueFrom:
    fieldRef:
    fieldPath: metadata.name
    - name: REGION
    value: "guangzhou-zoneB"
    ports:
    - containerPort: 7000
    protocol: TCP
    ---
    
    apiVersion: v1
    kind: Service
    metadata:
    name: cart
    namespace: base
    labels:
    app: cart
    spec:
    ports:
    - name: http
    port: 7000
    protocol: TCP
    selector:
    app: cart
    type: ClusterIP
    ---
    
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: order-v1
    namespace: base
    labels:
    app: order
    version: v1
    spec:
    replicas: 1
    selector:
    matchLabels:
    app: order
    version: v1
    template:
    metadata:
    labels:
    app: order
    version: v1
    spec:
    containers:
    - name: order
    image: ccr.ccs.tencentyun.com/zhulei/testorder1:v1
    imagePullPolicy: Always
    env:
    - name: POD_NAME
    valueFrom:
    fieldRef:
    fieldPath: metadata.name
    - name: REGION
    value: "guangzhou-zoneB"
    ports:
    - containerPort: 7000
    protocol: TCP
    ---
    
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: order-v2
    namespace: base
    labels:
    app: order
    version: v2
    spec:
    replicas: 1
    selector:
    matchLabels:
    app: order
    version: v2
    template:
    metadata:
    labels:
    app: order
    version: v2
    spec:
    containers:
    - name: order
    image: ccr.ccs.tencentyun.com/zhulei/testorder2:v1
    imagePullPolicy: Always
    env:
    - name: POD_NAME
    valueFrom:
    fieldRef:
    fieldPath: metadata.name
    - name: REGION
    value: "guangzhou-zoneB"
    ports:
    - containerPort: 7000
    protocol: TCP
    ---
    
    apiVersion: v1
    kind: Service
    metadata:
    name: order
    namespace: base
    labels:
    app: order
    spec:
    ports:
    - name: http
    port: 7000
    protocol: TCP
    selector:
    app: order
    type: ClusterIP
    After the deployment, locality load balancing will not take effect if health check is not configured. In this case, services are called randomly in the two AZs, without adherence to the nearby access principle. The order service calls the stock service in another AZ as shown below:
    
    
    To enable locality load balancing for service access, you need to configure the health check feature for all the services by submitting the following YAML file to the primary cluster:
    apiVersion: networking.istio.io/v1alpha3
    kind: DestinationRule
    metadata:
    name: cart
    namespace: base
    spec:
    host: cart
    trafficPolicy:
    loadBalancer:
    consistentHash:
    httpHeaderName: UserID
    outlierDetection:
    consecutiveErrors: 5
    interval: 10000ms
    baseEjectionTime: 30000ms
    maxEjectionPercent: 10
    minHealthPercent: 50
    exportTo:
    - '*'
    
    ---
    apiVersion: networking.istio.io/v1alpha3
    kind: DestinationRule
    metadata:
    name: frontend
    namespace: base
    spec:
    host: frontend
    trafficPolicy:
    outlierDetection:
    consecutiveErrors: 5
    interval: 10000ms
    baseEjectionTime: 30000ms
    maxEjectionPercent: 10
    minHealthPercent: 50
    exportTo:
    - '*'
    
    ---
    apiVersion: networking.istio.io/v1alpha3
    kind: DestinationRule
    metadata:
    name: order
    namespace: base
    spec:
    host: order
    trafficPolicy:
    outlierDetection:
    consecutiveErrors: 5
    interval: 10000ms
    baseEjectionTime: 30000ms
    maxEjectionPercent: 10
    minHealthPercent: 50
    subsets:
    - name: v1
    labels:
    version: v1
    - name: v2
    labels:
    version: v2
    exportTo:
    - '*'
    
    ---
    apiVersion: networking.istio.io/v1alpha3
    kind: DestinationRule
    metadata:
    name: product
    namespace: base
    spec:
    host: product
    trafficPolicy:
    outlierDetection:
    consecutiveErrors: 5
    interval: 10000ms
    baseEjectionTime: 30000ms
    maxEjectionPercent: 10
    minHealthPercent: 50
    subsets:
    - name: v1
    labels:
    version: v1
    - name: v2
    labels:
    version: v2
    
    ---
    apiVersion: networking.istio.io/v1alpha3
    kind: DestinationRule
    metadata:
    name: stock
    namespace: base
    spec:
    host: stock
    trafficPolicy:
    outlierDetection:
    consecutiveErrors: 5
    interval: 10000ms
    baseEjectionTime: 30000ms
    maxEjectionPercent: 10
    minHealthPercent: 50
    exportTo:
    - '*'
    
    ---
    apiVersion: networking.istio.io/v1alpha3
    kind: DestinationRule
    metadata:
    name: user
    namespace: base
    spec:
    host: user
    trafficPolicy:
    outlierDetection:
    consecutiveErrors: 5
    interval: 10000ms
    baseEjectionTime: 30000ms
    maxEjectionPercent: 10
    minHealthPercent: 50
    exportTo:
    - '*'
    After health check is configured, when a user accesses a website service, browses a product page, adds an item to the cart, or places an order through the edge gateway in the cluster of AZ A, the gateway will route the traffic to the frontend service in the same AZ, and the frontend service will also call the user, cart, order, and stock services in the same AZ. Similarly, when a user accesses a website service through the edge gateway of AZ B, the request will be routed to the frontend service in AZ B, and services in AZ B will be called. You can view the AZ information of the currently called service in the floating window in the bottom-left corner of the demo page. Locality load balancing is as shown below:
    
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support