For an upcoming project I want to use microservices on my Raspberry Pi. This means multiple process will take care of individual tasks. To coordinate these processes they need to communicate with each other. A great way to implement this is using a Redis server. Redis is an open source (BSD licensed), in-memory data structure store and can be used as a database, cache and message broker. Since version 4.0 Redis supports ARM processors and gets tested against the Raspberry Pi.
All microservices on the Raspberry will run as docker containers. Docker provides a method of packaging software to include not only your code, but also other components such as a full file system, system tools, services, and libraries. You can then run the software on multiple machines without a lot of setup. Each microservice is independent and it is very easy to manage or exchange certain services.
The short tutorial assumes you already have a Raspberry Pi setup with Raspbian.
Install Docker
Installing Docker on the Raspberry Pi has become fairly easy lately. You only need to run the following code from your bash.
$ curl -sSL https://get.docker.com | sh
After installing Docker you might want to add the user pi to the docker group. This allows you to run Docker commands without using sudo.
$ sudo usermod -G docker pi
Create Dockerfile
Now we will create our Docker image for the Redis server. We will use the rpi-raspbian image from resin as our base image and add the redis-server to it.
Also we will expose the port 6379 which is the Redis communication port. At the end we will start the redis server with the flag --protected-mode no
. If you
don't use this flag you will not be able to connect to the Redis server from a network interface other then the loopback adapter. If we would install Redis directly
on our host machine this would be OK. However as we run it in a Docker container a connection from the host will be handled like a connection from the outside.
That is why we need to enable this flag here.
FROM resin/rpi-raspbian:stretch RUN apt-get update RUN apt-get install -y redis-server EXPOSE 6379 CMD ["redis-server", "--protected-mode no"]
Build the docker image
Now we can build the image by typing:
$ docker build -t rpi-redis .
This will take a while. When the build is finished we can see the new image in the image list.
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE rpi-redis latest e3212d56205f 3 hours ago 174MB resin/rpi-raspbian stretch 79d4a99c6e95 4 days ago 145MB
Run the image
To run our image in a new container we need the following command.
$ docker run -d -p6379:6379 --restart unless-stopped rpi-redis
The -d
flag runs the container in detached mode as a background service. The -p6379:6379
argument tells docker to publish the port 6379 to our host. The last argument --restart unless-stopped
tells docker to automatically start our container when the Raspberry has been booted.
Test the Redis server
To test our new microservice we will use the Python library python-redis. We install it with pip:
$ sudo pip3 install redis
We will write a string bar
to a variable named foo
. Afterwards we will read the variable foo
and the value should be bar
.
>>> import redis >>> client = redis.Redis() >>> client.set('foo', 'bar') True >>> client.get('foo') b'bar'
Of course this is a very simple example but it shows how redis works. The true power of Redis will be revealed if you use it for inter-process communication.
Links