ToB企服应用市场:ToB评测及商务社交产业平台

标题: Enhancing K8s Gateway API with Easegress Without Changing a Single Lin [打印本页]

作者: 种地    时间: 2024-11-22 03:52
标题: Enhancing K8s Gateway API with Easegress Without Changing a Single Lin
In the article “Revolutionize Your Kubernetes Experience with Easegress: Kubernetes Gateway API”, we explored the powerful capabilities of the Kubernetes Gateway API. Today, we will present how to use the flexibility of Kubernetes Gateway to enhance its functionalities by using existing filters and resilience policies in Easegress without changing a single line of code.
   Through this article, you will learn how to equip the Kubernetes Gateway API with resilient fault-tolerance capabilities without modifying any code.
  Why Enhance the K8s Gateway API?

We already know that Easegress possesses robust resilient fault-tolerance features, including circuit breaking, rate limiting, and retries. With these features, Easegress can effectively protect backend services. However, in the current Kubernetes Gateway API standards, the protection mechanisms for backend services are not clearly defined. The standards are more about traffic forwarding, load balancing, redirection, and so on. So, how can we implement protection for backend services in Kubernetes Gateway? How can we equip the Kubernetes Gateway API with capabilities like circuit breaking, rate limiting, and retries? This is the key question we need to explore today.
Kubernetes Gateway ExtensionRef: The Glue Between Kubernetes and Easegress

First, let’s understand how the Kubernetes Gateway API, through the ingenious configuration of ExtensionRef [1], provides a way to implement custom functionalities. Below is an example of an HTTPRoute, demonstrating how to reference resources within a cluster:"
  1. apiVersion: gateway.networking.k8s.io/v1
  2. kind: HTTPRoute
  3. metadata:
  4.   name: httproute-extension
  5. spec:
  6.   rules:
  7.   - matches:
  8.     - path:
  9.         value: /test
  10.     filters:
  11.     - type: ExtensionRef
  12.       # Referencing the FilterSpec resource through ExtensionRef.
  13.       extensionRef:
  14.         group: "easegress.megaease.com"
  15.         kind: "FilterSpec"
  16.         name: "rate-limiter"
  17.     backendRefs:
  18.     - name: service-a
  19.       port: 8080
复制代码
This ExtensionRef references a ‘FilterSpec’ resource named ‘rate-limiter’ in the ’easegress.megaease.com’ group. This configuration will be recognized by the Easegress Gateway Controller [2] and transformed into the corresponding Easegress settings. This expands the functionality of the Kubernetes Gateway API, enabling the HTTPRoute to have rate limiting capabilities.
Custom Resource Definitions: Balancing Security and Flexibility

To seamlessly integrate the advanced functionalities of Easegress, we chose Custom Resource Definition (CRD) as our solution. Compared to directly using ConfigMap, it has a smaller impact and offers better flexibility. Below is the corresponding CRD configuration:
  1. apiVersion: apiextensions.k8s.io/v1
  2. kind: CustomResourceDefinition
  3. metadata:
  4.   name: filterspecs.easegress.megaease.com
  5. spec:
  6.   group: easegress.megaease.com
  7.   versions:
  8.     - name: v1
  9.       served: true
  10.       storage: true
  11.       schema:
  12.         openAPIV3Schema:
  13.           type: object
  14.           properties:
  15.             spec:
  16.               type: object
  17.               properties:
  18.                 name:
  19.                   type: string
  20.                 kind:
  21.                   type: string
  22.                 spec:
  23.                   type: string
  24.   scope: Namespaced
  25.   names:
  26.     plural: filterspecs
  27.     singular: filterspec
  28.     kind: FilterSpec
复制代码
In this CustomResourceDefinition, we defined the ’easegress.megaease.com’ group and the ‘FilterSpec’ kind. Our definition is designed with compatibility in mind, retaining only the three most essential attributes: name, kind, and spec. Where name and kind are common to all Easegress Filters, and spec is the specific configuration of the Filter, where the corresponding yaml configuration can be placed for use.
Practical Exercise

Next, we will take RateLimiter [3] and ResponseAdaptor [4] as examples, which are two of the many Filters provided by Easegress.
First, let’s create the corresponding Kubernetes resources:
  1. apiVersion: easegress.megaease.com/v1
  2. kind: FilterSpec
  3. metadata:
  4.   name: rate-limiter
  5. spec:
  6.   name: rate-limiter
  7.   kind: RateLimiter
  8.   spec: |
  9.     policies:
  10.     - name: policy
  11.       limitRefreshPeriod: 5000ms
  12.       limitForPeriod: 1
  13.     defaultPolicyRef: policy
  14.     urls:
  15.     - url:
  16.         prefix: /
  17.       policyRef: policy   
  18. ---
  19. apiVersion: easegress.megaease.com/v1
  20. kind: FilterSpec
  21. metadata:
  22.   name: response-adaptor
  23. spec:
  24.   name: response-adaptor
  25.   kind: ResponseAdaptor
  26.   spec: |
  27.     header:
  28.       add:
  29.         X-Eg-Response-Adaptor: "true"   
复制代码
This RateLimiter allows only one request to pass in a 5-second period. The ResponseAdaptor adds an X-Eg-Response-Adaptor header to the HTTP response.
To use these extensions in HTTPRoute, you simply need to reference these Filters when creating the HTTPRoute. A specific example is as follows:
  1. apiVersion: gateway.networking.k8s.io/v1
  2. kind: HTTPRoute
  3. metadata:
  4.   name: example-route-2
  5. spec:
  6.   parentRefs:
  7.   - kind: Gateway
  8.     name: example-gateway
  9.     sectionName: example-listener
  10.   rules:
  11.   - matches:
  12.     - path:
  13.         value: /test
  14.     filters:
  15.     - type: ExtensionRef
  16.       extensionRef:
  17.         # use rate-limiter
  18.         group: "easegress.megaease.com"
  19.         kind: "FilterSpec"
  20.         name: "rate-limiter"
  21.     - type: ExtensionRef
  22.       extensionRef:
  23.         # use response-adaptor
  24.         group: "easegress.megaease.com"
  25.         kind: "FilterSpec"
  26.         name: "response-adaptor"
  27.     backendRefs:
  28.     - name: hello-service
  29.       port: 60002
复制代码
Thus, after creating this HTTPRoute, our Easegress Gateway Controller will incorporate the specified rate limiter and response adaptor by reference. This endows the HTTPRoute with the capabilities of rate limiting and response modification.
Next, we perform some simple tests. The environment we use is minikube, and we map the port of the Gateway to nodePort 30081. Then we login for testing using minikube ssh. More details on the configuration can be found in our official documentation [2].
  1. docker@minikube:~$ curl http://127.0.0.1:30081/test -v
  2. ...
  3. < Date: Thu, 23 Nov 2023 02:57:59 GMT
  4. < X-Eg-Response-Adaptor: true  # ResponseAdaptor works
  5. < Connection: close
  6. <
  7. Hello, world!
  8. Version: 2.0.0
  9. Hostname: hello-deployment-688d8666c-xl9sb
  10. * Closing connection 0
  11. docker@minikube:~$ curl http://127.0.0.1:30081/test -v
  12. ...
  13. < HTTP/1.1 429 Too Many Requests
  14. < X-Eg-Rate-Limiter: too-many-requests  # RateLimiter works
  15. < Date: Thu, 23 Nov 2023 02:58:00 GMT
  16. ...
复制代码
Our test results show that the first request is successful and includes the X-Eg-Response-Adaptor header, while the second request is rejected due to the effect of the rate limiter.
Circuit Breaker and Retry Strategies

Furthermore, we have also provided definitions for circuit breakers and retry strategies [5], further enhancing the resilience and reliability of the network.
  1. apiVersion: easegress.megaease.com/v1
  2. kind: FilterSpec
  3. metadata:
  4.   name: circuit-breaker
  5. spec:
  6.   name: circuit-breaker
  7.   kind: CircuitBreaker
  8.   spec: |
  9.     slidingWindowType: TIME_BASED
  10.     failureRateThreshold: 60
  11.     slidingWindowSize: 200   
  12. ---
  13. apiVersion: easegress.megaease.com/v1
  14. kind: FilterSpec
  15. metadata:
  16.   name: retry
  17. spec:
  18.   name: retry
  19.   kind: Retry
  20.   spec: |
  21.     maxAttempts: 3
  22.     waitDuration: 500ms   
复制代码
Through this method, we can easily acquire various advanced functionalities of Easegress in Kubernetes Gateway.
[1] Kubernetes Gateway ExtensionRef https://gateway-api.sigs.k8s.io/reference/spec/#gateway.networking.k8s.io/v1.LocalObjectReference
[2] Easegress Gateway Controller https://github.com/megaease/easegress/blob/main/docs/04.Cloud-Native/4.2.Gateway-API.md
[3] Easegress RateLimiter Filter https://github.com/megaease/easegress/blob/main/docs/07.Reference/7.02.Filters.md#ratelimiter
[4] Easegress ResponseAdaptor Filter https://github.com/megaease/easegress/blob/main/docs/07.Reference/7.02.Filters.md#responseadaptor
[5] Easegress Resilience https://github.com/megaease/easegress/blob/main/docs/02.Tutorials/2.4.Resilience.md

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4