Cluster auto scaling configuration - Magento 2 - using DigitalOcean API - continue...

Since auto scaling option is not yet implemented in the digitalocean panel, we need to think through the logic and configuration steps ourselves. Significantly facilitates the whole task - the presence of a full-fledged API. Using API, you can manage both the creation of servers and their full configuration. Submit configuration data at any stage and initialization of any services.

API is very convenient and quick to use for auto scaling resources horizontally.

The official command line interface for the DigitalOcean API. doctl - It uses the DigitalOcean API to provide access to most account and Droplet features.

https://github.com/digitalocean/doctl

For example In order to create our droplet, we can send this request:


PRIVATE_IPV4=$(doctl compute droplet create ${DROPLET_TAG}-${RANDOM}.${MAGE_DOMAIN} \
 --size ${DROPLET_SIZE} \
 --image ${IMAGE_ID} \
 --region ${REGION} \
 --ssh-keys ${SSH_KEY} \
 --tag-name ${DROPLET_TAG} \
 --enable-monitoring \
 --enable-private-networking \
 --user-data-file /opt/${CLIENT}/do_config/user_data_file.sh \
 --no-header \
 --wait | awk {'print $4'})


if [ -z "${PRIVATE_IPV4}" ]; then
    exit 1
fi

All necessary parameters are set in one command

the most important are:


--user-data-file
--wait

Using the "user data file" we inject our configuration script in our new server and it will be executed as soon as it boots up.

"wait" - we tell the API to wait until the IP address is registered, and we can pass this variable further to update our special load balancer or inject/check some more data.

PRIVATE_IPV4 - new droplet IP address

Now that we have a ready droplet we can update the load balancer. Get the droplet data also from metadata:


DROPLET_ID=$(curl http://169.254.169.254/metadata/v1/id)

Update DigitalOcean load balancer records:


doctl compute load-balancer add-droplets ${LB_ID} --droplet-ids ${DROPLET_ID}

To update your custom load balancer, like separate droplet with nginx configuration, simply inject ${PRIVATE_IPV4} into nginx config file included and reload nginx service.

All you need to do is wait until the load on the main server increases, and then we can start a new server. You can monitor the load using internal services in the system, or external metric analyzers.

The main benefit of using an auto-scalable cloud is that all your resources are on separate servers - droplets. And in case of increased load or failure of any node, you can instantly launch a new one.