To deploy and configure a microservice, leverage the platform for scaling, monitoring & management, and create services that will be bound to the deployed application.
- Install the CF CLI
Download and install the Cloud Foundry Command Line Interface (CF CLI): Download and make sure it works:
cf help
Note: If you don't have admin privileges on your machine, download the appropriate binary.
- Register a free account on the Cloud Foundry public cloud of Pivotal called PWS (Pivotal Web Services)
or
download a copy of PCFDev which is a shrunk down version of PCF running on your laptop!
- Optional install and configure Java 7 (or later) https://java.com/en/download
A-1) Clone the sample application
$ git clone https://github.com/cloudfoundry-samples/spring-music
A-2) Use Gradle to assemble the app locally:
cd ./spring-music
./gradlew assemble
If you don't have Git installed, you can download a zip file of the app at https://github.com/cloudfoundry-samples/spring-music/archive/master.zip. Then navigate to the App directory cd ./spring-music
.
B-1) Download the zip file and extract it. Then navigate to the App directory cd ./spring-music
.
- Login to Pivotal Web Services with the credentials given
$ cf login -a https://api.run.pivotal.io
API endpoint: api.run.pivotal.io
Email> user
Password> pass
Hint We’ll use PWS in this guide as we move on. If you chose to use PCFDev, please login with
$ cf login -a https://api.local.pcfdev.io --skip-ssl-validation
API endpoint: api.local.pcfdev.io
Email> user
Password> pass
Some plans in the PCFDev marketplace might be named differently compared with PWS. Please adjust accordingly.
- Push the application
Please give the app a name to identify it later on e.g. spring-music
$ cf push my_app_name
- Get the url from the response given on the command line and open the sample app in your browser.
requested state: started
instances: 1/1
usage: 1G x 1 instances
urls: spring-music-germproof-obedience.cfapps.io
state since cpu memory disk details
#0 running 2016-07-10 09:49:06 AM 0.0% 426.7M of 1G 155.3M of 1G
Congratulations! You have successfully pushed your first application to Pivotal Cloud Foundry!
Hint: If you're wondering how the runtime to execute the Java app (Spring MVC) got built for you, have a look at the concept of buildpacks. More specifically, the Java buildpack. Cloud Foundry being a polyglot application platform (Java, node.js. ruby, php, go, python, etc.), you could also push a docker image (not available on PWS due to security reasons) or a .NET application.
As your application is running and gets more and more consumed by your customers, it’s time to scale.
Scaling your app horizontally adds or removes app instances. Adding more instances allows your application to handle increased traffic and demand.
Increase the number of app instances from one to two:
$ cf scale spring-music -i 2
Check the status of the app and verify there are two instances running:
$ cf app spring-music
...
state since cpu memory
#0 running 2016-02-23 10:55:08 AM 0.1% 461M of 512M
#1 running 2016-02-23 01:14:59 PM 0.0% 455.1M of 512M
If you access your application again via the web browser, you will have a round robin load balancing across all your app instances automatically. Skeptical? Add /env
to the URL and watch for CF_INSTANCE_PORT
to change while you refresh.
Hint: Scaling is a matter of seconds; we don't need to re-stage the app. The original staged artifact called droplet has been stored on the platform's internal blob store already. More on scaling can be found at Scaling an Application.
Now that we have two instances running, we might want to check if the automatic recovery i.e. restart works in case we kill one of the running instances. The spring-music application provides an endpoint, which will kill the process. Check afterwards if Pivotal Cloud Foundry will properly restart it and route traffic to the healthy instances only.
Open your application in a web browser again (be sure to replace something
with your random route) at
http://spring-music-something.cfapps.io/errors/kill
and you will see (cf app spring-music
) one application instance being restarted by the Elastic Runtime automatically. As you have two instances by now, the http://spring-music-something.cfapps.io
(again, be sure to replace something
with your random route) will still return your application as the traffic is only routed to healthy instances.
PCF provides access to an aggregated view of logs related to you application. This includes HTTP access logs, as well as output from app operations such as scaling, restarting, and restaging. Simply log to STDOUT or STDERR within your app and the logs will get picked up by the platform's log aggregation system.
To view your recent logs use
$ cf logs spring-music --recent
or to get the live stream use
$ cf logs spring-music
Reload the app page to see activity. Press control-c
to stop streaming.
Hint: More on logs can be found at Streaming Logs
Besides the integrated health monitoring, the platform provides an agentless app monitoring for container metrics as well as latency, number of requests, etc. You can access the latest version (beta) of Pivotal CF Metrics under https://metrics-new.run.pivotal.io (requires authentication with the same user you logged-in via CF CLI). Enter your application name in the search box e.g. spring-music. Explore the current capabilities. Please note that you might need to access the app again to see metrics (ca. 3s delay).
Pivotal Cloud Foundry has a concept of a marketplace where various services can be consumed as needed and bound to an app. The used spring-music application uses a temporary h2 in-memory database by default. However, it also supports MySQL, Postgres, Redis, and MongoDB as a datasource to manage the music albums.
You can see what type of database the spring-music app is currently using by clicking the info icon in the upper right corner.
In the next step we’ll connect to a MySQL database to allow persistence.
Display the available services from the built-in marketplace.
$ cf marketplace
As we want to use a MySQL database, let’s display which plans are available.
$ cf marketplace -s cleardb
We’ll create a service using the free plan for MySQL. Please use free plans only :) Again, provide a unique identifier for the service instance name (e.g. mysql-db).
$ cf create-service cleardb spark mysql-db
To make the credentials available to the application we pushed, we need to bind this service to the application. This injects the credentials and connection information into the app via environment variables.
$ cf bind-service spring-music mysql-db
Once bound to the app, environment variables are stored that allow the app to connect to the service after a push, restage or restart command.
Let’s restart the app.
$ cf restart spring-music
To verify the new service is bound to the app, you can either click on the info icon in the upper right corner of the app or check using this command:
$ cf services
Getting services in org your-org / space development ...
OK
name service plan bound apps
mysql-db cleardb spark spring-music
Hint: More on services can be found at Managing Services. Please note that the marketplace is extensible by your own services.
Hint: We're using the Pivotal Web Services, the public offering of Pivotal in this example. For your own installation of Pivotal Cloud Foundry (on VMware, Openstack, AWS, Azure, etc.), check out the Pivotal Network to see what Marketplace services are being available.
Besides the CF CLI
, you can access and manage your application via a GUI called Apps Manager. Explore the capabilites, access your app and review its events, browse the Marketplace etc. Access the Apps Manager at https://console.run.pivotal.io and login with the same credentials used for the authentication via CF CLI
.
Please find below further information to get more familiar with Pivotal Cloud Foundry.
- Create a free account on our public cloud
- Get PCF on your local workstation with PCF Dev (single VM)
- Migrating to Cloud Native Application Architectures
- Beyond the 12-Factor App
- Cloud Foundry: The Cloud Native Platform
- Migrating Legacy Applications to Cloud
Netflix OSS, Spring Cloud and Pivotal Cloud Foundry are a great way to build and manage your microservices architecture. Spring Cloud Services, available in the Marketplace, provide a Configuration Server powered by Spring Cloud Config, a Service Registry, powered by the battle-tested Netflix OSS Eureka server as well as a Circuit Breaker Dashboard, powered by the combination of Netflix OSS Turbine and Hystrix.
A sample app using all of those services is the fortune-teller available on github.
You might want to check out User Provided Services
This will let you create and bind any service provided to an app of your choice. If you have a Papertrail Account, try if you can bind this application to your app and see your logs there.
Hints:
cf help cups
will provide you with the syntax for binding e.g. a log aggragation service to your application.
Hint: More on this can be found at User-Provided Service Instances