github.com/cilium/cilium@v1.16.2/Documentation/network/servicemesh/gateway-api/splitting.rst (about)

     1  .. only:: not (epub or latex or html)
     2  
     3      WARNING: You are looking at unreleased Cilium documentation.
     4      Please use the official rendered version released here:
     5      https://docs.cilium.io
     6  
     7  .. _gs_gateway_splitting:
     8  
     9  *************************
    10  Traffic Splitting Example
    11  *************************
    12  
    13  HTTP traffic splitting is the process of sending incoming traffic to multiple backend services, based on predefined weights or other criteria. 
    14  The Cilium Gateway API includes built-in support for traffic splitting, allowing users to easily distribute incoming traffic across multiple backend services. 
    15  This is very useful for canary testing or A/B scenarios.
    16  
    17  This particular example uses the Gateway API to load balance incoming traffic to different backends, starting with the same weights before testing with a 99/1 weight distribution.
    18  
    19  .. include:: ../echo-app.rst
    20  
    21  Deploy the Cilium Gateway
    22  =========================
    23  
    24  You can find an example Gateway and HTTPRoute definition in ``splitting.yaml``:
    25  
    26  .. literalinclude:: ../../../../examples/kubernetes/gateway/splitting.yaml
    27  
    28  Notice the even 50/50 split between the two Services.
    29  
    30  Deploy the Gateway and the HTTPRoute:
    31  
    32  .. parsed-literal::
    33  
    34      $ kubectl apply -f \ |SCM_WEB|\/examples/kubernetes/gateway/splitting.yaml
    35  
    36  The preceding example creates a Gateway named ``cilium-gw`` that listens on port 80.
    37  A single route is defined and includes two different ``backendRefs`` (``echo-1`` and ``echo-2``) and weights associated with them.
    38  
    39  .. code-block:: shell-session
    40  
    41      $ kubectl get gateway cilium-gw
    42      NAME        CLASS    ADDRESS          PROGRAMMED   AGE
    43      cilium-gw   cilium   172.18.255.200                8s
    44  
    45  .. Note::
    46  
    47      Some providers like EKS use a fully-qualified domain name rather than an IP address.
    48  
    49  Even traffic split
    50  ==================
    51  
    52  Now that the Gateway is ready, you can make HTTP requests to the services.
    53  
    54  .. code-block:: shell-session
    55  
    56      $ GATEWAY=$(kubectl get gateway cilium-gw -o jsonpath='{.status.addresses[0].value}')
    57      $ curl --fail -s http://$GATEWAY/echo
    58  
    59      Hostname: echo-1-7d88f779b-m6r46
    60  
    61      Pod Information:
    62          node name:      kind-worker2
    63          pod name:       echo-1-7d88f779b-m6r46
    64          pod namespace:  default
    65          pod IP: 10.0.2.15
    66  
    67      Server values:
    68          server_version=nginx: 1.12.2 - lua: 10010
    69  
    70      Request Information:
    71          client_address=10.0.2.252
    72          method=GET
    73          real path=/echo
    74          query=
    75          request_version=1.1
    76          request_scheme=http
    77          request_uri=http://172.18.255.200:8080/echo
    78  
    79      Request Headers:
    80          accept=*/*  
    81          host=172.18.255.200  
    82          user-agent=curl/7.81.0  
    83          x-forwarded-proto=http  
    84          x-request-id=ee152a07-2be2-4539-b74d-ebcebf912907  
    85  
    86      Request Body:
    87          -no body in request-
    88  
    89  Notice that the reply includes the name of the Pod that received the query. For example:
    90  
    91  .. code-block:: shell-session
    92  
    93      Hostname: echo-2-5bfb6668b4-2rl4t
    94  
    95  Repeat the command several times.
    96  You should see the reply balanced evenly across both Pods and Nodes.
    97  Verify that traffic is evenly split across multiple Pods by running a loop and counting the requests:
    98  
    99  .. code-block:: shell-session
   100  
   101      while true; do curl -s -k "http://$GATEWAY/echo" >> curlresponses.txt ;done
   102  
   103  Stop the loop with ``Ctrl+C``.
   104  Verify that the responses are more or less evenly distributed.
   105  
   106  .. code-block:: shell-session
   107  
   108      $ cat curlresponses.txt| grep -c "Hostname: echo-1"
   109      1221
   110      $ cat curlresponses.txt| grep -c "Hostname: echo-2"
   111      1162
   112  
   113  Uneven (99/1) traffic split
   114  ===========================
   115  
   116  Update the HTTPRoute weights, either by using ``kubectl edit httproute`` or by updating the value in the original manifest before reapplying it to. For example, set ``99`` for echo-1 and ``1`` for echo-2:
   117  
   118  .. code-block:: shell-session
   119  
   120      backendRefs:
   121      - kind: Service
   122        name: echo-1
   123        port: 8080
   124        weight: 99
   125      - kind: Service
   126        name: echo-2
   127        port: 8090
   128        weight: 1
   129  
   130  
   131  Verify that traffic is unevenly split across multiple Pods by running a loop and counting the requests:
   132  
   133  .. code-block:: shell-session
   134  
   135      while true; do curl -s -k "http://$GATEWAY/echo" >> curlresponses991.txt ;done
   136  
   137  Stop the loop with ``Ctrl+C``.
   138  Verify that responses are more or less evenly distributed.
   139  
   140  .. code-block:: shell-session
   141  
   142      $ cat curlresponses991.txt| grep -c "Hostname: echo-1"
   143      24739
   144      $ cat curlresponses991.txt| grep -c "Hostname: echo-2"
   145      239