Using SmartOS for Node.js Development
I was fortunate enough to attend Nodejitsu’s latest workshop in London today, which was totally awesome - as an infrastructure guy, I’m a total Node.js lightweight and the workshop really opened my eyes to some of the power beneath the surface of node. The workshop was delivered by none other than Paolo and Nuno - people who need no further introduction, suffice to say they are seriously freaky when it comes to pretty much anything involving 1’s and 0’s.
During the course of the workshop Paolo took some time to espouse the virtues of SmartOS… he was in my case preaching to the converted, but aside from the Clock guys who were in the room I wasn’t sure how many others had tried on SmartOS. So this post is for you guys :)
SmartOS is different to pretty much any other mainstream OS out there at the moment, in that it’s designed from the ground up to be totally stateless - it runs completely from memory. VMware’s ESXi is the only thing that comes close in this regard. For the unfamiliar, this can be a bit of a stumbling block - persistence is useful for a local development environment, but actually it’s really easy to do with SmartOS once you know how.
Before we kick off, it’s necessary to introduce a potentially new concept: “OS level” virtualisation. In Solaris and it’s children, of which SmartOS is one, the feature is known as ‘zones’. Think of them as basically resource boundaries around processes - it’s not a fully encapsulated virtualisation construct like KVM. So when you run a zone that is itself in a VM, you’re not running any kind of nested virtualisation at all - if you’re running in a zone on bare metal, you get bare metal performance. For the sake of this article, there are 2 types of zones: the “global” zone and “local” zones. And therein lies the core of what you will learn in this post - global zone bad / transient, local zone good / persistent.
You’ll need some kind of machine virtualisation app to follow along. I use the free VMware Player, but the steps should be the same for any of the alternatives.
1. Head over to https://download.joyent.com/pub/iso/ and grab the latest.iso.
2. Fire up VMware Player, and create a new VM. The important option is “Solaris 10 64bit” for the OS type. If I was planning on running this VM on my laptop, I would choose a single vCPU, 2GB RAM, and a 4GB disk. That’s right, only 4GB disk - you could easily use less. Remember you don’t actually “install” SmartOS anywhere. The storage is purely for local zones.
3. Attach the SmartOS .iso file you downloaded in step 1 to the VM, and power it up.
4. You’ll be presented with a brief first-time configuration script - I usually just choose dhcp for the networking option. The other questions are self explanatory.
5. The setup script will format the virtual disk with the last word in file systems, do a few other things you don’t need to worry about and then reboot the system.
6. When the system reboots, you should be presented with something like:
Login as root, using the password you entered during the setup script. You have just logged into the “global zone”.
7. Everything from here on in will be _much_ easier via SSH, so grab the IP address of the global zone by entering the following command: ipadm show-addr
8. Fire up your favourite terminal / SSH client and login to the global zone via SSH.
OK, so where are we at? We are logged in to the global zone - a completely (for all intents and purposes) non-persistent OS environment running from RAM. You also have a ZFS formatted local disk, with a single ZFS pool called ‘zones’ which is mounted at, funnily enough, /zones. We now need to create a local zone, which will be persistent on disk just like any other OS you are probably used to.
Local zones can be created from what amounts to a snapshot of the global zone, or from a bundled set of files called ‘templates’. We’ll use the template option.
9. The imgadm command is used to manage templates. First of all do an imgadm update then (assuming that is successful) imgadm avail. You will now see a list of available templates to download and install.
10. We’re going to go with the minimal ‘base64’ template - something that will give you a nice small system, and includes node.js. At the time of writing, the most recent base64 template has a UUID of e8c41d40-f161-11e1-b839-a3631c115653 and includes node.js 0.8.7, so download and install that by issuing imgadm import e8c41d40-f161-11e1-b839-a3631c115653 - the download size is ~65MB, which expands out to ~300MB on disk so it shouldn’t take long.
11. Once imgadm has finished importing the template, you can finally deploy a local zone from it. To do this, you use the vmadm command, giving it a JSON file with a few parameters. The absolute most basic JSON file you can use is as follows (obviously modify the memory amount and default gateway value to suit your environment):
Copy that into a new file and save it somewhere under /zones so it survives reboots. I usually just call it ‘zone.json’ and stick directly under /zones.
12. Finally, create a new local zone with vmadm create -f /zones/zone.json
13. Assuming that is successful, you are done! Check that your local zone is running with vmadm list
You can now log in to your new local zone in a few ways: either zlogin <UUID> (that’s the UUID from vmadm list - not the template UUID) from the global zone, or simply by SSH. Use zlogin to get into the local zone, then use ipadm show-addr to get the local zone IP address. After that you can just SSH straight into the local zone using it’s IP. Note that if you want to do this using a password rather than a key, you’ll need to make the necessary changes to /etc/ssh/sshd_config and then restart the ssh server with svcadm restart ssh.
You can now install whatever stuff you want. SmartOS uses NetBSD’s pkgsrc system, rather than IPS which is used by Solaris proper and other Illumos based distro’s. To get started, run pkgin update to get the latest packages database, then see what is available to install with pkgin avail. To find something, for example git, just pkgin search git.
Now, the beauty of this whole setup is that you never actually need to go through it again! When new releases of SmartOS come out, simply attach the new ISO to your VM and boot up - it will pick up everything persisted in your ZFS pool, including all local zones, plus the global zone user database and network config. See how awesome SmartOS is? Patching OSes is for losers - just boot into a fresh build every time :).
That pretty much wraps it up for now. If you’re serious about node.js, you really should develop on SmartOS. Chances are you won’t notice much of a difference from a Linux environment (if you basically spend all your time coding in vim and debugging with tcpdump - the SmartOS equivalent being snoop - and client side browser based tools) and you have the power of DTrace at your fingertips. What’s DTrace you say? That’s a topic for another post ;).