Scaling Charms
One of the killer features of computing in the cloud is that it (should) seamlessly allow you to scale up or down your services to meet your needs and whims. Juju not only makes it simple to deploy services, but crucially makes it easy to manage them too. It won't anticipate you getting slashdotted or on the front page of hacker news (yet), but it does mean that when you do you can reliably scale your services to meet the demand.
Adding Units
The general usage to scale a service up is via the add-unit
command:
juju add-unit [options] <service-name>
The command options are:
#juju environment to operate in -e, --environment <environment_name> # number of service units to add -n, --num-units [integer] #the machine or container to deploy the unit in, bypasses constraints --to <machine>
Scaling up services is really as simple as asking for more instances. Consider the following setup for a mediawiki:
juju bootstrap juju deploy mysql juju deploy mediawiki juju add-relation mysql mediawiki:db juju expose mediawiki
When you notice the mediawiki instance is struggling under the load of people making edits, you can simply scale up the service using the command:
juju add-unit mediawiki
This will cause a new instance to be run and configured to work alongside the currently running one. Behind the scenes Juju is adding an instance to the Juju environment called a machine and provisioning the specified service onto that instance/machine.
Suppose your mysql service needs hyper-scale, you can use the -n
or --num-units
options to add-unit
to specify the desired number of units you want to be added to the service. For example, to scale up your service by 100 units simply do:
juju add-unit -n 100 mysql
or you can use --num-unit
which has the same result, but is more readable:
juju add-unit --num-unit 100 mysql
If you would like to add a unit to a specific machine just append the --to
# add unit to machine 23 juju add-unit mysql --to 23 # add unit to lxc container 3 on host machine 24 juju add-unit mysql --to 24/lxc/3 # add unit to a new lxc container on host machine 25 juju add-unit mysql --to lxc:25
The add-unit
command deploys a machine matching the constraints of the initially deployed service. For example, if mysql was deployed with the defaults (i.e. no --constraints
option) you would have mysql on an instance that matches the closest to 1Gig of memory and 1CPU. If you would like to add a unit with more resources to the mysql service you will first need to issue a add-machine
with the desired constraint followed by a add-unit
. For example, the following command adds a 16Gig unit to the mysql service (note in this example juju status
returns machine 3 for the add-machine
command):
juju add-machine --constraints="mem=16G" juju add-unit mysql --to 3
Note: Keep in mind you can always use the -e
or --environment
options to specify which environment/cloud you would like the command ran against. In the following example the -e hpcloud
adds 100 units to the mysql service in HP's cloud:
juju add-unit -n 100 mysql -e hpcloud
More on deploying to specific machines.
Scaling Back
Sometimes you also want to scale back some of your services, and this too is easy with Juju.
The general usage to scale down a service is with the remove-unit
command:
juju remove-unit [options] <unit> [...]
For example, the following scales down the mediawiki service by one unit:
juju remove-unit mediawiki/1
If you have scaled-up the mediawiki service by more than one unit you can remove multiple units in the same command as long as you know the unit name (ie <service>/#
).
juju remove-unit mediawiki/1 mediawiki/2 mediawiki/3 mediawiki/4 mediawiki/5
The remove-unit
command can be run to remove running units safely. The running services should automatically adjust to the change.
Note: After removing a service the machine will still be running. In order to completely remove the machine that once housed the service you need to issue a destroy-machine
. For example, to remove machine 1 that the unit mediawiki/1
was housed on use the command:
juju destroy-machine 1
For more information on removing services, please see the section on destroying services.