We use machine learning technology to do auto-translation. Click "English" on top navigation bar to check Chinese version.
Building a Cloud in the Cloud: Running Apache CloudStack on Amazon EC2, Part 1
This blog is written by Mark Rogers, SDE II – Customer Engineering Amazon Web Services .
How do you put a cloud inside another cloud? Some features that make
CloudStack can work with a number of hypervisors. The instructions in this article will use Kernel-based Virtual Machine (KVM) on Linux. KVM will manage the VMs at a low level, and CloudStack will manage KVM.
Prerequisites
Most of the information in this article should be applicable to a range of CloudStack versions. I targeted CloudStack 4.14 on CentOS 7. I also tested CloudStack versions 4.16 and 4.17, and I recommend them.
The
You’ll need to know the basics of CloudStack. The scope of this article is making CloudStack and Amazon Web Services coexist peacefully. Once CloudStack is running, I’m assuming that you’ll handle things from there. Refer to the
Making things easier
I wrote
Amazon EC2 instance types
I like c5.metal because it’s one of the least expensive metal types, and has a low cost per vCPU. It has 96 vCPUs and 192 GiB of memory. If you run 20 VMs on it, with 4 CPU cores and 8 GiB of memory each, then you’d still have 16 vCPUs and 32 GiB to share between the operating system, CloudStack, and MySQL. Using CloudStack’s overprovisioning feature, you could fit even more VMs if they’re running light loads.
Networking
The biggest challenge is the network. Amazon Web Services knows which IP and MAC addresses should exist, and it knows the machines to which they should belong. It blocks any traffic that doesn’t fit its idea of how the network should behave. Simultaneously, CloudStack assumes that any IP or MAC address it invents should work just fine. When CloudStack assigns addresses to VMs on an Amazon Web Services subnet, their network traffic gets blocked.
You could get around this by enabling network address translation (NAT) on the instance running CloudStack. That’s a great solution if it fits your needs, but it makes it hard for other machines in your Amazon VPC to contact your VMs. I recommend a different approach.
Although Amazon Web Services restricts what you can do with its layer 2 network, it’s perfectly happy to let you run your own layer 3 router. Your EC2 instance can act as a router to a new virtual subnet that’s outside of the jurisdiction of Amazon Web Services. The instance integrates with Amazon Web Services just like a VPN appliance, routing traffic to wherever it needs to go. CloudStack can do whatever it wants in the virtual subnet, and everybody’s happy.
What do I mean by a virtual subnet? This is a subnet that exists only inside the EC2 instance. It consists of logical network interfaces attached to a Linux bridge. The entire subnet exists inside a single EC2 instance. It doesn’t scale well, but it’s simple. In my next post, I’ll cover a more complicated setup with an overlay network that spans multiple instances to allow horizontal scaling.
The simple way
The simple way is to put everything in one EC2 instance, including the database, file storage, and a virtual subnet. Because everything’s stored locally, allocate enough disk space for your needs. 500 GB will be enough to support a few basic VMs. Create or select a security group for your instance that gives users access to the CloudStack UI (TCP port 8080). The security group should also allow access to any services that you’ll offer from your VMs.
When you have your instance, configure Amazon Web Services to treat it as a router.
- Go to Amazon EC2 in the
Amazon Web Services Management Console . - Select your instance, and
stop source/destination checking .
3. Update the
a. Go to the VPC settings, and select Route Tables.
b. Identify the tables for subnets that need CloudStack access.
c. In each of these tables, add a route to the new virtual subnet. The route target should be your EC2 instance.
4. Depending on your network needs, you may also need to add routes to transit gateways, VPN endpoints, etc.
Because everything will be on one server, creating a virtual subnet is simply a matter of creating a Linux bridge. CloudStack must find a network adapter attached to the bridge. Therefore, add a dummy interface with
yum install -y bridge-utils net-tools # The bridge must be named cloudbr0. cat << EOF > /etc/sysconfig/network-scripts/ifcfg-cloudbr0 DEVICE=cloudbr0 TYPE=Bridge ONBOOT=yes BOOTPROTO=none IPV6INIT=no IPV6_AUTOCONF=no DELAY=5 STP=yes USERCTL=no NM_CONTROLLED=no IPADDR=$virtual_host_ip_address NETMASK=$virtual_netmask DNS1=$dns_address EOF # Create a dummy network interface. cat << EOF > /etc/sysconfig/modules/dummy.modules #!/bin/sh /sbin/modprobe dummy numdummies=1 /sbin/ip link set name ethdummy0 dev dummy0 EOF chmod +x /etc/sysconfig/modules/dummy.modules /etc/sysconfig/modules/dummy.modules cat << EOF > /etc/sysconfig/network-scripts/ifcfg-ethdummy0 TYPE=Ethernet BOOTPROTO=none NAME=ethdummy0 DEVICE=ethdummy0 ONBOOT=yes BRIDGE=cloudbr0 NM_CONTROLLED=no EOF # Turn the instance into a router echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf sysctl -p # Must kill dhclient or the network service won't restart properly. # A reboot would also work, if you’d rather do that. pkill dhclient systemctl restart network
CloudStack must know which IP addresses to use for inter-service communication. It will select by resolving the machine’s fully qualified domain name (FQDN) to an address. The following commands will make it to choose the right one. You must provide a value for $virtual_host_ip_address.
hostnamectl set-hostname cloudstack.localdomain echo "$virtual_host_ip_address cloudstack.localdomain" >> /etc/hosts
You can finish the setup by following the
Remember that CloudStack is only directly connected to your virtual network. The EC2 instance is the router that connects the virtual subnet to the Amazon VPC. When you’re configuring CloudStack, use your instance’s virtual subnet address as the default gateway.
To access CloudStack from your workstation, you’ll need a connection to your VPC. This can be through a
If you’re just experimenting with CloudStack, then I suggest saving money by stopping your instance when it isn’t needed. The safe way to do that is:
- Disable your zone in the CloudStack UI.
- Put the primary storage into maintenance mode.
- Wait for the switch to maintenance mode to be complete.
- Stop the EC2 instance.
When you’re ready to turn everything back on, simply reverse those steps. If you have any virtual routers in CloudStack, then you may need to start those, too.
Cleanup
If you used my CloudFormation template, then
Conclusion
Getting CloudStack to run on Amazon Web Services isn’t so bad. The hardest part is simply knowing how. The setup explained here is great for small installations, but it can only scale vertically. In my next post, I’ll show you how to create an installation that scales horizontally. Instead of using a virtual subnet that exists in a single EC2 instance, we’ll build an overlay network that spans multiple instances. It will use more components and features, including some that might be new to you. I hope you find it interesting!
Now that you can create a simple setup, give it a try! I hope you have fun and learn something new along the way. Comment with the results of your experiments.
“Apache”, “Apache CloudStack”, and “CloudStack” are trademarks of the Apache Software Foundation.
The mentioned AWS GenAI Services service names relating to generative AI are only available or previewed in the Global Regions. Amazon Web Services China promotes AWS GenAI Services relating to generative AI solely for China-to-global business purposes and/or advanced technology introduction.