"it's very difficult to be a 'deep' IoT technologist—you have to be naturally curious about the world and a renaissance person at heart."

Docker swarm on Raspberry pi 

what

 

  1. HypriotOS
    A minimal Debian-based operating systems that is optimized to run Docker.

  2. RaspberryPi
    Raspberry Pi is a fully functional Linux computer.
  3. Flashtool 
    Command line script to flash SD card images of any kind.

  4. Docker-Machine
    Docker Machine is a tool that lets you install Docker Engine on virtual hosts, and manage the hosts with docker-machine commands.

WHY docker 

  1. Easier environment  setup 
  2. Single-threaded in multi-core 
  3. Virtualization 
  4. Microservice
  5. Fast and easy to rollback 
  6. High Availability 

>200,000 pulls

>7,500 github stars

>200 significant contributors

>200 projects built on top of docker

UIs, mini-PaaS, Remote Desktop...

1000’s of Dockerized applications

Memcached, Redis, Node.js, Hadoop...

Integration in Jenkins, Travis, Chef, Puppet, Vagrant and OpenStack

Meetups arranged around the world…

Containers vs VM

Simply put, containers provide OS-level process isolation whereas virtual machines offer isolation at the hardware abstraction layer (i.e., hardware virtualization).

why hypriot

  • minimal Debian-based operating system - most people know how to use Debian & Ubuntu based distros

  • optimized for Docker awesomeness - from optimized and tuned Linux kernel settings to the included fileystems everything is aligned to make Docker run very well

  • up-to-date Docker versions - often we are only mere seconds behind the officially published upstream versions

  • really easy to use - download, flash and boot - that’s all you need to get started

Prepare your notebook :

To control the Docker Swarm from our notebook, we have to install both dockerand docker-machine binaries. If you are on a Mac, you can use brew to install them.

brew install docker
brew install docker-machine

Flash all your SD cards

Now we can easily flash the latest HypriotOS image to the eight SD cards with our flash tool and assign different node names.

Flash --hostname swarm-dc1-pi01 hypriotos-rpi-v1.1.3.img.zip
Flash --hostname swarm-dc1-pi02 hypriotos-rpi-v1.1.3.img.zip
Flash --hostname swarm-dc1-pi03 hypriotos-rpi-v1.1.3.img.zip

The flash 

$ docker run -ti resin/rpi-raspbian:jessie-20160831 /bin/bash

Use Cases 

Make use of GPIO

FROM resin/rpi-raspbian:jessie-20160831

RUN apt-get -q update && \  
    apt-get -qy install \
        python python-pip \
        python-dev python-pip gcc make  
RUN pip install rpi.gpio  
$ docker build -t gpio-base 
import RPi.GPIO as GPIO  
import time  
GPIO.setmode(GPIO.BCM)  
led_pin = 17  
GPIO.setup(led_pin, GPIO.OUT)

while(True):  
    GPIO.output(led_pin, GPIO.HIGH)
    time.sleep(1)
    GPIO.output(led_pin, GPIO.LOW)
    time.sleep(1)
FROM gpio-base:latest  
ADD ./app.py ./app.py

CMD ["python", "app.py"]  
$ docker build -t blink .
$ docker run -ti --privileged blink
function getip() { (traceroute $1 2>&1 | head -n 1 | cut -d\( -f 2 | cut -d\) -f 1) }

IP_ADDRESS=$(getip swarm-dc1-pi01.local)

#ssh-keygen -R $IP_ADDRESS
ssh-copy-id -oStrictHostKeyChecking=no -oCheckHostIP=no pirate@$IP_ADDRESS

ssh pirate@$IP_ADDRESS sudo sed -i \'s/ID=raspbian/ID=debian/g\' /etc/os-release

Prepare your Pi’s for docker-machine

We want to create the Docker Swarm with the standard docker-machine binary. To make this work we need to prepare the Raspberry Pi’s a little bit. The next steps are adding your SSH public key to all of your Pi’s as well as fixing the ID in /etc/os-release to debian

Save the following script into a bash script and run the script for all swarm member :

Create Swarm Token

A Docker Swarm cluster uses a unique Cluster ID to enable all swarm agents find each other. We need such a Cluster ID to build our Docker Swarm. This can be done in your shell with this command

export TOKEN=$(for i in $(seq 1 32);
do echo -n $(echo "obase=16; $(($RANDOM % 16))" | bc); done; echo)

Create the swarm with docker-machine

We now create the Swarm Master on the first Raspberry Pi.

 
docker-machine create -d generic \
  --engine-storage-driver=overlay --swarm --swarm-master \
  --swarm-image hypriot/rpi-swarm:latest \
  --swarm-discovery="token://$TOKEN" \
  --generic-ip-address=$(getip swarm-dc1-pi01.local) \
  --generic-ssh-user pirate \
  swarm-dc1-pi01

Building the swarm using docker 

 ssh pirate@swarm-dc1-pi01.local

 Sudo service docker start 

 Docker info 

 Docker swarm init 

If the command is successful  

 docker swarm join \
    --token
     SWMTKN-1-33i9udav8jwj6ruzsu2y8gspe8f7c97l3f5g3zfxdoaxorngkr-15puvftww2culnffd80ecj3ik \
    192.168.0.102:2377
ssh pirate@swarm-dc1-pi02.local
service docker start 
docker info 

 docker swarm join \
    --token
     SWMTKN-1-33i9udav8jwj6ruzsu2y8gspe8f7c97l3f5g3zfxdoaxorngkr-15puvftww2culnffd80ecj3ik \
    192.168.0.102:2377



ssh pirate@swarm-dc1-pi03.local
service docker start 
docker info 

 docker swarm join \
    --token
     SWMTKN-1-33i9udav8jwj6ruzsu2y8gspe8f7c97l3f5g3zfxdoaxorngkr-15puvftww2culnffd80ecj3ik \
    192.168.0.102:2377
docker node ls

Get Swarm Status 

docker node promote NODENAME

Lets test the Swarm 

## First Scenario 

docker service create --name ping1 --replicas=2 alexellis2/arm-pingcurl ping google.com

Docker service ls 

Docker service ps ping1 

Docker ps

Docker log xxxxxx | tail -n 10 

## Cleanup 

docker service rm ping1
## Scenario 2 - the hello world webservice


docker service create --name hello1 --publish 3000:3000 --replicas=3 alexellis2/arm-alpinehello
docker service create --name hello1 --publish 3000:3000 --replicas=3 alexellisio/guid-generator

# Test 

 # curl -4 localhost:3000 
   hello
 # curl -4 localhost:3000
   hello
 # curl -4 localhost:3000
   hello
 # curl -4 localhost:3000
   hello


# Cleanup

docker service rm hello1
## Scenario 3 - inter-container communication

>>Thie scenario tests inter-container resolution and connectivity.
 A redis database runs on only one node and on both nodes we run a hit
 counter which will increment a number held in the redis database with each GET request.

>>Make sure you have at least 2 nodes in your swarm.

 docker network ls 

 docker network create --driver overlay --subnet 20.0.14.0/24 armnet

 docker service create --replicas=1 --network=armnet --name redis alexellis2/redis-arm:v6

 docker service ls 
 docker service ps redis 

 docker service create --name counter --replicas=2 
--network=armnet --publish 3000:3000  alexellis2/arm_redis_counte

 docker service ls
 docker service ps counter

 curl http://localhost:3000/incr/ 
 ab -n 100 -c1 http://localhost:3000/incr/
docker run -d -p 9000:9000 -v "/var/run/docker.sock:/var/run/docker.sock"  portainer/portainer

Visualize Your Docker Swarm 

There is many tools available i choose portainer

What 

Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications.

It groups containers that make up an application into logical units for easy management and discovery.

 

Features 

WHY !

  • Lightweight: Kubernetes painlessly runs on a Raspberry Pi.

  • ARM compatible: Kubernetes is ARM compatible. 

  • General purpose: Kubernetes uses a container runtime
    (with Docker as the 100% supported runtime for the time being) that
    allows to run whatever you want.

  • Production ready: Kubernetes itself is production ready,

https://blog.hypriot.com/post/setup-kubernetes-raspberry-pi-cluster/

Thank You 

@conversionpoint

Simon Tadros

github/evilqubit

Arabnet2017-d0c|<er0nP1

By Simon Tadros

Arabnet2017-d0c|<er0nP1

In a the world of containers This workshop aims to create a Swarm cluster, built with C.H.I.P. computers and a Raspberry Pis . Connect them securely from a local computer via Docker Machine and provides native clustering capabilities to turn a group of Docker engines into a single, virtual Docker instance . The docker instance will act as a webserver that will handle a nodejs simple express application . A stress/load test will be conducted to show the advantage of such cluster . The workshop will cover Docker introduction and how developers can use Docker to pack, ship, and run any application as a lightweight, portable, self sufficient LXC container .

  • 1,706