Enhancing K8s Gateway API with Easegress Without Changing a Single Line of Code

embedded/2024/11/26 20:11:17/

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?gateway-api-with-easegress-without-changing-a-single-line-of-code/#why-enhance-the-k8s-gateway-api" rel="nofollow">

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 Easegressgateway-api-with-easegress-without-changing-a-single-line-of-code/#kubernetes-gateway-extensionref-the-glue-between-kubernetes-and-easegress" rel="nofollow">

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:"

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:name: httproute-extension
spec:rules:- matches:- path:value: /testfilters: - type: ExtensionRef# Referencing the FilterSpec resource through ExtensionRef.extensionRef:group: "easegress.megaease.com"kind: "FilterSpec"name: "rate-limiter"backendRefs:- name: service-aport: 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 Flexibilitygateway-api-with-easegress-without-changing-a-single-line-of-code/#custom-resource-definitions-balancing-security-and-flexibility" rel="nofollow">

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:

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:name: filterspecs.easegress.megaease.com
spec:group: easegress.megaease.comversions:- name: v1served: truestorage: trueschema:openAPIV3Schema:type: objectproperties:spec:type: objectproperties:name:type: stringkind:type: stringspec:type: stringscope: Namespacednames:plural: filterspecssingular: filterspeckind: 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 Exercisegateway-api-with-easegress-without-changing-a-single-line-of-code/#practical-exercise" rel="nofollow">

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:

apiVersion: easegress.megaease.com/v1
kind: FilterSpec
metadata:name: rate-limiter
spec:name: rate-limiterkind: RateLimiterspec: |policies:- name: policylimitRefreshPeriod: 5000mslimitForPeriod: 1defaultPolicyRef: policyurls:- url:prefix: /policyRef: policy    ---apiVersion: easegress.megaease.com/v1
kind: FilterSpec
metadata:name: response-adaptor
spec:name: response-adaptorkind: ResponseAdaptorspec: |header:add: 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:

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:name: example-route-2
spec:parentRefs:- kind: Gatewayname: example-gatewaysectionName: example-listenerrules:- matches:- path:value: /testfilters: - type: ExtensionRefextensionRef:# use rate-limitergroup: "easegress.megaease.com"kind: "FilterSpec"name: "rate-limiter"- type: ExtensionRefextensionRef:# use response-adaptorgroup: "easegress.megaease.com"kind: "FilterSpec"name: "response-adaptor"backendRefs:- name: hello-serviceport: 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].

docker@minikube:~$ curl http://127.0.0.1:30081/test -v 
...
< Date: Thu, 23 Nov 2023 02:57:59 GMT
< X-Eg-Response-Adaptor: true  # ResponseAdaptor works
< Connection: close
< 
Hello, world!
Version: 2.0.0
Hostname: hello-deployment-688d8666c-xl9sb
* Closing connection 0docker@minikube:~$ curl http://127.0.0.1:30081/test -v 
...
< HTTP/1.1 429 Too Many Requests
< X-Eg-Rate-Limiter: too-many-requests  # RateLimiter works
< Date: Thu, 23 Nov 2023 02:58:00 GMT
...

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 Strategiesgateway-api-with-easegress-without-changing-a-single-line-of-code/#circuit-breaker-and-retry-strategies" rel="nofollow">

Furthermore, we have also provided definitions for circuit breakers and retry strategies [5], further enhancing the resilience and reliability of the network.

apiVersion: easegress.megaease.com/v1
kind: FilterSpec
metadata:name: circuit-breaker
spec:name: circuit-breakerkind: CircuitBreakerspec: |slidingWindowType: TIME_BASEDfailureRateThreshold: 60slidingWindowSize: 200    --- apiVersion: easegress.megaease.com/v1
kind: FilterSpec
metadata:name: retry
spec:name: retrykind: Retryspec: |maxAttempts: 3waitDuration: 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


http://www.ppmy.cn/embedded/140710.html

相关文章

速盾:CDN缓存的工作原理是什么?

CDN&#xff08;内容分发网络&#xff09;是一种将内容分发到全球不同地理位置的网络架构&#xff0c;以提供更快速、可靠的内容传输。其核心原理是利用缓存技术&#xff0c;将数据内容分布到离用户最近的边缘节点上。当用户请求内容时&#xff0c;CDN将根据用户的IP地址&#…

Windows系统电脑安装TightVNC服务端结合内网穿透实现异地远程桌面

文章目录 前言1. 安装TightVNC服务端2. 局域网VNC远程测试3. Win安装Cpolar工具4. 配置VNC远程地址5. VNC远程桌面连接6. 固定VNC远程地址7. 固定VNC地址测试 前言 在追求高效、便捷的数字化办公与生活的今天&#xff0c;远程桌面服务成为了连接不同地点、不同设备之间的重要桥…

IDEA 2024安装指南(含安装包以及使用说明 cannot collect jvm options 问题一)

下载 完整下载链接软件的下载地址是&#xff1a;https://www.jetbrains.com/zh-cn/products/ 备用idea下载地址&#xff1a;https://www.jetbrains.com/idea/download/other.html 也可直接点击网盘内容&#xff1a; 安装包 关注文章&#xff0c;都在更新。

easyui combobox 只能选择第一个问题解决

easyui combobox 只能选择第一个问题解决 问题现象 在拆分开票的时候&#xff0c;弹出框上面有一个下拉框用于选择需要新增的明细行&#xff0c;但是每次只能选择到第一个 选择第二条数据的时候默认选择到第一个了 代码如下 /*新增发票编辑窗口*/function addTicketDialog…

重新定义社媒引流:AI社媒引流王如何为品牌赋能?

在社交媒体高度竞争的时代&#xff0c;引流已经不再是单纯追求流量的数字游戏&#xff0c;而是要找到“对的用户”&#xff0c;并与他们建立真实的连接。AI社媒引流王通过技术创新和智能策略&#xff0c;重新定义了社媒引流的方式&#xff0c;帮助品牌在精准触达和高效互动中脱…

node.js、nginx、iis、tomcat针对部署方面的简述

了解 Node.js、Nginx、IIS 和 Tomcat 这些技术的部署方式及其应用场景&#xff0c;可以帮助你做出更合理的架构选择。下面是这些技术的简要讲解及它们在部署中的应用&#xff1a; 1. Node.js 部署 Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行时环境&#xff0c;广泛…

React 表单Form 中的 useForm

1、介绍 useForm 是 React Hook Form 中的核心 Hook&#xff0c;用于管理表单的状态和行为。它提供了处理表单验证、数据收集、状态管理等功能的简便方法。useForm 本质上是用于创建和配置表单&#xff0c;并允许你在组件中与表单字段交互。 2、基本用法 useForm 是一个函数…

Centos 8, add repo

Centos repo前言 Centos 8更换在线阿里云创建一键更换repo 自动化脚本 华为Centos 源 , 阿里云Centos 源 华为epel 源 , 阿里云epel 源vim /centos8_repo.sh #!/bin/bash # -*- coding: utf-8 -*- # Author: make.han