Телевизоры. Приставки. Проекторы и аксессуары. Технологии. Цифровое ТВ

Кластер из raspberry pi web сервер. Параллельные вычисления на raspberry pi. Почему Cluster HAT


Плата Cluster HAT является решением проблемы построения кластерных вычислений. Распределенные вычисления сложны и этот крошечный аппаратный комплект является одним из решением данной проблемы.

Не смотря на то, что создание не такое и простое, это представляет собой один из самых впечатляющих проектов Raspberry Pi.

Почему Cluster HAT?

Плата Cluster HAT (Hardware Attached on Top) взаимодействует с (контроллером) Raspberry Pi A+ / B+ / 2 / 3 / 4 и четырьмя платами Raspberry Pi Zero. Она настроена на использование режима USB Gadget. Кроме того, это идеальный инструмент для обучения, тестирования или моделирования небольших кластеров.

Cluster HAT использует гибкость Raspberry Pi, позволяя программистам экспериментировать с кластерными вычислениями.

Важно отметить, что HAT не поставляется с платой Raspberry Pi или Pi Zero. Обе платы нужно будет приобретать отдельно. Производитель Pimoroni предоставляет инструкции по сборке и управлению на своей странице продукта . Также компания утверждает, что существует 3 способа настройки платы HAT.

Технические характеристики Cluster HAT

  • HAT может быть использована с любым из модулей Pi Zero 1.2, Pi Zero 1.3 и Pi Zero W.
  • Режим USB Gadget: Ethernet и последовательная консоль.
  • Встроенный 4-портовый хаб USB 2.0.
  • Питание Raspberry Pi Zeros осуществляет через контроллер Pi GPIO (USB дополнительно).
  • Питание Raspberry Pi Zero контролируется через контроллер Pi GPIO (I2C).
  • Разъем для контроллер последовательной консоли (FTDI Basic).
  • Контроллер Pi может быть перезагружен без прерывания питания для Pi Zeros (восстановление сети при загрузке).

Комплект включает в себя:

  • Комплект крепления HAT (стойки и винты)
  • Короткий USB провод (цвет может отличаться)

В заключении

Плата Cluster HAT v2.3 сейчас доступна для покупки и хотя ее пока нет в наличии у

A 4-node Raspberry Pi Cluster. The top board is an original Model B, while the three below are brand new Raspberry Pi 2 boards.

Updated: This project was originally published on 26th Aug 2015 and was then updated on the 5th Sept 2015 with additional instructions on how to add a second Ethernet adaptor to the head node, and have it serve as a DHCP server for the other nodes in the cluster.

Over the weekend I sat down and built a small Raspberry Pi cluster consisting of 4 nodes. I used three boards for compute nodes and an for the head node. I wanted the cluster - more commonly known as a ‘bramble’ - to be as compact as possible, with just two cables coming out, one for power and the other for network. I made use of a USB hub to power the boards, and a small Ethernet switch which I could hack to be also be powered from the USB hub rather from a separate wall wart.

It’s hardly the biggest cluster built from Raspberry Pi boards, as far as I know the 120 Pi cluster built by the folks at Resin.io is still the biggest built, although since it’s actually 5 independent 24-node clusters, possibly the still takes the title.

However, even with just 4 nodes my pocket cluster is big enough for what I want, which is as a testbed for some distributed computing work I’m doing. The small cluster sitting on my desk lets me test code out before deploying jobs to the much more extensive, and expensive, cluster I’m using for grunt work on the project.

The 4 board ‘dogbone’ enclosure

The enclosure I finally settled on was a four board stackable ‘dog bone’ case that I picked up on Amazon , although if you’re willing to wait a little bit there are plenty of similar cases on AliExpress that can be had for much less. It shipped overnight and I had it the next day; it was the only thing I bought to build the cluster as I had everything else on the shelf.

The 5-port USB Hub

The USB hub I used was the thing that actually inspired me to do the build in the first place: It’s a 5-port hub from Anker and has coincidentally about the same footprint as the Raspberry Pi itself. With five ports, there’s one port for each of my four Raspberry Pi boards, and a final port left over to power an Ethernet switch for the cluster.

The 5V power supply and a spare USB cable

The first step is to carefully snip off the end of the 5V supply cable, making sure to label which of the two wires corresponded to the two wires left attached to the power brick. Stripping off the ends of the wires you can plug the brick into the wall and use a volt meter to measure which of the two wires is +5V and which is GND.

The 5V supply cable (top) and the USB cable end (bottom)

Then snip off the end of the USB cable and carefully, as the wires inside the cable are small and delicate, strip back the cover to reveal the wires. You’re looking for the red and black wires, the others carry data. You can just cut them off, you won’t need them.

The internal wiring of a USB cable

Soldering the two end of the cables together - joining the +5V to +5V, and the GND to GND - and then covering each individual wire, as well as the join itself, with some shrink wrap gives me the Frankenstein cable I need to power the Ethernet switch from the last available port of my USB hub.

The Frankenstein cable

After searching through my stack of spare cables to find the shortest USB and Ethernet cables possible, sticking the cluster together at this point came down to cable ties and velcro.

The finished Raspberry Pi cluster

% sudo apt-get install autofs

and then edit the /etc/auto.master file adding

/mnt/nfs /etc/auto.nfs

at the end. Then create the /etc/auto.nfs file, adding,

Rpi0 rpi0:/mnt/usb

and restart the autofs service,

% sudo /etc/init.d/autofs restart.

if all goes well at this point if you change to the /mnt/nfs/rpi0/ directory and the disk attached to the head node should automatically mount itself. You can check,

% df -h Filesystem 1K-blocksUsed Available Use% Mounted on rootfs14984668 25132281181235618% / /dev/root 14984668 25132281181235618% / devtmpfs470416 0470416 0% /dev tmpfs94944 232 94712 1% /run tmpfs 5120 05120 0% /run/lock tmpfs 189880 0189880 0% /run/shm /dev/mmcblk0p1 57288 19448 3784034% /boot rpi0:/mnt/usb 604670086460466944 1% /mnt/nfs/rpi0

to see whether it has been automatically mounted.

Blinking Lights

Alongside the USB flash drive (since I had one lying around) I installed a Blinkstick. A single software-controllable RGB LED, the stick actually comes in rather handy for server status light. It’s hard to ignore a blinking light. After slotting the stick into the head node’s last remaining USB port, you can set up the software by,

% sudo apt-get install -y python-pip python2.7-dev % sudo pip install blinkstick % sudo blinkstick --add-udev-rule

from there it’s actually pretty easy to manipulate the RGB LED from the command line.

Or when you want to use the Blinkstick programmatically to indicate status you can use the API, and your programming language of choice .

Next Steps

I travel a lot. That means I spend a lot of time away from my home office. While I can leave the cluster up and running and just ssh into it while I’m away, I’d actually sort of like to be able to take it on the road with me to shows. So, going forward, I’d really like just to be able to pick the cluster up and dump it down on any network.

That means I’m going to have to reconfigure the networking just a little bit.

Instead of directly connecting the Ethernet switch to the external network, and having my home router allocate IP addresses for each of the nodes, as a next step I’m going to add a USB Ethernet adaptor to the head node. This will give the head node two Ethernet connections.

The first will connect to the external network, giving the head node - and hence the cluster - an ‘external’ IP address. The second will connect to the cluster’s Ethernet switch. We can then configure the head node as a DHCP server for other three ‘internal’ nodes attached to the switch, creating a second network visible only to the cluster.

In this configuration I’ll still be able to ssh into the head node, but I’ll only be able to reach the three compute nodes from the head node. There is a problem however: How will I know the external IP address of the head node?

Adding an LCD

The Blinkstick is good for simple messaging, you can actually do a lot with an RGB LED to let yourself know what’s going odd. But it’s actually pretty easy to add a simple LCD display to the head node.

After connecting the panel you’ll need to install the I2C tools and associated Python libraries

% sudo apt-get install python-smbus % sudo apt-get install i2c-tools

and to enable I2C on the head node by adding the following at the bottom of the /boot/config file,

device_tree= dtparam=spi=on dtparam=i2c1=on dtoverlay=w1-gpio-pullup,gpiopin=3,pullup=3 dtoverlay=w1-gpio-pullup,gpiopin=5,pullup=5

and adding the following modules to the /etc/modules file,

I2c_dev i2c_bcm2708

After rebooting the head node you should be able to see the panel with an I2C ID of 27,

% sudo i2cdetect -y 1 0123456789abcdef 00:-- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- UU -- -- -- -- 20: -- -- -- -- -- -- -- 27 -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- UU -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- --

You can see that eth0 has the static internal IP address we allocated to it, while eth1 has a new IP address allocated by our home router. If all goes to plan you should be able to ssh into the head node using its new external IP address, and see something like this,

% ifconfig eth0Link encap:EthernetHWaddr b8:27:eb:22:60:fb inet addr:192.168.50.1Bcast:192.168.50.255Mask:255.255.255.0 RX packets:2470 errors:0 dropped:0 overruns:0 frame:0 TX packets:2267 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:215730 (210.6 KiB)TX bytes:237032 (231.4 KiB) eth1Link encap:EthernetHWaddr ac:29:3a:da:74:37 inet addr:192.168.1.194Bcast:192.168.1.255Mask:255.255.255.0 UP BROADCAST RUNNING MULTICASTMTU:1500Metric:1 RX packets:15245 errors:0 dropped:1 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:1787746 (1.7 MiB)TX bytes:283761 (277.1 KiB) loLink encap:Local Loopback inet addr:127.0.0.1Mask:255.0.0.0 UP LOOPBACK RUNNINGMTU:65536Metric:1 RX packets:4 errors:0 dropped:0 overruns:0 frame:0 TX packets:4 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:260 (260.0 B)TX bytes:260 (260.0 B)

% route -n Kernel IP routing table Destination Gateway Genmask Flags Metric RefUse Iface 0.0.0.0 192.168.1.254 0.0.0.0 UG000 eth1 192.168.1.0 0.0.0.0 255.255.255.0 U 000 eth1 192.168.50.00.0.0.0 255.255.255.0 U 000 eth0

If not everything goes to plan and you’re stuck unable to reach the head node over the network, it’s possible that you might have to dig out a HDMI monitor and a USB keyboard and connect them directly to the head node - you can temporarily yank the USB disk to give yourself and free USB port for the keyboard - so you can diagnose and fix any networking issues.

Hopefully however you can reach the head node from the external network. You should be able to ping both external hosts on the 192.168.1.* network, and internal hosts on the 192.168.50.* network.

However, at least right now, if we ssh into one of the compute nodes, while they can see the head node - and each other - they can’t yet see the outside world. We’re going to have to forward packets from the internal to the external networks before that’s possible.

On the head node go ahead and,

% sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward "

and then edit the /etc/sysctl.conf file uncommenting the line saying,

Net.ipv4.ip_forward=1

After activating forwarding we’ll need to configure iptables ,

% sudo iptables - t nat - A POSTROUTING - o eth1 - j MASQUERADE % sudo iptables - A FORWARD - i eth1 - o eth0 - m state -- state RELATED , ESTABLISHED - j ACCEPT % sudo iptables - A FORWARD - i eth0 - o eth1 - j ACCEPT % sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

and then add at the bottom of the /etc/network/interfaces file a line to load the tables on boot,

Up iptables-restore < /etc/iptables.ipv4.nat

Rebooting the head node at this point, you should now be able to ssh into any of the compute nodes from the head node and be able to ping the outside world,

% ssh rpi1 Linux rpi2 3.18.11-v7+ #781 SMP PREEMPT Tue Apr 21 18:07:59 BST 2015 armv7l The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Sat Sep5 20:49:07 2015 from rpi0 % ping 8.8.8.8 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 64 bytes from 8.8.8.8: icmp_req=1 ttl=54 time=23.8 ms 64 bytes from 8.8.8.8: icmp_req=2 ttl=54 time=21.4 ms 64 bytes from 8.8.8.8: icmp_req=3 ttl=54 time=23.2 ms ^C --- 8.8.8.8 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 21.470/22.838/23.829/1.014 ms %

That’s it. We have a working cluster.

In Closing

At this point we have a cluster with two cables going into it, one for power and the other for network. You can plug into any network and the head node will report its external IP address on an LCD panel, allowing you to ssh into it, and from there you can ssh into - and between - any of the nodes in the cluster without needing a password. All the nodes also share a disk.

In other words, it’s all pretty much all working at this point. In fact, I’m currently using it as a desktop Hadoop cluster .

From here there are a couple of things we could do, the most obvious next step would be to add some SNMP monitoring, and an external facing ‘status’ dashboard on the head node to monitor the cluster health . However in the longer term, the free Ethernet port on the switch means that we can expand the cluster fairly easily by adding another rack of four compute nodes without too much extra effort.

Введение

Основополагающими затратами в области вычислений являются мощности компьютеров и их энергопотребление. Современные суперкомпьютеры занимают огромное пространство и потребляют сотни тысяч ватт.

Большой проблемой в этом случае является процесс обучения параллельному программированию и выполнению расчетов на суперкомпьютерах и тем более – управлению такого типа компьютерами, поскольку непосредственного доступа к ним у обучающихся, как правило, нет.

В данной работе предлагается для решения данной проблемы использовать одноплатные микрокомпьютеры, недавно появившиеся на рынке компьютерной техники (Paspberry Pi и аналоги). На их основе можно собрать недорогой вычислительный кластер и обучать студентов основам параллельного программирования. Таким образом, цель этой работы – создание недорогого учебного кластера из микрокомпьютеров для разработки и внедрения в учебный процесс алгоритмов параллельного программирования. Демонстрируется пример параллельных вычислений в разработанном учебном кластере.

Raspberry Pi – микрокомпьютер разработанный компаний Raspberry Pi Foundation. Маленький, размером с банковскую карту, он представляет собой полноценный одноплатный компьютер (System - on - a - Chip ). Процессор (в модели PI 3): 4 ядра ARM Cortex-A53 x64 . Операционная система по умолчанию Raspberian (основанная на Linux ядре). При цене всего в 35$ у платы есть все нужные интерфейсы (Wi - Fi , Bluetooth , Usb , Ethernet ), а также большой набор готовых программ для любого вида деятельности . Именно поэтому для небольшого учебного вычислительного кластера были выбраны эти микрокомпьютеры.

Понятие кластера и кластерных вычислений

Общеизвестно, что кластер – это группа компьютеров, объединённых высокоскоростными каналами связи, представляющая с точки зрения пользователя единый аппаратный ресурс . С другой стороны, кластер – это слабо связанная совокупность нескольких вычислительных систем, работающих совместно для выполнения общих программных приложений . Для того чтобы связать несколько raspberry PI в кластер, была собрана типовая кластерная вычислительная система (маршрутизатор, кабели Ethernet, USB и др.) на базе процессора PI 3 (рис. 1).

Рисунок 1. Кластерная вычислительная система на базе процессора из двух PI 3

Демонстрация параллельных вычислений

Для наглядной демонстрации возможностей кластера из двух PI 3 была выбрана среда программирования Python 2 и реализация алгоритма сортировка массива методом слияния. Компьютеры были объединены локальной сетью. Для упрощения составления кластера из нескольких R PI существует много готовых программ, одна из которых называется “mpi4py” .

Программный код сортировки массива слиянием на языке Python выглядит следующим образом:

def merge(left,right): #merges 2 sorted lists together

#Goes through both lists

while i < len(left)and j < len(right):

#Adds smaller element of the lists to the final list

if left[i] <= right[j]:

result.append(left[i])

result.append(right[j])

result += left

result += right

def mergesort(lst):

#if there"s only 1 element, no need to sort

if len(lst)< 2:

#breaks down list into 2 halves

middle = len(lst)/ 2

#recursively splits and sorts each half

left = mergesort(lst[:middle])

right = mergesort(lst)

#merges both sorted lists together

return merge(left, right)

Алгоритм работы программы состоит из следующей последовательности действий:

1. На PI 3 (сервер) генерируется случайный массив чисел.

2. Данный массив разбивается на n частей, по количеству процессоров в локальной сети.

3. С помощью модуля socket и локальной сети Pi3 (сервер) передает часть массива Pi3 (клиент).

4. Pi3 (сервер) сортирует свою часть массива и ждет ответа Pi3 (клиент).

5. Pi3 (клиент) сортирует свою часть массива и передает ее Pi3 (сервер).

6. Pi3 (сервер) получает отсортированную часть массива и выполняет конечную сортировку.

Расчеты показали, что для сортировки массива из 500 тысяч элементов одному Pi3 потребовалось около 23 секунд. После добавления второго Pi3 это время уменьшилось до 16 секунд. Прирост скорости нелинейный, но чем больше будет в кластере компьютеров, тем меньше будет затрачиваемое время.

Заключение

Одноплатные компьютеры лишь недавно вышли за пределы сегмента устройств для автоматизации производства и начали завоевывать массовый рынок. Их небольшие размеры, небольшое энергопотребление и достаточно высокие вычислительные возможности способны сделать их основой для реализации различных проектов, например, обучение параллельному программированию. Особенностью, представленной кластерной вычислительной системы на базе raspberry PI 3, является хорошая масштабируемость, определяемая возможностями коммутационного оборудования, невысокая стоимость, возможность применения бесплатно распространяемого программного обеспечения, что важно при внедрении в учебный процесс. Проведенная демонстрационная работа показывает, что кластер даже из двух PI 3 способен ускорить вычисление простой, но одновременно и объемной задачи, такой как сортировка большого массива данных.

В перспективе планируется увеличить в вычислительной системе число микрокомпьютеров и провести сравнение производительности криптографических алгоритмов, в частности, планирующихся использовать для шифрования/расшифрования изображений (фото-, аеро-, космо- изображений) большого объема и передаче их по сети Интернет.

Список литературы :

  1. Robert Mullins/ Distributed computed //University Cambridge. – 2012. – http://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/distributed-computing/ .
  2. Кластер. – Режим доступа. – URL: https://ru.wikipedia.org/wiki/Кластер (дата обращения 25.02.2017).
  3. Лукин В.В., Марчевский И.К. Учебно-экспериментальный вычислительный кластер. Ч. 1. Инструментарий и возможности. – Режим доступа. – URL: https://elibrary.ru/download/elibrary_17091004_33209664.pdf (дата обращения 25.02.2017).

Вполне возможно, что это самый дешёвый и доступный кластер, построенный в домашних условиях.
В данный момент он считает задачки seti@home.

Сборка

Сборка не составляет особого труда - вот список материалов для повторения:

  • 4 платы OrangePi PC (One тоже подойдет) с кабелями питания
  • 16 стоек для печатных плат для крепления между собой
  • 4 стойки (коротких) для крепления на подставку или использования в качестве ножек
  • 2 куска оргстекла (верхняя и нижняя крышка)
  • Вентилятор 92 мм
  • 4 уголка для крепления вентилятора
  • 100Mbs Ethernet HUB, желательно с питанием либо 5, либо 12 вольт
  • Патчкорды для соединения Ethernet в необходимом количестве (кстати, поскольку сеть все равно 100 МБит, можно использовать 4х-жильную телефонную лапшу и немного сэкономить на кабеле)
  • Источник питания (об этом позже)
  • Для связи с внешним миром - дешёвый USB WiFi

Скручиваем вместе четыре платы, крепим верхнюю и нижнюю крышки, ставим вентилятор с использованием уголков. На верхнюю крышку водружаем хаб и соединяем наш всё вместе через Ethernet.

А вот так “изделие” выглядит “с тыльной стороны”.

К сожалению, синей изоленты не было - так что хаб крепим резиночками.

Питание

Каждая из OPI потребляет не меньше ампера (производитель рекомендует источник не меньше 1.5…2A). Вентилятор требует 12 вольт, хаб тоже, хотя бывают и 5-вольтовые модели.

Так что потребуется хороший источник питания с двумя напряжениями.

Старый компьютерный вполне подойдет, но лучше использовать современный безвентиляторный импульсный источник, например от MeanWell.

Я, собственно, так и поступил, упаковав его в корпус от винтажного блока питания и выведя наружу обычный молекс-разъем (как на компьютере).

Для “раздачи” 5ти вольт будем использовать модифицированный USB-хаб из дешевых. Для этого можно либо высверлить чип, либо просто отрезать ножки данных, оставив только цепи питания и землю. Я остановился на втором способе, правда внутри проложил еще “толстые” соединения на линии 5В. Ну и повесим ответный molex для соединения с БП. Получается примерно так:

А вот вся конструкция в сборе:

Система

Вообще, это просто “маленькая локальная сеть из 4х компьютеров”.
В качестве базовой системы - обычный Debian, о котором уже много говорили .

Сеть

Самый верхний узел - clunode0, он умеет соединяться по WiFi с внешней сетью, при этом раздает “интернет” на машины clunode1, clunode2, clunode3. Там же работает сервер NFS для общего хранилища и dnsmasq для раздачи DHCP адресов вида 10.x.x.x.

На clunode0 в /etc/network/interfaces примерно такая запись:

1
2
3
4
5
6
7
8
9
10
11
12
13
auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-scan-ssid 1
wpa-ap-scan 1
wpa-key-mgmt WPA-PSK
wpa-proto RSN WPA
wpa-pairwise CCMP TKIP
wpa-group CCMP TKIP
wpa-ssid "MyWiFi"
wpa-psk "MyWiFiPassword"
post-up /usr/local/bin/masquerade.sh eth0 wlan0
iface default inet dhcp

Хотя, вроде как там ситуация переломилась и бинарник можно сгрузить с сайта. Не проверял - проще было собрать самому.

Еще можно установить и настроить консольную утилиту boinctui . Выглядит всё вполне пристойно (animated GIF):

Перспективы

Можно развить идею - вот навскидку несколько идеек:

  • Первая плата (clunode0) - load balancer, сlunode2,3 - веб-сервера или приложение, clunode4 - БД ==> микродатацентр:)
  • Hadoop (и такие случаи уже есть, народ строит кластеры на Raspberry)
  • Proxmox кластер, правда я не уверен, что все запчасти доступны для ARM
  • Майнер cryptocurrency, если конечно подберете криптовалюту, которую всё еще выгодно майнить на процессоре и выгодно майнить вообще.

Спасибо, что дочитали до конца.



Похожие публикации