Search
⌃K

Env Sync Tutorial with Python

This tutorial will walk you through the process of enabling Velocity's Env Sync feature for a Velocity Service written in Python.

Prerequisites:

NOTE: If you are not using the Velocity Operator, contact Velocity to enable a container registry that is local to your cluster.

1. Install rsync locally

macOS
Debian
CentOS
brew install rsync
apt-get install rsync
yum install rsync

2. Clone the example repo

git clone https://github.com/techvelocity/velocity-blueprints.git
cd velocity-blueprints/examples/env-sync-python/src/web_api

3. Create a .velocity.yaml config

Create the file /src/web_api/.velocity.yaml and copy the following configurations into it:
version: 1
services:
web-api:
sync:
live_updates:
- .:/app
build_and_push:
velocity_builder:
args: [-f ./Dockerfile .]
You can learn more about the .velocity.yaml config here.
Your file tree should now look like this:
.
├── k8s
│   ├── Chart.yaml
│   ├── templates
│   │   └── web_api.yml
│   └── velocity-values.yml
└── src
   └── web_api
   ├── Dockerfile
   ├── main.py
├── .velocity.yaml
   └── requirements.txt

4. Create your Velocity Environment

From the k8s directory, run:
helm template . --values velocity-values.yml | veloctl env create -f -
This command creates an environment from a local Helm template that has not been committed to Velocity.

5. Add rsync to your Dockerfile

Update /src/web_api/Dockerfile as follows:
FROM python:3.10-alpine
COPY requirements.txt .
RUN pip install -r requirements.txt
RUN apk add rsync # <----- Add this line
COPY main.py .
CMD ["sh", "-c", "uvicorn main:app --host 0.0.0.0 --port 8000 --reload"]
NOTE: For FastAPI apps, pass the --reload flag to enable hot reload.

6. Start env sync

Run the following from /src/web_api:
veloctl env sync -s web-api

7. Change the source code

Add the following to /src/web_api/main.py:
@app.get('/test')
async def test():
return 'success'

8. View the result

Navigate in your web browser to https://<your-env-domain>/test
Congratulations! Env Sync is set up and working!

Next Steps: