<@ULVA73B9P> when Prefect is deployed on one Linux...
# marvin-in-the-wild
d
@Marvin when Prefect is deployed on one Linux server, is it possible to launch flow on another Linux server, e.g. to run specified docker container there?
m
To run a Prefect flow on a different server, including running a specified Docker container, you'll need to: 1. Deploy the Flow to Prefect Cloud: To allow the flow to be orchestrated from anywhere. 2. Use Work Pools/Workers: Configure the workers to run on different servers, targeted using work pools. ### Step-by-Step Guide 1. Deploy Your Flow: You can deploy your Prefect flow using the
deploy()
method:
Copy code
python
from prefect import flow
from prefect.deployments import Deployment

@flow
def my_flow():
    print("Running my flow")

if __name__ == "__main__":
    Deployment(name="my_deployment", flow=my_flow).apply()
2. Setup Work Pools in Prefect Cloud: Configure work pools on the servers where the flow should run. This can be done in the Prefect Cloud UI under the "Work Pools" section. 3. Run Workers: On the target server where you want to run the flow, start a Prefect worker from the specified work pool:
Copy code
sh
prefect worker start --pool your_work_pool_name
4. Specify Docker Container: If you need the flow to run in a specific Docker container, specify the image in your deployment configuration:
Copy code
python
Deployment(name="my_deployment", flow=my_flow, work_pool_name="your_work_pool_name", image="your-docker-image").apply()
### Links for Reference: - Deployment Configuration - Work Pools This setup will ensure your flow can be orchestrated by Prefect but executed on another server, potentially using a specified Docker container.
d
@Marvin consider only on-premises Prefect deployment, don't consider Prefect Cloud.
m
For on-premises Prefect deployments, you can configure the setup to run flows on different servers by following these steps: 1. Deploy the Flow: Use the deployment capabilities of Prefect. 2. Configure Workers on Target Servers: Ensure you have Prefect workers running on the servers where you want the flows to execute. Here is a detailed approach: ### 1. Deploy the Flow First, create a deployment for your flow. This is similar to the previous example but ensuring it is meant for on-premises:
Copy code
python
from prefect import flow
from prefect.deployments import Deployment

@flow
def my_flow():
    print("Running my flow")

if __name__ == "__main__":
    Deployment(name="my_local_deployment", flow=my_flow).apply()
### 2. Start Workers on Target Servers On the server where you want to run the flow, start a Prefect worker:
Copy code
sh
prefect worker start --pool work-pool-name
This command ensures that the worker is listening for tasks assigned to it from the specified work pool. ### 3. Specify Docker Container (If Needed) If the execution needs to be within a Docker container, modify the deployment to include the Docker image:
Copy code
python
Deployment(
    name="my_local_docker_deployment",
    flow=my_flow,
    work_pool_name="your_work_pool_name",
    image="your-docker-image"
).apply()
### Example Code for Reference Here is some example code to get you started on running tasks remotely, which can be configured similarly for deploying and running flows: LINK: https://raw.githubusercontent.com/zzstoatzz/prefect-code-examples/main/examples/tasks/submitting.py EXAMPLE:
Copy code
python
import time
from prefect import flow, task

@task
def sleep(t: int):
    print(f"Going to sleep for {t} seconds...")
    time.sleep(t)
    print("...and I'm awake!")

@flow(log_prints=True)
def my_flow():
    sleep(3)
    future = sleep.submit(2)
    future.wait()
    sleep.map([5] * 10)

if __name__ == "__main__":
    my_flow()
### Additional Resources - Deployments Documentation - Workers Documentation By following these steps, you can configure Prefect in an on-premises environment to deploy and run flows on different servers.
👍 1
🙌 1
k
The Prefect server and workers are independent from each other, so as long as the worker can reach the server for requesting work over the network, the answer is yes
👍 1
the worker will need an API url
d
OK, thanks, now I understand that it's enough to have one Prefect server (UI) on one machine, and Prefect workers on all machines where flows are needed to run. Though I don't yet understand how to communicate with workers on other machines (does every worker have its API url?...), but I will check it later...
k
the API url is the server, so each worker will use the same one to find out from the server when there are flows to run
d
Oh, you mean that worker needs to see environment variable with API url to server?
k
correct!
🤝 1