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.

Screenshot

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.

Screenshot

Go to the “Global Config” page.

Screenshot

Click on “Global Lua Extensions”.

Screenshot

Let’s create a new Lua Extension.

Screenshot

Create an extension to prevent GoDaddy from resolving a domain name to an already offline node.

Screenshot

Here we enter the name as “remove_node_from_godaddy_if_node_is_offline”.

Screenshot

Screenshot

Select “By event”.

Screenshot

OpenResty’s Edge supports several kinds of built-in events.

Screenshot

Here, we select the type “Gateway Server Abnormal Offline”. This means that if the server is offline, the Lua Extension will be triggered.

Screenshot

If you are used to using Vim, you can enable Vim mode to edit code.

Screenshot

When the event occurs, it will pass trigger_event to Lua Extension.

Screenshot

Send a request to the manager service, which could remove the node from GoDaddy when it’s offline.

Screenshot

When the response content meets the conditions, use Lua’s cjson module to decode it and assign the result to the body variable.

Screenshot

If body exists and body.status equals 0, then output the successful message.

Screenshot

Click to create it!

Screenshot

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.

Screenshot

Go to Gateway Clusters’s page.

Screenshot

This is the gateway cluster we are going to use.

Screenshot

It’s online now.

Screenshot

Use the systemctl command on the terminal to stop the service.

Screenshot

Refresh the list.

Screenshot

The server is offline now.

Screenshot

Go back to “Global Config”.

Screenshot

Enter “Global Lua Extensions”.

Screenshot

Check the history of execution.

Screenshot

As shown, the extension has been triggered and executed.

Screenshot

The node has been removed from GoDaddy successfully.

Screenshot

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.