Cloud Foundry: Deploying to the Cloud
Total Page:16
File Type:pdf, Size:1020Kb
APPENDIXA Cloud Foundry: Deploying to the Cloud In this appendix, we will discuss what it takes to get our application to run on a different environment than what we have been running it on so far. Instead of running on any boring old Tomcat instance, we'll run it in the cloud. But before we get down to it, we'll talk just a little bit about what the cloud means. Cloud Computing You probably haven't been able to avoid the onslaught of cloud computing in the specialized media. But you may still not have an idea of what cloud computing entails. We'll try to give a short overview of what cloud computing is about. It's hard to give a single definition of what cloud computing is, as it tends to mean different things to different people. One broader definition goes like this: Cloud Computing is a distributed computing model consisting of three ti ers infrastructure, platform, and serv ices-that enables ubiquitous, convenient, on demand network access to ash ared pool of configurable computing resources (e.g., networks, servers, storage, applications, and services). Consider Amazon S3,1 for example: Amazon allows you to store data on its servers using web-based APIs. You pay Amazon for the bandwidth you use in uploading or downloading that data. At no point in time do you ever need to have your own storage infrastructure. There are several kinds of cloud service models in use: • Infrastructure as a service (laaS): Delivers computing infrastructure as a service. Examples of such infrastructure include virtual computers, storage, network infrastructure, and so forth. This is more the realm of sysadmins than developers. Amazon and Rackspace are two of the best-known IaaS providers. lhttp://aws.amazon.com/es/s3/ 535 APPENDIX A • CLOUD FOUNDRY: DEPLOYING TO THE CLOUD • Platform as a service (PaaS): Offers services beyond the infrastructure. Here, the emphasis is on the development environment, application services, and application-deployment mechanisms. The control over the actual infrastructure itself is mostly handed by the service provider. Good examples of a PaaS approach are Google App Engine and Cloud Foundry. • Software as a service (SaaS): Provides the delivery of a full solution as a service, eliminating the need to install and run the application on the customer's own computers and simplifying maintenance and support. Contrast this with the traditional delivery model for software, where end users install software on every desktop, manage the upgrades themselves, and so on. In this model, the user just visits a website and begins using the application online. Google Docs is a good example of a SaaS. Cloud Foundry In this appendix, we'll use Cloud Foundry as our PaaS of choice to deploy our bookstore application to. But what is Cloud Foundry and why would we want to use it? Cloud Foundry describes itself like this: [Cloud Foundry is] an open platform as a service, providing a cho ice of clouds, developer frameworks and application services. Initiated by VMware, with broad industry support, Cloud Foundry makes itfaster and easier to build, test, deploy and scale applications. It is an open source project and is available through a variety 0 f private cloud distributions and public cloud i nstances, including CloudFoundry.com. 2 Now what does this mean? It means that Cloud Foundry is not a proprietary, vendor-specific PaaS, but a truly open platform that is being developed by several individuals and organizations. For example, ActiveState offers Stackato,3 an expansion of Cloud Foundry that offers Python, PHP, and Perl support. Ubuntu is also working on supporting Cloud Foundry under their Ubuntu Cloud4banner. It also means that we have to define some things more explicitly: • Cloud Foundry is the name of the open source project. You can fmd the code for this on github;5 its website is at www. cloudfoundry. org. • Cloudfoundry.com is a VMWare-hosted instance of Cloud Foundry that lets you deploy your application on VMWare-owned infrastructure. In the course of this appendix, we don't differentiate between Cloud Foundry the platform and CloudFoundry.com the hosted service. 2http://www.cloudfoundry.com 3http://www.activestate.com/stackato 4http://cloud.ubuntu.com 5https:llgithub.com/cloudfoundry/ 536 APPENDIX A • CLOUD FOUNDRY: DEPLOYING TO THE CLOUD Now that we've seen what Cloud Foundry is, why should we as Java/Spring developers care? Why should we use it over any other PaaS? One reason: The fact that Cloud Foundry is an open, multi-vendor platform is very nice because it helps us avoid the dangers of vendor-locking and the related costs. Another reason: It also has multi-language support, including Ruby, Java, Scala, and NodeJs; and it offers many services, from relational stores such as MySQL and PostgreSQL, to queues like RabbitMQ, to N oSQL storage like MongoDB and Redis. Another big advantage is that it integrates very nicely with Spring. These things combined make Cloud Foundry a very interesting platform. CloudFoundry.com, the VMWare-hosted version of Cloud Foundry, is still in beta at the time of writing. Nevertheless, you can sign up for it by going to https:llmy.cloudfoundry.com/signup and entering an e-mail address. After reading and agreeing with the Terms of Service, you can click the Request Invite button. You'll get an e-mail confirming your signup request. Later (it can take a few hours or even a day), you'll receive an e-mail with the credentials you need to actually deploy on CloudFoundry.com. That's all you need to do to register. Deploying Our Application In this section, we are going to describe how we can deploy the sample application on Cloud Foundry. We will describe each step in detail and also explain the modifications we must do to make everything work as expected. If you want to perform the actions described here yourself, we will assume that you have read the initial chapter on how to set up your environment. We are also assuming you have either STS or an Eclipse instance with the STS plug-in installed. Installing the Cloud Foundry Plug-in Although Cloud Foundry offers several tools to get your application deployed, the fastest way to get started when you first dive in is to use the Eclipse plug-in. The alternative is using the VMC command line application, which is implemented as a Ruby gem. An advanced user can't escape using the VMC command-line; but for the scope of this appendix, we'll use the Eclipse plug-in. • Note RubyGems6 is the package manager for Ruby. It is comparable to apt-get for Ubuntu or yum for Red Hat. A gem is a packaged Ruby application or library. You can find more info on the VMC install at http://start.cloudfoundry . com/tools/vmc/installing-vmc. html. If you have Ubuntu, you can also directly install the command-line client using apt-get install cloudfoundry-client. We'll start by installing the Cloud Foundry Eclipse plug-in. Go to the STS plug-in Dashboard (it can be found under Menu ~ Help ~ Dashboard), click the Extensions tab found on the bottom of the window, and search for cloud foundry in the search bar. Check the found extension, and then click the Install button on the bottom right, as seen in Figure A-I. 6http://rubygems.org 537 APPENDIX A • CLOUD FOUNDRY: DEPLOYING TO THE CLOUD , Dashboard 1::l =0 SprlngSource Tool Suite 4 spring Find: cloud foundry Show Installed Server and Clouds Support for Server and Clouds 6 Cloud Foundry Integration by VMware. Inc .• Free. Commercial <D The integration supports managing application deployments. service bindings and instance scaling on VMware Cloud Foundry find updates configure Extensions Install Dashboard Extensions Figure A -1. Finding the Cloud Foundry plug-in You'll get the normal plug-in installation windows that ask you to accept or reject the license agreement. Click the Next and OK buttons until you get a dialog asking you to restart Eclipse, and then choose to restart it by clicking Restart now. Now that we've installed the plug-in, we can proceed to the next step: deploying our application on Cloud Foundry. Making Some Adjustments The teams behind Spring and Cloud Foundry have worked at making it very easy to deploy Spring apps on Cloud Foundry. It is an explicit goal to make Spring applications deployable without changing one line of configuration or code in most cases. Unfortunately, this is not entirely true for our application because it will not get deployed without us making some changes. The main reason is that Cloud Foundry, at the time of writing, only provides a Tomcat 6 instance (support for Tomcat 7 is on the road map). The other driver for change is the way SSL is set up in Cloud Foundry, which has some impact on the way we configure Spring Security. 538 APPENDIX A • CLOUD FOUNDRY: DEPLOYING TO THE CLOUD Switching to Tomcat 6 Tomcat 6 is a Java EE5 servlet container, so it does not implement the Servlet 3 API. The local server instance we have been using so far is Tomcat 7, which is Java EE 6 compliant. Our sample applications have an explicit dependency on Java EE 6---and the Servlet 3 API in particular. Until now, we've been bootstrapping the Spring application context in BookstoreWebApplicationlni tializer, which is an implementation of org. springframework. web . WebApplicationlnitializer. The WebApplicationlni tializer mechanism is what allowed us to get by without using a web. xml fIle for so long; however, it depends on a Servlet 3 implementation to work.