In this tutorial, you will learn step by step guide to set up Mule cluster on AWS EC2 instance with load balancer configuration.
This tutorial consists three steps:
- Set up Mule runtime on each EC2 instance hosted on AWS
- Configure Mule Cluster on Anypoint Runtime Manager
- Set up a Load balancer
Prerequisites
Useful links:
- How to create EC2 instances on AWS with Elastic IP address
- How to upload files to EC2 instances using FileZilla
- Mule 4 standalone runtime download portal
Set up Mule Cluster on AWS
Install Mule Runtime on EC2 Instances
Open SSH session into the EC2 instance using SSH client such as Putty and run the below commands.
- Update the packages
sudo apt update
- Install JDK
sudo apt install default-jdk
- Install Unzip
sudo apt install unzip
- Unzip the mule runtime zip file
sudo unzip mule-enterprise-standalone-4.3.0-20210119
Register Mule Runtime with ARM
To manage the Mule runtime through the Anypoint Platform UI, you’ll need to register each runtime with the platform using Runtime Manager.
Step 1: Login to Anypoint Platform and
- Navigate to Runtime Manager
- Click on Servers
- Click on Add Servers

Step 2: In the next screen,
- Name your server
- Copy the command

Step 3: Now switch back to your SSH session and,
- Navigate to the bin folder in the mule home directory
- Paste the command that you copied while adding server in ARM

Once the Mule agent configured successfully, the server will be created in ARM as shown below,

Next we will need to start Mule Runtime for the Anypoint Platform UI to detect the VM instance.
Step 4: Inside bin folder run ./mule start

Now switch back to Anypoint Platform and you should see that your server is registered within Anypoint Platform.

Note: You should perform the above steps in both the EC2 instances.
Possible Errors:
You may ended up with an error saying WARNING: Mule Enterprise Edition may have failed to start.

This is due to OutOfMemory issue as there will be zero swap . To check the swap, run free -h
.
If your logs looks like below then you will need add swap.

To create swap, execute the below commands in the root directory,
sudo fallocate -l 4G /swapfile //create swap file
sudo chmod 600 /swapfile //enable swap file
sudo mkswap /swapfile //setup swapspace
sudo swapon /swapfile //enable swap to use
sudo swapon -s //verify whether system reports swap or not
free -m //verify the space allocated

Suggestion: Anything over 4G of swap is probably unnecessary if you are just using it as a RAM fallback.
To know more about swap file, visit this blog.
Create Cluster in Anypoint Platform
Step 1: Switch back to Anypoint Platform and navigate to Runtime manager -> Servers and click on Create Cluster.

Step 2: In the next screen,
- Name your cluster
- Select the Cluster type (Unicast or Multicast)
- Select the Servers
- Select the Servers and Select the Ports for the servers
- Click on Create

Your cluster should be up and running as shonw below,

Now deploy a sample application and verify that everything working fine.
Here sample application that logs the server host.
<logger message="#[server.host]" level="INFO"/>
Implement NGINX Load Balancer on AWS EC2
Now let’s see how to implement NGINX Load balancer on EC2 instance. You can use other open source Load balancers such as Apache, Amazon ALB, etc.,
What is Load Balancer
A load balancer is a hardware/software device that acts as a reverse proxy and distributes network or application traffic across a number of servers. Load balancers are used to increase capacity (concurrent users) and reliability of applications.
How Load Balancer works

Read this NGINX blog for more details about load balancing.
Step 1: Create new EC2 instance.

Step 2: Connect to the newly created EC2 instance thru any SSH Client and execute the following commands,
- Update the packages and install NGINX
sudo apt update //update packages
sudo apt install nginx //install nginx
Copy and Paste the public IP of your EC2 instance on web browser and verify that nginx is installed.

- Next, create a server block file called
/etc/nginx/conf.d/loadbalancer.conf
.
sudo nano /etc/nginx/conf.d/loadbalancer.conf
Copy and Paste the below code snippet in the loadbalancer.conf file and save the file (CTRL+X).
upstream backend {
server 34.232.7.201:8081;
server 52.55.137.127:8081;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}

Where, 34.232.7.201
and 52.55.137.127
are the public IP address of your AWS EC2 instance.
The port is common port, basically its your mule domain HTTP Listener port.

The proxy_pass
directive (which should be specified inside a location, /
in this case) is used to pass a request to the HTTP proxied servers referenced using the word backend, in the upstream directive .
Note: We are using round-robin mechanism in this example. You can use different mechanism as per your business needs.
For example, If you want to serve one server (52.55.137.127) 4 requests and another server (34.232.7.201) 2 requests then you can use following configuration.
upstream backend {
server 172.31.17.147:8081 weight=4;
server 172.31.19.134:8081;
}
Visit NGINX documentation for more information about different mechanisms.
- Run the following command to ensure the load balancer configuration is correct and valid.
sudo nginx -t
[email protected]:~$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
- If the configuration is OK, restart and enable the Nginx service to apply the changes.
sudo systemctl restart nginx
sudo systemctl enable nginx
- Optionally you can check the nginx status.
sudo systemctl status nginx

Testing NGINX Load Balancer
Now open Postman and send a request to http://<<EC2-instance-public-ip-address>>:<<PORT>>/path
.
In our case, http://54.242.254.245:80/host
. Here, host is my mule application path. But why 80, it should be 8081 right? Nope, check the loadbalancer.conf
file above.
Your application should log different IP address, If you configured everything correctly as shown below,

That’s simple!
Conclusion
We hope this tutorial helped you understand what is Load balancing and how to setup Mule cluster and load balancer using NGINX.
If you face any issues, please do let us know in the comment section. We are happy to help you.
Please do share it with your friends and don’t forget to follow us on Facebook, Twitter and LinkedIn. Visit our MuleSoft Hub for more tutorials and updates.