When your Visual Builder applications have predictable and steady usage, you can set a fixed number of nodes for your VB instance. In the rare case when the load changes, you manually update the number of nodes as needed.
On the other hand, in cases where the load fluctuates, having an automated way to scale the service would be nice.
The following guide shows how to setup an autoscaler for Visual Builder. It can be adapted for other Oracle Cloud services.
Resources Used
The following Oracle Cloud resources are used in this guide.
- Developer Services
- Visual Builder
- Visual Builder
- Visual Builder Studio
- Containers & Artifacts
- Container Registry
- Functions
- Applications
- Application Integration
- Notifications
- Visual Builder
- Observability & Management
- Monitoring
- Alarm Definitions
- Monitoring
Please review the pricing for each service and ensure you have the correct permissions before implementing them.
Overview
- You will update your Visual Builder instance with a Python application using the Oracle Cloud SDK for Python.
- The application will be deployed as an Oracle Cloud Infrastructure Function.
- The function will be executed by an Oracle Cloud Notification.
- The notification will be called by an Oracle Cloud Alarm that will fire whenever your Visual Builder instance OCPU usage exceeds the defined threshold.
When the alarm fires it will pass 3 pieces of information to the Python application.- The number of nodes to scale the instance.
The example below passes a 1 to scale the instance up, but you could modify it to send a -1 to scale the instance down. - The OCID of the Visual Builder instance.
- The type of the alarm. Either "OK_TO_FIRING" meaning the alarm is now triggering, or "FIRING_TO_OK" meaning the alarm is resetting.
- The number of nodes to scale the instance.
There are many other ways you could configure the code and components used in this post. These examples are intended to be very basic for example purposes only.
This guide is meant as an example only and should NOT be used in a production system.
Create a Python application
The method below will process information sent from the monitoring alarm and scale your instance as requested. There is a built in safety catch that will not allow your instance to be scaled above 3 nodes.
Review the code, to make sure you understand everything it's doing before you deploy it.
Executing this code could add charges to your Oracle Cloud account!
Create a Git repository in Visual Builder Studio
- Log into Visual Builder Studio.
- Create a new git repository.
- Enter a name.
'manage-vb' - Enter a description.
'Fn function used to scale a Visual Builder instance' - Click "Create".
Add files to your Git Repository
- Create a new file.
- Name the file.
'requirements.txt' - Enter the file contents.
fdk oci
- Commit your changes.
- Go back to the repository root.
- Create a new file.
- Name the file.
'func.py' - Enter the file contents.
import io import json import oci from fdk import response def handler(ctx, data: io.BytesIO = None): resp = {"errors": {}, "messages": {}} try: # Parse incoming alarm body try: alarm_type = json.loads(data.getvalue())["type"] data = json.loads(data.getvalue())["body"].split() node_change = int(data[0]) vb_ocid = data[1] resp["messages"]["parse_body"] = {"data": data} except Exception as ex: resp["errors"]["parse_body"] = {"data": data, "exception": str(ex)} raise if alarm_type == "OK_TO_FIRING": # OCI objects and information try: signer = oci.auth.signers.get_resource_principals_signer() vb_client = oci.visual_builder.VbInstanceClient(config={}, signer=signer) new_nodes = vb_client.get_vb_instance(vb_ocid).data.node_count + node_change resp["messages"]["oci_objects"] = {"new_nodes": new_nodes} except Exception as ex: resp["errors"]["oci_objects"] = {"new_nodes": new_nodes, "exception": str(ex)} raise if 0 < new_nodes < 4: # Scale the instance try: oci_resp = vb_client.update_vb_instance( vb_instance_id=vb_ocid, update_vb_instance_details=oci.visual_builder.models.UpdateVbInstanceDetails( node_count=new_nodes ), ) resp["messages"]["scale_instance"] = 'Work Request submitted to set the node count for instance {} to {}.'.format(vb_ocid, new_nodes) except Exception as ex: resp["errors"]["scale_instance"] = {"exception": str(ex)} raise else: resp["errors"]["scale_instance"] = 'Instance {} cannot be scaled to {} nodes.'.format(vb_ocid, new_nodes) else: resp = {"Alarm Type": alarm_type} except Exception as ex: print(resp) print(str(ex), flush=True) return response.Response( ctx, response_data=resp, headers={"Content-Type": "application/json"}, ) - Commit your changes.
- Copy the repository URL and save it for later.
Create an Oracle Cloud Repository
OCI Functions are deployed from an Image Repository.
This section will show you how to setup your own repository in Oracle Cloud.
- Under Developer Services, click "Container Registry".
- Click "Create Repository".
- Select a compartment.
- Enter a Repository name.
'vb-fn-apps' - Click "Create".
- When the repository details page opens save the repository Namespace for later.
Create an Oracle Cloud Application
- In the Cloud Console, open the navigation menu under Developer Services, click "Applications".
- Select the compartment you're using with OCI Functions, Click "Create Application".
- Enter a Name.
'VB-Python-SDK' - Choose your VCN.
- Select a Subnet.
- Set the instance Shape.
- Click "Create".
In addition to the information saved in the above steps, you will need to collect some information for the following steps.
- The region-key for your Container Repository.
found here https://docs.oracle.com/en-us/iaas/Content/Registry/Concepts/registryprerequisites.htm#regional-availability
- The OCID of the compartment containing your Application.
- The OCID of the compartment containing your Container Repository.
- Under Identity & Security, click "Compartments".
- Locate the compartment containing your Application, hover the OCID and click "Copy".
- Do the same for your Container Repository if it's in a different compartment.
Your Application will need to be granted permission in order to make changes to your OCI objects. You will create a Dynamic group for all of the applications in the compartment. Then you will add a policy that allows members of that group to make changes to your OCI objects.
Check with your Cloud Administrator before adding the group and policy.
Create a Dynamic Group
- In your cloud console under Identity and Security click "Dynamic Groups".
- Click "Create Dynamic Group".
- Enter a name.
'VB-FN' - Enter a description.
'Function that will be used to scale VB instances' - Add a rule.
ALL {resource.type = 'fnfunc', resource.compartment.id = '<Compartment containing your application>'} - Click "Create".
Create a policy
- In your cloud console under Identity and Security click "Policies".
- Click "Create Policy".
- Enter a Name.
'vb-fn-policy' - Enter a description.
'Allow function in the VB-FN dynamic group to manage Visual Builder instances in the <your compartment name> compartment' - Click the slider next to "Show manual editor".
- Enter a policy statement.
'Allow dynamic-group VB-FN to manage visualbuilder-instance in compartment <your compartment name>' - Click "Create".
Deploy the Function code
You'll use the Oracle Cloud Shell to deploy the Function. Oracle Cloud Shell has all of the necessary tools already installed and configured.
Prerequisites
- Open your Cloud Shell.
- Setup some environment variables.
export vn_application_compartment_ocid=<Application Compartment OCID from above> export container_repository_compartment_ocid=<Docker Repository Compartment OCID from above> export ocir_region_key=<Region key from above> export ocir_namespace=<Namespace from above> export application_name=VB-Python-SDK export function_name=manage-vb
- List the available contexts and select the context for your current region.
fn list context fn use context <your current region context>
- Configure the fn context.
fn update context oracle.compartment-id ${vn_application_compartment_ocid} fn update context registry ${ocir_region_key}/${ocir_namespace}/${function_name} fn update context oracle.image-compartment-id ${container_repository_compartment_ocid} - Clone your git repository using the URL you saved from above.
git clone ssh://<your git repo from above> cd ${function_name} - Initialize your code as an Fn function.
fn init
- Commit and push the changes.
git add . git commit -m"Initial Commit" git push origin main
- Deploy the function to your application.
fn deploy --app ${application_name}
Test the Function
- In the Cloud Console, open the navigation menu under Developer Services, click "Visual Builder".
- Click on the instance you want to use.
Note: this is the instance you'll be running tests on, so I would not recommend using a production instance.
- Copy the OCID and save it.
- Save the "Nodes" value to compare with after the tests.
The Python method accepts a JSON object from an OCI alarm. In order to test the function, you'll use a JSON object with a body key.
{"body":"1 <Visual Builder OCID from above>", "type": "OK_TO_FIRING"}
The function has a built in safety stop at 3 nodes. If your Visual Builder instance is at 3 or more nodes, you can change the '1' to a '-1'. This will scale the instance down by 1 node.
Enter the following in your Cloud Shell.
echo '{"body":"1 <Visual Builder OCID from above>", "type": "OK_TO_FIRING"}' | fn invoke ${application_name} ${function_name}
You should receive a JSON object similar to this.
{
"errors": {},
"messages": {
"parse_body": {
"data": ["1", "ocid1.visualbuilderinstance.oc1.iad.myvbinstanceocid"]
},
"oci_objects": { "new_nodes": 2 },
"scale_instance": "Work Request submitted to set the node count for instance ocid1.visualbuilderinstance.oc1.iad.myvbinstanceocid to 2."
}
}
Switch back to your Visual Builder instance, your Nodes should now be 1 higher (or lower if you scaled down).
Edit your instance and set the nodes back to the original value.
Create a Notification
You will use a Notification to execute your Application Function.
- In your cloud console under Developer Services click "Notifications".
- Click "Create Topic".
- Enter a Name.
'ScaleVB' - Click "Create".
- Click on your new Topic.
- Click "Create Subscription".
- Change the Protocol to "Function".
- Select your Function compartment.
- Select the application you created above.
- Select the function you created above.
- Click "Create".
Test your notification
- In your Notification detail page, click "Publish Message".
- Enter the message body and click "Publish".
{"body":"1 <Visual Builder OCID from above>", "type": "OK_TO_FIRING"}
Switch back to your Visual Builder instance, your Nodes should now be 1 higher (or lower if you scaled down).
Edit your instance and set the nodes back to the original value.
Create an alarm
- Open the details page for your Visual Builder instance.
- In the OCPU Consumption, look at the graph, choose a percentage that is in the middle of the fluctuations and save it.
- Click "Options".
- Select "Create an alarm on this query".
- Enter an Alarm name.
'HighUsage' - Enter the Alarm body
'1 ocid1.visualbuilderinstance.oc1.<OCID of your VB instance>'
- In the "Trigger rule" section set a value that is in the middle of the fluctuation you noted above.
- In the "Define alarm notifications" section, under Destination select the Compartment and Topic you created above.
- Under Message Format, select "Send raw messages".
- Click "Save alarm".
- You should be in the Alarm Definitions page for your new alarm.
- Watch the Alarm data section, after a couple of minutes it will refresh. When the value crosses your Trigger rule percentage the alarm will fire.
- Look at the top of the alarm to see that your alarm is in a firing state.
- Uncheck the "Alarm is enabled" checkbox so it will not continue to scale your instance!
Warning! If you leave the alarm active it will continue to modify your nodes. This can add charges to your cloud account.
Log into Visual Builder.
Watch the Nodes value. It should now be 1 higher (or lower if you scaled down).
Edit your instance and set the nodes back to the original value.
Once you have this working and you want to auto scale the instance down, you could create an alarm calling the same function that triggers when you're below a certain threshold. Just change the '1' to a '-1' in the alarm body and set a different trigger rule.
In this blog we walked you through setting up an automatic scaler for Visual Builder leveraging a collection of Oracle Cloud Infrastructure services and utilities. A similar approach could be adopted for other cloud services as well.
