Cloud Foundry for Developers
Cloud Foundry for Developers
Cloud Foundry, a popular open source PaaS, gives developers an environment to run, scale, and maintain applications easily. Learn to create an app with Cloud Foundry.
Join the DZone community and get the full member experience.Join For Free
Learn how to migrate and modernize stateless applications and run them in a Kubernetes cluster.
Cloud Foundry is a popular open source Platform as a Service (PaaS). Cloud Foundry can be used on your own deployment infrastructure or on any IaaS like Amazon web services, Azure, VMware, or vSphere. It can be deployed using the BOSH deployment system. Cloud Foundry gives an environment to run, scale, and maintain applications easily. Cloud Foundry supports most of the languages and environments like Java, NodeJS, Ruby, Python etc. Pivotal has a commercial instance of Cloud Foundry called Pivotal Web Services on AWS cloud.
Pivotal also provides a lightweight distribution of PCF called PCF Dev which is for development and can run on a single machine.
Concepts of Cloud Foundry
Users are the users of Cloud Foundry who can manage org/space/app.
Roles are the privileges assigned to different users like Org manager/Space manager.
Orgs are at the Top level that can be used by different users to group apps. Each Orgs has one or multiple space.
Space is where Apps can be deployed and share the configurations. Multiple apps can be deployed in single space. Single or multiple spaces constitute an Org.
Apps are each and individual applications that run inside Cloud Foundry.
Every external service that an application uses like database, messaging queues, etc. are services in Cloud Foundry. These services need to be defined at the Cloud Foundry level, which can be used by any org/space through Service Broker API. All the services need to be registered in Marketplace. Marketplace is the place to see all the available services exposed on the Cloud Foundry.
We can create an instance of any service available that suits our needs. For example, Pivotal provides a MySQL service on Pivotal Web Services called p-mysql with different types of plans like 512MB and 1024MB plans. If we need a MySQL database in our application, we can create the service instance of p-mysql of an appropriate plan depending on our needs and bind this service instance to the application. Then Cloud Foundry exposes the connection parameter of this MySQL as an environment variable VCAP_SERVICES, which can be used by the app to connect to the MySQL database.
Bindings are the mappings between services available in the space to the apps.
Every application is identified by a single/multiple addresses which are called routes. These routes can be mapped to applications and are used to access the application.
Build packs resolve/download/configure the dependencies of an application depending on the user inputs. An example of build pack is java-buildpack. This would bring the dependencies like Java, Tomcat, Grails, Groovy, Play, etc. depending on the nature of the app. Cloud Foundry comes with build packs for most of the platforms. There are community supported build packs. Cloud Foundry allows creating custom build packs.
When User creates an application (cf push), Cloud Foundry creates a droplet out of it. A droplet is like an image or blob storage of the actual application binaries and dependencies. This is stored inside Cloud Foundry for later usage. Whenever the user tries to run/scale the application, this image is used to create a VM and run it. So whenever there are changes to the application/configuration/env. variables, the app needs to be re-pushed so the droplet is created again. Restarting/restaging will only restart/redeploy the same droplet.
These are the shared variables between Cloud Foundry and the application. The application can use these to discover various services.
VCAP_SERVICES- Cloud Foundry adds the services related information to this variable.
VCAP_APPLICATIONS - Cloud Foundry adds the application-related properties into this variable.
Logging Into Cloud Foundry
LogAggregator is Cloud Foundry component that consolidates the logs from all App instances. Logs can be streamed to a terminal, or to a file or to a third party service.
CF CLI tool
CF CLI is the command line client for using Cloud Foundry; it is used to pass commands to deploy/scale/manage/monitor an application, create/stop services etc. CF CLI can be installed on any Linux/Windows/Mac machines. List of CF CLI commands. Spring Tool Suite (STS) is an eclipse based IDE used for Spring Application development, and also has a Cloud Foundry Plugin that helps to deploy/manage applications, and create and manage org/space from IDE.
Pivotal provides a lightweight developer version of Cloud Foundry called PCF Dev that can be installed on a single developer machine. If you don't want to install PCF Dev on your machine and still want to try Cloud Foundry you can skip installation part, only install CF CLI, and use evaluation version available in Pivotal Web Service. You need a free Pivotal account to try either of these.
PCF Dev Install
1) Install VirtualBox for your OS.
2) Install CF CLI.
3) Download PCF Dev for your choice of OS. Extract the PCF Dev into a folder, and run pcfdev-[VERSION]-[OS]. This installs PCFDev plugin to CF CLI.
4) Open a new terminal in this folder and execute command
cf dev start
5) This prompts you to enter the pivotal account credentials, and then downloads the VM, dependencies, starts all the required services. If you don't have a Pivotal account, sign up.
Once the CF is started, then you can open a new terminal and run CF commands to connect to Cloud Foundry.
Log Into Cloud Foundry
cf login -a [api-endpoint] --skip-ssl-validation
The API-endpoint for PCFDev is https://api.local.pcfdev.com and for Pivotal Web Services it is https://api.run.pivotal.io. This prompts for email and password. The email/password would be admin/admin for PCFDev and pivotal account email/password for Pivotal Web Services. Target to the Required Org and Space.
Deploying a Sample Application in Cloud Foundry
The sample application can be found in this Github location. This is a sample Spring Boot Web Services application exposing get API. In the default profile, the application creates an in-memory database and gets data from this database as JSON. In the cloud profile, the app connects to a MySQL database on the cloud with service name mysqldb. This project uses the Spring cloud service connector to connect to the cloud MySQL service.
Deploy the application in the ORG/Space selected using the command:
cf push [App-Name] -b [build-pack] -i [No-of-instances] -m [App-memory]
This deploys and starts the app. If no routes are specified then a default route with the same name as that of app-name would be created and bound to the app. The app can be also pushed using a manifest file. All the options can be used inside the manifest file:
cf push -f [Path-To-Manifest-File]
Sample contents of a manifest file:
applications: - name: pcf-demo-app buildpack: https://github.com/cloudfoundry/java-buildpack memory: 512M instances: 1 host: pcf-demo-app path: pcf-demo-1.0.0-SNAPSHOT.jar timeout: 180
See the Status of the Application
cf apps - Shows the status of all the apps in the space.
cf app [App-Name] - Shows the status of an app.
If everything goes well, then the app status would be like this:
Apps can be restarted or restaged:
cf restart [App-Name]
cf restage [App-Name]
Application logs can be viewed.
cf logs [App-Name] --recent Environment properties of the application can be viewed:
cf env [App-Name]
If the app status is showing "Running," then
CURL -k https://pcf-demo-app.pcfdev.com/user will fetch the data.
Run the Application in the Cloud Profile With MySQL Service
You can see all the services available in Cloud Foundry:
cf marketplace - Shows all the Marketplace services available.
cf marketplace -s [service-name] - Shows details of a particular service available in Marketplace.
cf create-service [service-name] [service-type] [service-instance-name] - This creates a service with name [service-instance-name] of [service-name], and type [service-type] in the current space.
cf services - This shows all the services in the current space.
cf service [service-instance-name] - This shows details about a specific service inside the space.
You can change the contents of a manifest file:
applications: - name: pcf-demo-app buildpack: https://github.com/cloudfoundry/java-buildpack memory: 512M instances: 1 host: pcf-demo-app path: pcf-demo-1.0.0-SNAPSHOT.jar timeout: 180 env: SPRING_PROFILES_ACTIVE: cloud services: - mysqldb
This creates the environment variable SPRING_PROFILES_ACTIVE and sets the value to cloud. It also creates a binding between the application that is getting pushed and the service mysqldb. This binding can also be manually created using this command:
cf bind-service [App-Name] [Service-Instance-Name]
Push the app again now, with the new manifest changes:
cf push -f [Path-To-Manifest-File]
You can again run CF env to view VCAP_SERVICES updated with mysqldb details.
Scale the Application
cf scale [App-name] -i [no-of-instances] -m [memory-limit] There are commands in CF CLI to delete, rename, start, and stop an app, logout a user, view the files, create/update/delete services, create/update/delete user-provided custom services, bind/unbind services, bind/unbind routes, create/delete orgs and spaces, create/update/delete build packs, create/delete users, and more.
If you are using Pivotal Cloud Foundry (the commercial distribution of Cloud Foundry) or Pivotal Dev, a Web-based console application is available with Cloud Foundry that can be used as an alternative to CF CLI.
Opinions expressed by DZone contributors are their own.