cloudsoft.io

Swarm Location Tutorial

Introduction

This tutorial is focused on deploying apps to a Swarm cluster. Please note, this can be any swarm cluster, not just a cluster provisioned by AMP. However for the purpose of this tutorial we will assume that you provisioned the cluster using the approach described in the Swarm Cluster Tutorial

Pre-requisites

This tutorial assumes you have installed Cloudsoft AMP, and the AMP CLI. It also assumes you have followed the steps to get a swarm cluster running.

Instructions

Creating an AMP location

To deploy to a swarm cluster we must represent the cluster as an AMP location. We will do this by creating a swarm location in our amp catalog with the following yaml:

brooklyn.catalog:
  version: "1.0.0"
  items:
  - id: swarm-location
    itemType: location
    item:
      type: docker:https://<HOSTNAME>:<PORT>/
      brooklyn.config:
        templateOptions:
          networkMode: brooklyn
        identity: <path/to/cert.pem>
        credential: <path/to/key.pem>

The hostname and port can be retrieved from the swarm.endpoint sensor on the swarm entity. The following command will retrieve this info:

br app swarm entity swarm-manager-load-balancer sensor swarm.endpoint

You should use the cert.pem and key.pem created in the Swarm Cluster Tutorial.

Once you have added these values to the bom file you can add it to you catalog with this command:

br add-catalog swarm-example-location.bom

Deploying to our swarm cluster

The location we just created can be used, as you would use any other cloud location. The yaml below deploys a three tier web app to your swarm location.

location: swarm-location
name: 3-Tier web app on swarm
services:
- type: org.apache.brooklyn.entity.group.DynamicCluster
  id: cluster
  name: "Webapp Cluster"
  brooklyn.config:
    initialSize: 2
    memberSpec:
      $brooklyn:entitySpec:
        type: org.apache.brooklyn.entity.webapp.tomcat.TomcatServer
        id: tomcat
        name: "Tomcat Server"
        brooklyn.config:
          wars.root:
            "http://search.maven.org/remotecontent?filepath=org/apache/brooklyn/example/brooklyn-example-hello-world-sql-webapp/0.9.0/brooklyn-example-hello-world-sql-webapp-0.9.0.war"
          java.sysprops:
            brooklyn.example.db.url:
              $brooklyn:formatString:
                - "jdbc:mysql://%s:%s/%s?user=%s&password=%s"
                - $brooklyn:entity("db").attributeWhenReady("host.subnet.address")
                - $brooklyn:entity("db").attributeWhenReady("mysql.port")
                - "visitors"
                - "brooklyn"
                - "br00k11n"

- type: org.apache.brooklyn.entity.proxy.nginx.NginxController
  id: nginx
  name: "Load Balancer (nginx)"
  brooklyn.config:
    loadbalancer.serverpool: $brooklyn:sibling("cluster")
    nginx.sticky: false
  brooklyn.enrichers:
    - type: org.apache.brooklyn.core.network.OnPublicNetworkEnricher
      brooklyn.config:
        sensors:
          - main.uri

- type: org.apache.brooklyn.entity.database.mysql.MySqlNode
  id: db
  name: "Database (MySQL)"
  brooklyn.config:
    datastore.creation.script.url:
      "https://github.com/apache/brooklyn-library/raw/master/examples/simple-web-cluster/src/main/resources/visitors-creation-script.sql"

You can deploy it with the following command:

br deploy multi-node-application.bom

Once it has been deployed you can get the publicly available endpoint using the following command:

br app "3-Tier web app on swarm" entity "Load Balancer (nginx)" sensor main.uri.mapped.public

If you inspect the above yaml it is similar to the yaml discussed in previous tutorials e.g. Policy Tutorial except we have included a separate mysql database. The main difference is the use of the OnPublicNetworkEnricher. We use this to indicate to AMP any ports that need to be available outside the swarm network. In this case we are making the load balancer available so that we can use our deployed app. We might also use this if we were deploying an application on a mix of containers and virtual machines.