How to Set up Mule Cluster on AWS EC2 with Load Balancer

How to Set up Mule Cluster on AWS EC2 with Load Balancer

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:

  1. Set up Mule runtime on each EC2 instance hosted on AWS
  2. Configure Mule Cluster on Anypoint Runtime Manager
  3. Set up a Load balancer

Prerequisites

  • Create two EC2 instances on AWS.
  • Upload Mule 4 standalone runtime server on EC2 instances
  • Anypoint Platform access

Useful links:

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

  1. Navigate to Runtime Manager
  2. Click on Servers
  3. Click on Add Servers
mule-server

Step 2: In the next screen,

  1. Name your server
  2. Copy the command
add-server

Step 3: Now switch back to your SSH session and,

  1. Navigate to the bin folder in the mule home directory
  2. Paste the command that you copied while adding server in ARM
amc_command

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

server-added

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

mule-start

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

servers-registered

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.

mule-startup-failed

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.

zero-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
create-swap

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.

create-cluster

Step 2: In the next screen,

  1. Name your cluster
  2. Select the Cluster type (Unicast or Multicast)
  3. Select the Servers
    • Select the Servers and Select the Ports for the servers
  4. Click on Create
cluster-config

Your cluster should be up and running as shonw below,

cluster-status

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

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

load balancing

Read this NGINX blog for more details about load balancing.

Step 1: Create new EC2 instance.

load-balancer-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.

nginx-status
  • 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;
          }
 }
load-balancer-config

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.

http-listener

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 
nginx-running-status

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,

load-balancer-test

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 FacebookTwitter and LinkedIn. Visit our MuleSoft Hub for more tutorials and updates.

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:

  1. Set up Mule runtime on each EC2 instance hosted on AWS
  2. Configure Mule Cluster on Anypoint Runtime Manager
  3. Set up a Load balancer

Prerequisites

  • Create two EC2 instances on AWS.
  • Upload Mule 4 standalone runtime server on EC2 instances
  • Anypoint Platform access

Useful links:

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

  1. Navigate to Runtime Manager
  2. Click on Servers
  3. Click on Add Servers
mule-server

Step 2: In the next screen,

  1. Name your server
  2. Copy the command
add-server

Step 3: Now switch back to your SSH session and,

  1. Navigate to the bin folder in the mule home directory
  2. Paste the command that you copied while adding server in ARM
amc_command

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

server-added

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

mule-start

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

servers-registered

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.

mule-startup-failed

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.

zero-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
create-swap

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.

create-cluster

Step 2: In the next screen,

  1. Name your cluster
  2. Select the Cluster type (Unicast or Multicast)
  3. Select the Servers
    • Select the Servers and Select the Ports for the servers
  4. Click on Create
cluster-config

Your cluster should be up and running as shonw below,

cluster-status

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

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

load balancing

Read this NGINX blog for more details about load balancing.

Step 1: Create new EC2 instance.

load-balancer-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.

nginx-status
  • 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;
          }
 }
load-balancer-config

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.

http-listener

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 
nginx-running-status

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,

load-balancer-test

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 FacebookTwitter and LinkedIn. Visit our MuleSoft Hub for more tutorials and updates.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related Articles

How to Create Multiple Folders at Once in Windows 10

Creating folders and subfolders manually is time consuming tasks...

How to Integrate Solace with MuleSoft using JMS API

In this tutorial, we will demonstrate step by step...

How to Open Port on AWS EC2 Instance

In this tutorial, we will demonstrate step by step...

LEAVE A REPLY

Please enter your comment!
Please enter your name here