Use Lua Extensions to Respond to Gateway Server Offline Events in OpenResty Edge
OpenResty Edge offers powerful Lua extension capabilities. This video uses the example of automatically handling abnormal offline situations of gateway servers to provide a detailed introduction on how to create and configure Lua extensions in Edge’s Web console. The goal is to automatically remove corresponding nodes from GoDaddy when a gateway server goes offline.
Create a new Lua extension
OpenResty Edge provides powerful Lua extensions to automatically respond to offline events from the gateway server.
Let’s switch to the web console of OpenResty Edge. This is our sample deployment of the console. Every user would have their own deployment.
Go to the “Global Config” page.
Click on “Global Lua Extensions”.
Let’s create a new Lua Extension.
Create an extension to prevent GoDaddy from resolving a domain name to an already offline node.
Here we enter the name as “remove_node_from_godaddy_if_node_is_offline”.
Select “By event”.
OpenResty’s Edge supports several kinds of built-in events.
Here, we select the type “Gateway Server Abnormal Offline”. This means that if the server is offline, the Lua Extension will be triggered.
If you are used to using Vim, you can enable Vim mode to edit code.
When the event occurs, it will pass trigger_event
to Lua Extension.
Send a request to the manager service, which could remove the node from GoDaddy when it’s offline.
When the response content meets the conditions, use Lua’s cjson
module to decode it and assign the result to the body
variable.
If body
exists and body.status
equals 0, then output the successful message.
Click to create it!
Trigger the Lua extension with an offline gateway server
After creating, the new Lua Extension will work automatically. Let’s test it with an offline gateway server.
Go to Gateway Clusters’s page.
This is the gateway cluster we are going to use.
It’s online now.
Use the systemctl
command on the terminal to stop the service.
Refresh the list.
The server is offline now.
Go back to “Global Config”.
Enter “Global Lua Extensions”.
Check the history of execution.
As shown, the extension has been triggered and executed.
The node has been removed from GoDaddy successfully.
What is OpenResty Edge
OpenResty Edge is our all-in-one gateway software for microservices and distributed traffic architectures. It combines traffic management, private CDN construction, API gateway, security, and more to help you easily build, manage, and protect modern applications. OpenResty Edge delivers industry-leading performance and scalability to meet the demanding needs of high concurrency, high load scenarios. It supports scheduling containerized application traffic such as K8s and manages massive domains, making it easy to meet the needs of large websites and complex applications.
If you like this tutorial, please subscribe to this blog site and/or our YouTube channel. Thank you!
About The Author
Yichun Zhang (Github handle: agentzh), is the original creator of the OpenResty® open-source project and the CEO of OpenResty Inc..
Yichun is one of the earliest advocates and leaders of “open-source technology”. He worked at many internationally renowned tech companies, such as Cloudflare, Yahoo!. He is a pioneer of “edge computing”, “dynamic tracing” and “machine coding”, with over 22 years of programming and 16 years of open source experience. Yichun is well-known in the open-source space as the project leader of OpenResty®, adopted by more than 40 million global website domains.
OpenResty Inc., the enterprise software start-up founded by Yichun in 2017, has customers from some of the biggest companies in the world. Its flagship product, OpenResty XRay, is a non-invasive profiling and troubleshooting tool that significantly enhances and utilizes dynamic tracing technology. And its OpenResty Edge product is a powerful distributed traffic management and private CDN software product.
As an avid open-source contributor, Yichun has contributed more than a million lines of code to numerous open-source projects, including Linux kernel, Nginx, LuaJIT, GDB, SystemTap, LLVM, Perl, etc. He has also authored more than 60 open-source software libraries.