Docker Swarm Sample

Docker swarm sample with NodeJS app on Hyper-V

Prerequisites


  • This sample is using Hyper-V

1. Clone sample repository

git clone https://github.com/wswijaya/docker-swarm-node-app.git

2. Build Docker Image and push to Docker Hub

docker build -t /node-app-sample .
docker push /node-app-sample

3. Create VMs

docker-machine create -d hyperv --hyperv-virtual-switch "myswitch" myvm3
docker-machine create -d hyperv --hyperv-virtual-switch "myswitch" myvm2
docker-machine create -d hyperv --hyperv-virtual-switch "myswitch" myvm3
docker-machine create -d hyperv --hyperv-virtual-switch "myswitch" myvm4

4. Create Docker Swarm and set VM1, VM3, and VM4 as manager

docker-machine ssh myvm1 "docker swarm init"
docker-machine ssh myvm1 "docker swarm join-token manager"
docker-machine ssh myvm3 "docker swarm join --token :"
docker-machine ssh myvm4 "docker swarm join --token :"
docker-machine ssh myvm1 "docker swarm join-token worker"
docker-machine ssh myvm2 "docker swarm join --token :"

5. Deploy Services

docker-machine scp docker-compose-node-app.yml myvm1:~
docker-machine ssh myvm1 "docker stack deploy -c docker-compose-node-app.yml nodeapplab"
docker-machine ssh myvm1 "docker stack ps nodeapplab"

6. Test Site

7. Remove Services

docker-machine ssh myvm1 "docker stack rm nodeapplab"

8. Remove Docker Swarm

docker-machine ssh myvm1 "docker stack rm nodeapplab"
docker-machine ssh myvm2 "docker swarm leave"
docker-machine ssh myvm3 "docker swarm leave --force"
docker-machine ssh myvm4 "docker swarm leave --force"
docker-machine ssh myvm1 "docker swarm leave --force"

Optional: Configure Load Balancer using HAProxy

Installation steps for HAProxy on Ubuntu -> How to use HAProxy

Sample config file /etc/haproxy/haproxy.cfg

global
daemon
maxconn 200

defaults
mode http
timeout connect 5000ms
timeout client 10000ms
timeout server 10000ms

frontend http-in
bind *:9090
default_backend servers

backend servers
server vm1 : maxconn 40
server vm3 : maxconn 40
server vm4 : maxconn 40
Advertisements

HTML5 @Google I/O 2011

Just sharing some of the talk/presentations on HTML5 at Google I/O 2011. HTML5 seems to be moving fast and you will see some of the cool stuff in the “HTML5 versus Android” demo,

  • I/O BootCamp 2011: Getting Started with HTML5

 

  • HTML5 Showcase for Web Developers: The Wow and the How

 

  • HTML5 and What’s Next

 

  • GWT + HTML5: A web developers dream!

 

  • HTML5 vs. Android: Apps or Web Mobile Development?

 

  • HTML5 Today with Google Chrome Frame

 

  • Kick-Ass Game Programming with Google Web Toolkit

 

 

Sencha Touch MVC Application – Part 1

Developing MVC application is nothing new and there are many frameworks to help us implement MVC, e.g. Struts & Spring MVC (JEE), ASP.NET MVC, JavaScriptMVC, etc. If you are a mobile apps developer and using Sencha Touch, you will most likely wonder whether or not you can apply MVC to simplify your apps design and make it more maintainable.

Here I am sharing a useful presentation by Tommy Maintz (@tommymaintz). This presentation will bring you a step further into a better Sencha Touch mobile apps design by structuring your apps and applying MVC pattern.

Countdown to ExtJS 4

On the 16th Feb 2010 Sencha announced the availability of the first Ext JS 4 Developer Preview. Definitely an exciting news for the Ext JS developers, I can see from the comments in Sencha Blog that people cannot wait for the release. This is the biggest overhaul to the framework. For those who worry about migration, the team has taken that into consideration.

Ext JS 4 is completely sandboxed; there is no more native object augmentation or reliance on global variables. This means you can run Ext JS 4 alongside Ext JS 3 on the same page. To demonstrate this we’ve taken the best of both versions and combined them in a single page. We’ve used the Desktop example from Ext JS 3 and loaded Ext JS 4’s brand new charts alongside as if they were part of Desktop itself – check it out for yourself.

The second step is to provide as strong a legacy layer as possible to help you upgrade your Ext JS 3.x applications. We’re starting that today with the release of a detailed overview guide to the differences between the versions, and will update this as we continue our rollout. In addition to the guide we will be providing a legacy file that can be dropped in to an Ext JS 3.x application to help you upgrade your app. We’ll be expanding on both of these in another post very soon. – Sencha Blog

The following articles highlights the key features in Ext JS 4;

Ed Spencer from Sencha presented the introduction to Ext JS 4 and its architecture. You can see the slides below;

Last but not least, the examples and download is available;

Happy hacking!!

Safari5 Unable To Get Geolocation, Why?

With the latest release of Safari, the browser claims to support HTML5. A few days ago, I got a feedback that the NearBy Tweet prototype is not working in Safari5 (desktop). I never test it in Safari before, and turn out it doesn’t work due to no geolocation detected. So I tried map.google.com, it was unable to show my location as well. See screenshot below.


After a bit of digging around, It turns out Safari5 is using CoreLocation (wifi triangulation) to get geolocation data and it is only works when Wifi is ON. If you are using Safari5 on PC just turn on your wireless and if you are using Mac, just turn on Airport. It works!!

Find Destination (Lat,Lng) Given Distance & Bearing

Here, I will be posting a C# implementation to find a destination (latitude, longitude) given distance and bearing. This will be continuation from my previous post. Just add the method to the skeleton class GLatLng.

public GLatLng DestinationPoint(double dist, double brng, DistanceType dType)
{
    double R = (dType == DistanceType.Miles) ? EarthRadiusInMiles : EarthRadiusInKilometers;
    dist = dist / R;
    brng = Math.PI * brng / 180;
    double lat1 = DegreeToRadian(latitude);
    double lon1 = DegreeToRadian(longitude);
<p>    double lat2 = Math.Asin(Math.Sin(lat1) * Math.Cos(dist) + Math.Cos(lat1) * Math.Sin(dist) * Math.Cos(brng));
    double lon2 = lon1 + Math.Atan2(Math.Sin(brng) * Math.Sin(dist) * Math.Cos(lat1), Math.Cos(dist) - Math.Sin(lat1) * Math.Sin(lat2));
<p>    lon2 = (lon2 + 3 * Math.PI) % (2 * Math.PI) - Math.PI;
    lat2 = RadianToDegree(lat2);
    lon2 = RadianToDegree(lon2);
<p>    GLatLng newLatLng = new GLatLng(lat2, lon2);
    return newLatLng;
} // end DestinationPoint
public GLatLng RhumbDestinationPoint(double dist, double brng, DistanceType dType)
{
    double R = (dType == DistanceType.Miles) ? EarthRadiusInMiles : EarthRadiusInKilometers;
    double d = dist / R;  
    double lat1 = DegreeToRadian(this.latitude);
    double lon1 = DegreeToRadian(this.longitude);
    brng = DegreeToRadian(brng);
    double lat2 = lat1 + d * Math.Cos(brng);
    double dLat = lat2 - lat1;
    double dPhi = Math.Log(Math.Tan(lat2 / 2 + Math.PI / 4) / Math.Tan(lat1 / 2 + Math.PI / 4));
    double q = Math.Cos(lat1);
    if (dPhi != 0) q = dLat / dPhi;  // E-W line gives dPhi=0
    double dLon = d * Math.Sin(brng) / q;
     if (Math.Abs(lat2) &gt; Math.PI / 2) lat2 = (lat2 &gt; 0) ? Math.PI - lat2 : -(Math.PI - lat2);
    double lon2 = (lon1 + dLon + 3 * Math.PI) % (2 * Math.PI) - Math.PI;
   return new GLatLng(RadianToDegree(lat2), RadianToDegree(lon2));
} // end RhumbDestinationPoint

Calculate Distance & Bearing Between Geolocation

Recently, I toyed with “nearby” services based on geolocation. In this post, I will be sharing C# implementation to calculate distance and bearing between geolocation.  

Let’s start with the skeleton class GLatLng below

using System;

namespace GMap
{
    public enum DistanceType : int
    {
        Miles = 0,
        Kilometers = 1
    }
   
    public class GLatLng
    {
        public const double EarthRadiusInMiles = 3956.0;
        public const double EarthRadiusInKilometers = 6367.0;
       
        private double latitude;
        private double longitude;

        public GLatLng(double latitude, double longitude)
        {
            this.latitude = latitude;
            this.longitude = longitude;
        }

        public double DegreeToRadian(double angle) { return Math.PI * angle / 180.0; }

        public double RadianToDegree(double angle) { return 180.0 * angle / Math.PI; }
       
        public double Latitude
        {
            get { return this.latitude; }
            set { this.latitude = value; }
        }
        public double Longitude
        {
            get { return this.longitude; }
            set { this.longitude = value; }
        }
    } // end class GLatLng
}

The following is the method to calculate the distance. You can choose the distance type between miles or kilometers. This uses haversine formula which give great-circle distances between two points on a sphere from their longitudes and latitudes.

public double DistanceTo(double lat, double lng, DistanceType dType)
{
    double R = (dType == DistanceType.Miles) ? EarthRadiusInMiles : EarthRadiusInKilometers;
    double dLat = DegreeToRadian(lat) - DegreeToRadian(this.latitude);
    double dLon = DegreeToRadian(lng) - DegreeToRadian(this.longitude);
    double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(DegreeToRadian(this.latitude)) * Math.Cos(DegreeToRadian(lat)) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
    double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
    double distance = c * R;

    return Math.Round(distance, 2);
} // end DistanceTo

The second way to calculate the distance is based on the rhumb line which generally longer than great-circle routes.

public double RhumbDistanceTo(double lat, double lng, DistanceType dType)
{
    double R = (dType == DistanceType.Miles) ? EarthRadiusInMiles : EarthRadiusInKilometers;
    double lat1 = DegreeToRadian(this.latitude);
    double lat2 = DegreeToRadian(lat);
    double dLat = DegreeToRadian(lat - this.latitude);
    double dLon = DegreeToRadian(Math.Abs(lng - this.longitude));

    double dPhi = Math.Log(Math.Tan(lat2 / 2 + Math.PI / 4) / Math.Tan(lat1 / 2 + Math.PI / 4));
    double q = Math.Cos(lat1);
    if (dPhi != 0) q = dLat / dPhi;  // E-W line gives dPhi=0
    // if dLon over 180° take shorter rhumb across 180° meridian:
    if (dLon > Math.PI) dLon = 2 * Math.PI - dLon;
    double dist = Math.Sqrt(dLat * dLat + q * q * dLon * dLon) * R;

    return dist;
} // end RhumbDistanceTo

Next methods calculate the bearing between geolocation. Similar to distance there are 2 ways to calculate the bearing.


public double RhumbBearingTo(double lat, double lng)
{
    double lat1 = DegreeToRadian(this.latitude);
    double lat2 = DegreeToRadian(lat);
    double dLon = DegreeToRadian(lng - this.longitude);

    double dPhi = Math.Log(Math.Tan(lat2 / 2 + Math.PI / 4) / Math.Tan(lat1 / 2 + Math.PI / 4));
    if (Math.Abs(dLon) > Math.PI) dLon = (dLon > 0) ? -(2 * Math.PI - dLon) : (2 * Math.PI + dLon);
    double brng = Math.Atan2(dLon, dPhi);

    return (RadianToDegree(brng) + 360) % 360;
} // end RhumbBearingTo

public double BearingTo(double lat, double lng)
{
    double lat1 = DegreeToRadian(this.latitude);
    double lat2 = DegreeToRadian(lat);
    double dLon = DegreeToRadian(lng) - DegreeToRadian(this.longitude);

    double y = Math.Sin(dLon) * Math.Cos(lat2);
    double x = Math.Cos(lat1) * Math.Sin(lat2) - Math.Sin(lat1) * Math.Cos(lat2) * Math.Cos(dLon);
    double brng = Math.Atan2(y, x);

    return (RadianToDegree(brng) + 360) % 360;
} // end BearingTo