Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DockerGrunn Meetup #7: Docker Swarm on a Raspberry Pi Cluster

DockerGrunn Meetup #7: Docker Swarm on a Raspberry Pi Cluster

Slide deck for the 7th Docker Grunn Meetup of 18 January 2017

https://www.meetup.com/DockerGrunn/events/236134010/

Albert W. Alberts

January 18, 2017
Tweet

More Decks by Albert W. Alberts

Other Decks in Programming

Transcript

  1. Docker Swarm On a Raspberry Pi Cluster Dokcer Swarm on

    a Raspberry Pi cluster 18 - 01 - 2017 Albert W. Alberts
  2. ! "♂ Albert W. Alberts employer: KPN function: architect current

    projects: CloudNL VMware & Viper Next hobbies: meetups, travelling, cycling, gadgets, [email protected]
  3. Raspberry Pi 3 B CPU: ARM Cortex-A53 Quad 64 bit

    CPU Clock: 1.2 GHz RAM: 1 GB Memory: 8 GB microSD Ethernet: 100 Mbit WLAN, BT
  4. Raspberry Pi 3 B 2 x USB 100MBit Ethernet micro-USB

    voeding HDMI GPIO-pinnen micro-SD kaart camera kop- telefoon 2 x USB monitor
  5. Raspberry Pi Cluster hardware Raspberry Pi 6x CPU: ARM Cortex-A53

    Quad 64 bit CPU Clock: 1.2 GHz RAM: 1 GB Memory: 8 GB microSD Ethernet: 100 Mbit 6 x 4 Cores 6 x 1 GB 6 x 8 GB = 24 Cores op 1.2 GHz = 6 GB RAM = 48 GB storage op microSD 6x
  6. Raspberry Pi Cluster hardware Raspberry Pi 6x TP-Link TL-SF1008D Desktop

    Switch 8x 10/100MBit/s Anker 60W PowerPort 6-Port USB
  7. Raspberry Pi Cluster hardware Dragan, Bathroomset, bamboo CAT5 patch cables

    PVC spacers Euro printboard Metalfilm resistor 8 GB Micro SD kaart 2x5 pin female doublerow straight header connector RGB-led Common Cathode
  8. Common Cathode RGB LED for Raspberry Pi 3 B “A

    common cathode led LED should have the common leg connected to the ground and the others connected to 3 seperate GPIO pins. Connect the common pin to ground, then wire the other three legs of the LED to the desired GPIO pins. In your code pull these pins high to light the individual colors. The resistors should go between the LED color pins and the GPIO pins.” the “design”
  9. Common Cathode RGB LED for Raspberry Pi 3 B Wire

    Pin# Name Black 09 Ground Red 15 GPIO 22 Green 13 GPIO 27 Blue 11 GPIO 17 G 17 27 22 L
  10. Raspberry Pi Cluster setup in this demo Internet Router DHCP/DNS/GW

    blacknode greennode rednode pinknode yellownode whitenode X
  11. Raspberry Pi Cluster setting up the micro-SD cards $ ./flash

    –hostname blacknode.local hypriot.img 5) Repeat step 4 for each node. $ brew install pv HypriotOS 1.1.3 + micro-SD Card 1) Install the Hypriot flash tool. 2) Download the latest version of the HypriotOS. 3) Install pv (optional for visualization). + Hypriot flash = bootable micro-SD card $ flash … 4) Flash the image to the micro-SD card.
  12. Raspberry Pi Cluster setting up SSH blacknode greennode rednode pinknode

    yellownode whitenode $ ssh-copy-id -i ~/.ssh/id_rsa.pub pirate @ blacknode.local $ ssh pirate @ blacknode.local Repeat for each node: $ brew install ssh-copy-id $ ssh-keygen -t rsa
  13. Docker Swarm Docker Swarm provides native clustering capabilities to turn

    a group of Docker engines into a single, virtual Docker Engine.
  14. Docker Swarm features Cluster management integrated with Docker Engine: Use

    the Docker Engine CLI to create a swarm of Docker Engines where you can deploy application services. You don’t need additional orchestration software to create or manage a swarm. Decentralized design: Instead of handling differentiation between node roles at deployment time, the Docker Engine handles any specialization at runtime. You can deploy both kinds of nodes, managers and workers, using the Docker Engine. This means you can build an entire swarm from a single disk image. Declarative service model: Docker Engine uses a declarative approach to let you define the desired state of the various services in your application stack. For example, you might describe an application comprised of a web front end service with message queueing services and a database backend. Scaling: For each service, you can declare the number of tasks you want to run. When you scale up or down, the swarm manager automatically adapts by adding or removing tasks to maintain the desired state. Desired state reconciliation: The swarm manager node constantly monitors the cluster state and reconciles any differences between the actual state your expressed desired state. For example, if you set up a service to run 10 replicas of a container, and a worker machine hosting two of those replicas crashes, the manager will create two new replicas to replace the replicas that crashed. The swarm manager assigns the new replicas to workers that are running and available. Multi-host networking: You can specify an overlay network for your services. The swarm manager automatically assigns addresses to the containers on the overlay network when it initializes or updates the application. Service discovery: Swarm manager nodes assign each service in the swarm a unique DNS name and load balances running containers. You can query every container running in the swarm through a DNS server embedded in the swarm. Load balancing: You can expose the ports for services to an external load balancer. Internally, the swarm lets you specify how to distribute service containers between nodes. Secure by default: Each node in the swarm enforces TLS mutual authentication and encryption to secure communications between itself and all other nodes. You have the option to use self-signed root certificates or certificates from a custom root CA. Rolling updates: At rollout time you can apply service updates to nodes incrementally. The swarm manager lets you control the delay between service deployment to different sets of nodes. If anything goes wrong, you can roll-back a task to a previous version of the service.
  15. Raspberry Pi Cluster with Docker Swarm blacknode greennode rednode pinknode

    yellownode whitenode $ ssh pirate @ blacknode.local $ docker swarm init The initiating node automatically becomes the swarm manager.
  16. Raspberry Pi Cluster with Docker Swarm blacknode greennode rednode pinknode

    yellownode whitenode $ ssh pirate @ greennode.local $ docker swarm join --token [workertoken] [Swarm IP-address]:2377 The greennode will be added to the swarm.
  17. Raspberry Pi Cluster with Docker Swarm blacknode greennode rednode pinknode

    yellownode whitenode All nodes are now added to the swarm cluster.
  18. Raspberry Pi Cluster with Docker Swarm blacknode greennode rednode pinknode

    yellownode whitenode Service A Service B Service C $ ssh pirate @ blacknode.local $ docker service create --name busybox -p 8081:80 hypriot/rpi-busybox-httpd $ docker service create --name whoami -p 8082:8000 hypriot/rpi-whoami Create the services … on the cluster
  19. Raspberry Pi Cluster with Docker Swarm blacknode greennode rednode pinknode

    yellownode whitenode Service A Service B Service C $ ssh pirate @ blacknode.local $ docker service update busybox --replicas 12 $ docker service update whoami --replicas 12 Create extra instances (replicas) … on the cluster
  20. Raspberry Pi Cluster with Docker Swarm blacknode greennode rednode pinknode

    yellownode whitenode Service A Service B Service C Docker Swarm runs multiple instances of the services, which node is no longer relevant.
  21. Raspberry Pi Cluster with Docker Swarm Visualization of the Docker

    Swarm running on the blacknode [source: alexellis2/visualizer-arm]
  22. Docker Swarm deployment with Ansible Ansible is used to deploy

    the Docker Swarm on the Raspberry Pi stack. inventory playbooks + =
  23. Docker Swarm deployment: Ansible playbooks PLAYBOOK blacknode greennode rednode pinknode

    yellownode whitenode swarmSetup.yml: - Initialize the swarm - Open the visualization page - Add the nodes to the cluster - Start the visualization container
  24. Docker Swarm deployment: Ansible playbooks PLAYBOOK blacknode greennode rednode pinknode

    yellownode whitenode swarmSetup.yml: - Initialize the swarm - Open the visualization page - Add the nodes to the cluster - Start the visualization container
  25. Docker Swarm deployment: Ansible playbooks PLAYBOOK blacknode greennode rednode pinknode

    yellownode whitenode swarmSetup.yml: - Initialize the swarm - Open the visualization page - Add the nodes to the cluster - Start the visualization container
  26. Docker Swarm deployment: Ansible playbooks PLAYBOOK blacknode greennode rednode pinknode

    yellownode whitenode swarmSetup.yml: - Initialize the swarm - Open the visualization page - Add the nodes to the cluster - Start the visualization container
  27. Docker Swarm deployment: Ansible playbooks PLAYBOOK blacknode greennode rednode pinknode

    yellownode whitenode servicesSetup.yml: - Start the whoami service - Start the busybox service - Open the busybox page - Open the whoami page
  28. Docker Swarm deployment: Ansible playbooks PLAYBOOK blacknode greennode rednode pinknode

    yellownode whitenode servicesSetup.yml: - Start the whoami service - Start the busybox service - Open the busybox page - Open the whoami page
  29. Docker Swarm deployment: Ansible playbooks PLAYBOOK blacknode greennode rednode pinknode

    yellownode whitenode servicesSetup.yml: - Start the whoami service - Start the busybox service - Open the busybox page - Open the whoami page
  30. Docker Swarm deployment: Ansible playbooks PLAYBOOK blacknode greennode rednode pinknode

    yellownode whitenode servicesSetup.yml: - Start the whoami service - Start the busybox service - Open the busybox page - Open the whoami page
  31. Docker Swarm demo: Add replicas blacknode greennode rednode pinknode yellownode

    whitenode $ docker service update whoami --replicas 10 $ docker service update busybox --replicas 10
  32. Docker Swarm demo: Ansible playbooks PLAYBOOK blacknode greennode rednode pinknode

    yellownode whitenode whitenodeTeardown.yml: - Remove the white node - Leave the cluster
  33. Docker Swarm demo: Ansible playbooks PLAYBOOK blacknode greennode rednode pinknode

    yellownode whitenode whitenodeTeardown.yml: - Remove the white node - Leave the cluster
  34. Docker Swarm deployment: Ansible playbooks PLAYBOOK blacknode greennode rednode pinknode

    yellownode whitenode whitenodeSetup.yml: - Add the white node - Get the worker token
  35. Docker Swarm deployment: Ansible playbooks PLAYBOOK blacknode greennode rednode pinknode

    yellownode whitenode whitenodeSetup.yml: - Add the white node - Get the worker token
  36. Docker Swarm: Add replicas blacknode greennode rednode pinknode yellownode whitenode

    $ docker service update whoami --replicas 18 $ docker service update busybox --replicas 18
  37. Addressing the GPIO pins via bash Bash Script Control of

    GPIO Ports The Pi's GPIO ports can be controlled from the command line (i.e. bash), python scripts, and C/C++ programs. There are 17 GPIO ports available on the Pi. Some of them have special purposes or special hardware configurations and should be avoided for normal use. Source: http://raspberrypi-aa.github.io/session2/bash.html
  38. Bash command Bash commando for showing the different whoami instances:

    for i in {1..20}; do curl http://192.168.178.32:8082;done
  39. Ansible playbooks 1/3 Startup sequence: Initialize the GPIO files: $

    ansible-playbook -i inventory gpioSetup.yml Set up the swarm cluster: $ ansible-playbook -i inventory swarmSetup.yml Start the services: $ ansible-playbook -i inventory servicesSetup.yml
  40. Ansible playbooks 2/3 Swarm playtime: $ ssh [email protected] $ docker

    service update whoami --replicas 10 $ docker service update busybox --replicas 10 Remove the white node from the cluster: $ ansible-playbook -i inventory whitenodeTeardown.yml Add the white node to the cluster: $ ansible-playbook -i inventory whitenodeSetup.yml Add extra replicas: $ docker service update whoami --replicas 12 $ docker service update busybox --replicas 12
  41. Ansible playbooks 3/3 Teardown sequence: Shutdown the services: $ ansible-playbook

    -i inventory servicesTeardown.yml Teardown the cluster: $ ansible-playbook -i inventory swarmTeardown.yml Remove the GPIO files (optional): $ ansible-playbook -i inventory gpioTeardown.yml Shutdown the Raspberry Pi stack: $ ansible-playbook -i inventory shutdown.yml
  42. Find a headless Raspberry Pi b8:27:eb is the Mac address

    signature of a Raspberry Pi Use arp On the command line run: $ arp -a | grep b8:27:eb | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' or just $ arp -a | grep b8:27:eb Use fing Download and install fing from the Fing website [https://www.fing.io/download-free- ip-scanner-for-desktop-linux-windows-and-osx/] Fing needs sudo-rights to run. Router Log in to your router and look up the IP-address assigned to the Pi.
  43. Useful links: Hypriot Blog: http://blog.hypriot.com/ Swarm mode key concepts: https://docs.docker.com/engine/swarm/key-concepts/

    Swarm mode overview: https://docs.docker.com/engine/swarm/ SSH Keys, How-To: https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2 Raspberry Pi Dramble wiki: http://www.pidramble.com/wiki Tooling: Flash tool: https://github.com/hypriot/flash Swarm visualization (ARM): https://github.com/manomarks/docker-swarm-visualizer Homebrew: http://brew.sh
  44. Raspberry Pi Stack costs: *Prices are rounded up, shipping costs

    left out. Parts Price (€)* Supplier Switch 9 Amazon (.de) Patch cables 9 Amazon (.de) Anker PowerPort 40 Amazon (.de) Micro-SD cards (x6) 30 Norrod Dragan box 10 Ikea Raspberry Pi (x6) 260 Okaphone Electronics 16 Okaphone PVC “spacer bolts” 25 Okaphone Total € 399 The costs above are without the failed attempts and many nightly hours