成功最有效的方法就是向有经验的人学习!

负载均衡配置的两种方式

什么是负载均衡配置

isito负载均衡有三种类型,分别是一致性hash,简单负载均衡,基于位置的负载均衡。istio中负载均衡是通过destinationrule实现的。用envoyfilter实现负载均衡主要是通过修改cluster和路由。

实战

consistentHash

httpCookie

destinationrule实现

apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: productpage
spec:
  host: productpage
  trafficPolicy:
    loadBalancer:
      consistentHash:         
        httpCookie:
          name: user
          ttl: 0s

envoyfilter实现

cat << EOF > ef-lb-consistentHash-httpCookie.yaml
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: match
spec:
  workloadSelector:
    labels:
      istio: ingressgateway
  configPatches:
  - applyTo: NETWORK_FILTER
    match:
      listener:
        #name: 0.0.0.0_8080  
        portNumber: 8080
        filterChain:
          filter:
            name: "envoy.filters.network.http_connection_manager"
    patch:
      operation: MERGE
      value:
        name: envoy.filters.network.http_connection_manager
        typed_config:
          "@type": "type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager"
          codec_type: AUTO
          stat_prefix: ingress_http
          route_config:
            name: http.8080
            virtual_hosts:
            - name: “*.80”
              domains:
              - "*"
              routes:
              - match:
                  caseSensitive: true
                  prefix: /productpage
                route:
                  cluster: outbound|9080||productpage.slime.svc.cluster.local
                  hash_policy:
                  - cookie:
                      name: user
                      ttl: "0"
  - applyTo: CLUSTER
    patch:
      operation: MERGE
      value: 
        name: "outbound|9080||productpage.slime.svc.cluster.local"
        type: EDS
        lb_policy: RING_HASH
        ring_hash_lb_config:
          minimum_ring_size: 1024
        eds_cluster_config:
          service_name: outbound|9080||productpage.slime.svc.cluster.local
        metadata:
          filter_metadata:
            istio:
              default_original_port: 9080
              services:
                name: productpage
                host: productpage.slime.svc.cluster.local
                namespace: slime
EOF

kubectl apply -f  ef-lb-consistentHash-httpCookie.yaml -n istio-system --context context-cluster1

httpHeaderName

destinationrule实现

apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: productpage
spec:
  host: productpage
  trafficPolicy:
    loadBalancer:
      consistentHash:
        httpHeaderName: end-user

envoyfilter实现

cat << EOF > ef-lb-consistentHash-httpHeaderName.yaml
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: match
spec:
  workloadSelector:
    labels:
      istio: ingressgateway
  configPatches:
  - applyTo: NETWORK_FILTER
    match:
      listener:
        #name: 0.0.0.0_8080  
        portNumber: 8080
        filterChain:
          filter:
            name: "envoy.filters.network.http_connection_manager"
    patch:
      operation: MERGE
      value:
        name: envoy.filters.network.http_connection_manager
        typed_config:
          "@type": "type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager"
          codec_type: AUTO
          stat_prefix: ingress_http
          route_config:
            name: http.8080
            virtual_hosts:
            - name: “*.80”
              domains:
              - "*"
              routes:
              - match:
                  caseSensitive: true
                  prefix: /productpage
                route:
                  cluster: outbound|9080||productpage.slime.svc.cluster.local
                  hash_policy:
                  - header:
                      header_name: end-user
  - applyTo: CLUSTER
    patch:
      operation: MERGE
      value: 
        name: "outbound|9080||productpage.slime.svc.cluster.local"
        type: EDS
        lb_policy: RING_HASH
        ring_hash_lb_config:
          minimum_ring_size: 1024
        eds_cluster_config:
          service_name: outbound|9080||productpage.slime.svc.cluster.local
        metadata:
          filter_metadata:
            istio:
              default_original_port: 9080
              services:
                name: productpage
                host: productpage.slime.svc.cluster.local
                namespace: slime
EOF

kubectl apply -f  ef-lb-consistentHash-httpHeaderName.yaml -n istio-system --context context-cluster1

useSourceIp

destinationrule实现

apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: productpage
spec:
  host: productpage
  trafficPolicy:
    loadBalancer:
      consistentHash:
        useSourceIp: true

envoyfilter实现

cat << EOF > ef-lb-consistentHash-useSourceIp.yaml
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: match
spec:
  workloadSelector:
    labels:
      istio: ingressgateway
  configPatches:
  - applyTo: NETWORK_FILTER
    match:
      listener:
        #name: 0.0.0.0_8080  
        portNumber: 8080
        filterChain:
          filter:
            name: "envoy.filters.network.http_connection_manager"
    patch:
      operation: MERGE
      value:
        name: envoy.filters.network.http_connection_manager
        typed_config:
          "@type": "type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager"
          codec_type: AUTO
          stat_prefix: ingress_http
          route_config:
            name: http.8080
            virtual_hosts:
            - name: “*.80”
              domains:
              - "*"
              routes:
              - match:
                  caseSensitive: true
                  prefix: /productpage
                route:
                  cluster: outbound|9080||productpage.slime.svc.cluster.local
                  hash_policy:
                  - connection_properties:
                      source_ip: true
  - applyTo: CLUSTER
    patch:
      operation: MERGE
      value: 
        name: "outbound|9080||productpage.slime.svc.cluster.local"
        type: EDS
        lb_policy: RING_HASH
        ring_hash_lb_config:
          minimum_ring_size: 1024
        eds_cluster_config:
          service_name: outbound|9080||productpage.slime.svc.cluster.local
        metadata:
          filter_metadata:
            istio:
              default_original_port: 9080
              services:
                name: productpage
                host: productpage.slime.svc.cluster.local
                namespace: slime
EOF

kubectl apply -f  ef-lb-consistentHash-useSourceIp.yaml -n istio-system --context context-cluster1

httpQueryParameterName

destinationrule实现

apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: productpage
spec:
  host: productpage
  trafficPolicy:
    loadBalancer:
      consistentHash:
        httpQueryParameterName: test

envoyfilter实现

cat << EOF > ef-lb-consistentHash-httpQueryParameterName.yaml
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: match
spec:
  workloadSelector:
    labels:
      istio: ingressgateway
  configPatches:
  - applyTo: NETWORK_FILTER
    match:
      listener:
        #name: 0.0.0.0_8080  
        portNumber: 8080
        filterChain:
          filter:
            name: "envoy.filters.network.http_connection_manager"
    patch:
      operation: MERGE
      value:
        name: envoy.filters.network.http_connection_manager
        typed_config:
          "@type": "type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager"
          codec_type: AUTO
          stat_prefix: ingress_http
          route_config:
            name: http.8080
            virtual_hosts:
            - name: “*.80”
              domains:
              - "*"
              routes:
              - match:
                  caseSensitive: true
                  prefix: /productpage
                route:
                  cluster: outbound|9080||productpage.slime.svc.cluster.local
                  hash_policy:
                  - query_parameter:
                      name: test
  - applyTo: CLUSTER
    patch:
      operation: MERGE
      value: 
        name: "outbound|9080||productpage.slime.svc.cluster.local"
        type: EDS
        lb_policy: RING_HASH
        ring_hash_lb_config:
          minimum_ring_size: 1024
        eds_cluster_config:
          service_name: outbound|9080||productpage.slime.svc.cluster.local
        metadata:
          filter_metadata:
            istio:
              default_original_port: 9080
              services:
                name: productpage
                host: productpage.slime.svc.cluster.local
                namespace: slime
EOF

kubectl apply -f  ef-lb-consistentHash-httpQueryParameterName.yaml -n istio-system --context context-cluster1

simple

LEAST_CONN

destinationrule实现

apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: productpage
spec:
  host: productpage
  trafficPolicy:
    loadBalancer:
      simple: LEAST_CONN

envoyfilter实现

cat << EOF > ef-lb-simple-LEAST_CONN.yaml
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: match
spec:
  workloadSelector:
    labels:
      istio: ingressgateway
  configPatches:
  - applyTo: CLUSTER
    patch:
      operation: MERGE
      value: 
        name: "outbound|9080||productpage.slime.svc.cluster.local"
        type: EDS
        lb_policy: LEAST_REQUEST
        eds_cluster_config:
          service_name: outbound|9080||productpage.slime.svc.cluster.local
        metadata:
          filter_metadata:
            istio:
              default_original_port: 9080
              services:
                name: productpage
                host: productpage.slime.svc.cluster.local
                namespace: slime
EOF

kubectl apply -f  ef-lb-simple-LEAST_CONN.yaml -n istio-system --context context-cluster1

ROUND_ROBIN

destinationrule实现

apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: productpage
spec:
  host: productpage
  trafficPolicy:
    loadBalancer:
      simple: ROUND_ROBIN

envoyfilter实现

cat << EOF > ef-lb-simple-ROUND_ROBIN.yaml
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: match
spec:
  workloadSelector:
    labels:
      istio: ingressgateway
  configPatches:
  - applyTo: CLUSTER
    patch:
      operation: MERGE
      value: 
        name: "outbound|9080||productpage.slime.svc.cluster.local"
        type: EDS
        lb_policy: ROUND_ROBIN
        eds_cluster_config:
          service_name: outbound|9080||productpage.slime.svc.cluster.local
        metadata:
          filter_metadata:
            istio:
              default_original_port: 9080
              services:
                name: productpage
                host: productpage.slime.svc.cluster.local
                namespace: slime
EOF

kubectl apply -f  ef-lb-simple-ROUND_ROBIN.yaml -n istio-system --context context-cluster1

RANDOM

destinationrule实现

apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: productpage
spec:
  host: productpage
  trafficPolicy:
    loadBalancer:
      simple: RANDOM

envoyfilter实现

cat << EOF > ef-lb-simple-RANDOM.yaml
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: match
spec:
  workloadSelector:
    labels:
      istio: ingressgateway
  configPatches:
  - applyTo: CLUSTER
    patch:
      operation: MERGE
      value: 
        name: "outbound|9080||productpage.slime.svc.cluster.local"
        type: EDS
        lb_policy: RANDOM
        eds_cluster_config:
          service_name: outbound|9080||productpage.slime.svc.cluster.local
        metadata:
          filter_metadata:
            istio:
              default_original_port: 9080
              services:
                name: productpage
                host: productpage.slime.svc.cluster.local
                namespace: slime
EOF

kubectl apply -f  ef-lb-simple-RANDOM.yaml -n istio-system --context context-cluster1

PASSTHROUGH

destinationrule实现

apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: productpage
spec:
  host: productpage
  trafficPolicy:
    loadBalancer:
      simple: PASSTHROUGH

envoyfilter实现

cat << EOF > ef-lb-simple-PASSTHROUGH.yaml
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: match
spec:
  workloadSelector:
    labels:
      istio: ingressgateway
  configPatches:
  - applyTo: CLUSTER
    patch:
      operation: MERGE
      value: 
        name: "outbound|9080||productpage.slime.svc.cluster.local"
        type: ORIGINAL_DST
        lb_policy: CLUSTER_PROVIDED
        eds_cluster_config:
          service_name: outbound|9080||productpage.slime.svc.cluster.local
        metadata:
          filter_metadata:
            istio:
              default_original_port: 9080
              services:
                name: productpage
                host: productpage.slime.svc.cluster.local
                namespace: slime
EOF

kubectl apply -f  ef-lb-simple-PASSTHROUGH.yaml -n istio-system --context context-cluster1
赞(0) 打赏
未经允许不得转载:陈桂林博客 » 负载均衡配置的两种方式
分享到

大佬们的评论 抢沙发

全新“一站式”建站,高质量、高售后的一条龙服务

微信 抖音 支付宝 百度 头条 快手全平台打通信息流

橙子建站.极速智能建站8折购买虚拟主机

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册