Giter Club home page Giter Club logo

prefect-docker-compose's Introduction

Prefect - Docker Compose

A simple guide to understand and make Prefect work with your own docker-compose configuration.

This allows you to package your Prefect instance for Kubernetes or offline use.

Operating principle of Prefect

Run the server

Open and edit the server/.env file.
All PREFECT_SERVER_* options are explained in the official documentation and listed in the config.toml file.

Then you can run :

docker-compose -f server/docker-compose.yml up -d

Insert the following content in file ~/.prefect/config.toml :

# ~/.prefect/config.toml
debug = true

# base configuration directory (typically you won't change this!)
home_dir = "~/.prefect"

backend = "server"

[server]
host = "http://172.17.0.1"
port = "4200"
host_port = "4200"
endpoint = "${server.host}:${server.port}"

Finally, we need to create a tenant. Execute on your host :

pip3 install prefect
prefect backend server
prefect server create-tenant --name default --slug default

Access the UI at localhost:8080

Run one or multiple agents

Agents are services that run your scheduled flows.

Open and edit the agent/config.toml file.

ℹ️ In each config.toml, you will find the 172.17.0.1 IP address. This is the IP of the Docker daemon on which are exposed all exposed ports of your containers. This allows containers launched from different docker-compose networks to communicate. Change it if yours is different (check your daemon IP by typing ip a | grep docker0).

Docker interface IP

Here, mine is 192.168.254.1 but the default is generally to 172.17.0.1.

Then you can run :

docker-compose -f agent/docker-compose.yml up -d

ℹ️ You can run the agent on another machine than the one with the Prefect server. Edit the agent/config.toml file for that.

Maybe you want to instanciate multiple agents automatically ?

docker-compose -f agent/docker-compose.yml up -d --scale agent=3 agent

Run your first flow via the Prefect API

Principles to understand

💬 Execution in your cloud; orchestration in ours

This means the Prefect server never stores your code. It just orchestrates the running (optionally the scheduling) of it.

  1. When coding a flow, you need first to register it to the Prefect server through a script. In that script, you may ask the server to run your flow 3 times a day, for example.

  2. Your code never lies on the Prefect server : this means the code has to be stored somewhere accessible to the agents in order to be executed.

    Prefect has a lot of storage options but the most famous are : Local, S3 and Docker.

    • Local : saves the flows to be run on disk. So the volume where you save the flows must be shared among your client and your agent(s). Requires your agent to have the same environment than your client (Python modules, packages installed etc... (the same Dockerfile if your agent and client are containers))
    • S3 : similar to local, but saves the flows to be run in S3 objects.
    • Docker : saves the flows to be run as Docker images to your Docker Registry so your agents can easily run the code.

Flow with Local storage (easiest)

ℹ️ If your agents are installed among multiple machines, I recommend you to mount a shared directory with SSHFS.

Open the client/config.toml file and edit the IP to match your Prefect instance. Then you can run :

docker-compose -f client/docker-compose.yml up # Executes weather.py

Now your flow is registered. You can access the UI to run it.

Flow with S3 Storage

⚠️ I don't recommend this method if you plan to schedule a lot of flows every minute. MinIO times out regurarly in that case (maybe AWS wouldn't).

Tutorial for S3 Storage

We will use MinIO as our S3 server.

docker-compose -f client_s3/docker-compose.yml up -d minio # Starts MinIO
  1. Go to localhost:9000 create a new bucket named prefect by clicking the red (+) button bottom right.

  2. Open the client/config.toml file and edit the IP to match your Prefect instance and S3 server endpoint. Then you can run :

docker-compose -f client_s3/docker-compose.yml up weather # Executes weather.py

Now your flow is registered. You can access the UI to run it.

Flow with Docker storage

This method requires our client AND agent containers to have access to Docker so they can package or load the image in which the flow will be executed. We use Docker in Docker for that.

Tutorial for (secure) Docker Storage

Preparing the Registry

A Docker Registry is needed in order to save images that are going to be used by our agents.

  1. Open the client_docker/config.toml client_docker/docker-compose.yml files and edit the IP to match your Prefect instance.

  2. Generate the authentication credentials for our registry

sudo apt install apache2-utils # required to generate basic_auth credentials
cd client_docker/registry/auth && htpasswd -B -c .htpasswd myusername && cd -

To add more users, re-run the previous command without the -c option

  1. Start the registry
docker-compose -f client_docker/docker-compose.yml up -d registry
  1. Login to the registry

You need to allow your Docker daemon to push to this registry. Insert this in your /etc/docker/daemon.json (create if needed) :

{
  "insecure-registries": ["172.17.0.1:5000"]
}

Then, run :

docker login http://172.17.0.1:5000 # with myusername and the password you typed

You should see : Login Succeeded

Start the Docker in Docker agents

Edit registry credentials in ./agent_docker/docker-compose.yml and run :

docker-compose -f agent_docker/docker-compose.yml up -d

Registering the flow

We're going to push our Docker image with Python dependencies and register our flow.

  1. Build, tag and push the image
docker build . -f ./client_docker/execution.Dockerfile -t 172.17.0.1:5000/weather/base_image

You must prefix your image by the registry URI 172.17.0.1

docker push 172.17.0.1:5000/weather/base_image
  1. Register the flow

Edit registry credentials in ./client_docker/docker-compose.yml and run :

docker-compose -f ./client_docker/docker-compose.yml up weather

Now your flow is registered. You can access the UI to run it.

prefect-docker-compose's People

Contributors

flavienbwk avatar mikebirdgeneau avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.