Deploying Fn on Docker Swarm

In this example, we start an Fn cluster on a Swarm-scoped network.

Starting with deploying a network, we use weaveworks/net-plugin as the backbone network for stability reasons. Please note that the network must be attachable and the subnet must be inside the scope of  10.32.0.0/16. So, 10.32.3.0/24 is just fine here:

$ docker network create 
--driver weaveworks/net-plugin:2.1.3
--attachable
--subnet 10.32.3.0/24
fn_net

Then we prepare a volume for the datastore. As this section also wanted to demonstrate a product-grade setup, we use MySQL as the store rather than the default SQLite3. Using MySQL allows us to horizontally scale the number of Fn Servers.

The volume will be created using the docker volume create command. If we'd like to set up a MySQL cluster, the setups would be a bit more complex than this, but it will not be covered by this book:

$ docker volume create mysql_vol

This is the docker run command to start an instance of MySQL. We just attach the instance to the network fn_net created previously. We specify the network alias here to ensure that the service must be accessible by the name mysql. All environment variables are designed to set up a username, password, and the default database, fn_db. Do not forget to bind the volume, mysql_vol, to /var/lib/mysql inside the container. This is designed to enable the data survive to when the container is removed:

$ docker run 
--detach
--name mysql
--network fn_net
--network-alias mysql
-e MYSQL_DATABASE=fn_db
-e MYSQL_USER=func
-e MYSQL_PASSWORD=funcpass
-e MYSQL_RANDOM_ROOT_PASSWORD=yes
-v mysql_vol:/var/lib/mysql
mysql

The next step is to start the Fn Servers. This section demonstrates how to start two Fn Servers pointing to the same Log Store (MySQL). Each Fn Server attaches to the fn_net. This first instance is named fn_0. An Fn Server requires FN_DB_URL to point to an external Log Store, which may be PostgreSQL or MySQL. Just put the complete URL as shown in the following command. We also call the container fn_0 to make it easier to manage.

When having a setting such as this, the Fn Server becomes completely stateless, where all states will be stored externally to the database. So it is now safe to completely remove the Fn Server containers when things go wrong:

$ docker run --privileged 
--detach
--network fn_net
--network-alias fn_0
--name fn_0
-e "FN_DB_URL=mysql://func:funcpass@tcp(mysql:3306)/fn_db"
fnproject/fnserver

Let's start another one, fn_1. Basically, this should be done on a separate node (physical or virtual):

$ docker run --privileged 
--detach
--network fn_net
--network-alias fn_1
--name fn_1
-e "FN_DB_URL=mysql://func:funcpass@tcp(mysql:3306)/fn_db"
fnproject/fnserver

Well, after setting all Fn Server instances, now it's time to aggregate them. We use Fn LB to act as the load balancer in front of all the Fn Servers. Similar to other containers, we just create and attach it to the fn_net. As it is the FaaS gateway, we also expose its port to 8080 (from its internal port 8081) to make the Fn CLI able to connect to the Fn cluster without any special setting. The network alias is just used when we need other services to connect to this gateway.

Next, send a list of Fn Server nodes as the command line.

Currently, the node list configuration is allowed to pass directly to the container only. Just put them in <name>:<port> format, separated by a comma:

$ docker run --detach 
--network fn_net
--network-alias fnlb
--name fnlb
-p 8080:8081
fnproject/fnlb:latest --nodes fn_0:8080,fn_1:8080

OK, now it's time to verify that everything is up and running. We double-check all containers with the docker ps command:

$ docker ps --format "table {{.ID}}	{{.Names}}	{{.Command}}	{{.Ports}}"
CONTAINER ID NAMES COMMAND PORTS
ce4f8e9bc300 fnlb "./fnlb --nodes fn_0…" 0.0.0.0:8080->8081/tcp
dae4fb892b4d fn_1 "preentry.sh ./fnser…" 2375/tcp
8aefeb9e19ef fn_0 "preentry.sh ./fnser…" 2375/tcp
67bd136c331a mysql "docker-entrypoint.s…" 3306/tcp

In the next two sections, we will cover how to monitor what's happening with Fn UI and how to see and maybe further analyze the logs stored in the database.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset