暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

不懂envoyfilter也敢说精通istio系列-07-负载均衡配置不止是dr

k8s实战 2021-09-27
884

1什么是负载均衡配置

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

2实战

2.1consistentHash

2.1.1httpCookie

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

2.1.2httpHeaderName

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

2.1.3useSourceIp

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

2.1.4httpQueryParameterName

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

3.1simple

3.1.1LEAST_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

3.1.2ROUND_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

3.1.2RANDOM

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

3.1.2PASSTHROUGH

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

4基于位置负载均衡

还不知道配置原理抱歉


文章转载自k8s实战,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论