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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s