Skip to main content

Django + Celery

Learn how to deploy your Django + Celery project using Sleakops.

Prerequisites

Let's Start

For this example, we are going to use this project . It is a Django project with Celery that already has Docker configured to run. We are also going to configure a Postgresql database, S3 bucket and Rabbitmq needed for this project.

Create a project

To start, we are going to create a new project. To do this, click the "Projects" button in the left panel:

click-project-reference

Inside the Projects panel you will be able to see all the projects you have and manage them from here. We want to create a new one so let's click on the “create” button at the top right:

click-create-project-reference

In the project creation screen we have the following fields:

SettingDescription
EnvironmentWe have to select the previously created environment.
NodepoolWe will leave the default one.
RepositoriesWe will select our repository that we want to deploy. In our case example-django-celery.
Project NameWe can define a project name. For the example we will leave the default.
BranchIt has to coincide with the one we have in our project. In our case it is “Main”.
Dockerfile pathIt is the relative path to the dockerfile in your project.

Once configured all that we create the project with the “Submit” button at the bottom right:

create-project-reference

With that, the project begins to be created. In the meantime we go to the workloads with the “Workloads” button in the left panel:

click-workloads-reference

Create a Web Service

Here what we are going to do is to create a web service so we go to the web service section and create one:

click-create-web-service-reference

In this page we are going to complete the first form with the following fields:

SettingDescription
ProjectWe select the project we created previously, in our case “example-django-celery”.
NameWe define a name for the web service.
CommandBy default this will take the value that is in the dockerfile, in our case this is fine.
PortThe same as the command.

Then we continue by clicking on the “Next” button up to step 3:

create-web-service-reference

In step 3 we have to edit the path field and put the endpoint of healthcheck which in our case is “/healthcheck/”. Then click on the “Next” button until the web service is created:

create-web-service-2-reference

Deploy celery worker

Well, with this we can see our web service deploying. Now we are going to deploy the celery. For this we have to go to the workers section inside the same workloads screen:

click-worker-reference

And click on the “Create” button to create a new one:

click-create-worker-reference

In the workers creation screen we will have to complete the following fields:

SettingDescription
ProjectSelect the previously created project. In our case “example-django-celery”.
NameWe define the name that we are going to give to the worker. In our case “celery”.
CommandHere we set the command to run celery, in our case it is: bash celery -A core.celery_app worker -l INFO --concurrency 1 --max-tasks-per-child 1 --prefetch-multiplier 1 -n celery@%h --queues default,build,deployment,cluster,canvas,billing

With these fields filled in we will click on the “Next” button at the bottom right and then “Submit” as we do not need to edit anything else:

create-worker-reference

With this we will see our celery published. Now we have to configure the hooks. For this we go to the hooks section:

click-hook-reference

Create a migration hook

In the hook creation screen we will have the following fields:

SettingDescription
ProjectSelect the previously created project. In our case “example-django-celery”.
NameWe define the name that we are going to give to the worker. In our case “migrations”.
CommandHere we set the command to run celery, in our case it is: bash python manage.py migrate --no-input

With these fields filled in we will click on the “Next” button at the bottom right and then “Submit” as we do not need to edit anything else:

create-hook-reference

Create a collect static hook

Now we proceed to create another hook that we need for the statics:

click-create-collectstatic-hook-reference

In this form we are going to do the same as the previous one but modifying the command. We click next until we create the hook (without modifying anything else):

create-collectstatic-hook-reference

The command we use is as follows:

python manage.py collectstatic --no-input

Create a Postgresql Database

Once we have created the hooks we have to go to create our database. To do this we go to the “Dependencies” section:

click-dependencies-reference

Inside this section we click on the “Create” button at the top right and then select “Postgresql”:

click-create-dependencies-reference
click-postgresql-reference

In the 1st postgresql creation form we will have to select our previously created project and define a name for it, then click on the “Next” button at the bottom right:

create-postgresql-reference

In the 2nd form we are going to have a lot of fields, the only ones that matter to us are the following:

SettingDescription
Database Master UsernameHere we assign a root user name to our database.
Database Master PasswordA password for this root user.

With these fields filled in, we are ready to move on. Click on the “Next” button at the bottom right to proceed to the third form:

create-postgresql-2-reference

In this last form, we are going to adjust the environment variables we have in our project with respect to the database. To do this, we need to change the following variables to our own:

BeforeAfter
*_POSTGRESQL_NAMEDB_NAME
*_POSTGRESQL_USERNAMEDB_USER
*_POSTGRESQL_PASSWORDDB_PASSWORD
*_POSTGRESQL_ADDRESSDB_HOST
*_POSTGRESQL_PORTDB_PORT

It should look something like the image below. Then click on the “Submit” button and your database should be created:

create-postgresql-3-reference

Create S3 Bucket

In the same page of dependencies we have to create our s3 bucket, for it we are going to go to the “Create” button again:

click-create-s3-dependencies-reference

And select S3 Bucket:

click-create-s3-dependencies-2-reference

In the first form we have to select our previously created project and define a name for the bucket, we have to take into account that the name of the bucket is global so it has to be unique. Now click on the “Next” button and go to step 3:

create-s3-dependencies-reference

Here we are going to see some environment variables defined for the bucket. We are going to edit the one that says COLLECTSTATICEXAMPLEDJANGOCELERY_BUCKET_NAME and we are going to call it DJANGO_AWS_STORAGE_BUCKET_NAME. With this simple change we click on the “Submit” button at the bottom right to finish creating the bucket:

create-s3-dependencies-3-reference

Create Rabbitmq

Now we need one more dependency. Our Rabbitmq to queue celery tasks, so let's get to it:

click-create-rabbitmq-dependencies-reference

And select Rabbitmq:

click-select-rabbitmq-reference

In the first form we will have to select our project and define a name for it. Then click on the “Next” button at the bottom right:

create-rabbitmq-reference

In the following form we have several fields but the only ones that matter to us for this example are the username and password, we can define whatever we want. For this example I chose admin as username and for the password I generated it randomly with the dice button. Then we click on the “Next” button to go to the next form:

create-rabbitmq-2-reference

In this last form we have to change the name of the variable that ends in *_BROKER_AUTH_URL to CELERY_BROKER_URL (as shown in the image). Then we click on the “Submit” button at the bottom right to finish creating rabbitmq:

create-rabbitmq-3-reference

Create yours environment variables

Once the dependencies are deployed we have to configure our environment variables. We are going to go to the Vargroups section:

click-vargroups-reference

Here you will see all your environment variables that you created grouped in groups, for example you should have created one with the data for the database (which is the one you see in the image). Now we are going to create another one for our django environment variables, for this we click on the “Create” button at the top right:

click-create-vargroups-reference

In this form we have the following fields:

  • Project: we select the project we created previously.
  • Workload: We select “global” that makes reference to be used by all our workloads.
  • Name: We define a name for this group of variables.
  • Type: If we want to load it by file or by variable.
  • Vars: Here we enable the textmode and copy the following environment variables:
CELERY_RESULT_BACKEND=django-db
DJANGO_ADMIN_URL=admin/
DJANGO_DEBUG=False
DJANGO_SECRET_KEY=secret_key
DJANGO_SETTINGS_MODULE=core.settings.production
DJANGO_STATIC_STORAGE=storages.backends.s3boto3.S3StaticStorage
DB_ENGINE=django.db.backends.postgresql_psycopg2
ENVIRONMENT=production
LOGS_LEVEL=INFO
PYTHONPATH=.

These environment variables are required for our example project. Finally click on the “Submit” button at the bottom right to create the variable group.

create-vargroup-reference

Deployments

As last step we are going to see our project deployed, for this we go to the “Deployments” section of the left panel:

click-deployments-reference

Here we are going to see all the deploys that we do. In our case it is the first one and we can see that it has been created correctly, in case you see any error if you click on “error” you can see a description of it. If we do not see any error then it means that the project is already deployed, we could begin to use it from the url that the web service provided us.

deployments-reference

This concludes our project deployment process. We leave you an optional step which is to configure the ci with github.

Optional

CI with Github

Every time you make a change in your code and want to deploy it you will have to do a build and a deploy, this eventually becomes tedious. That's why to avoid this we have to implement ci on github.

For this we are going to go to “Projects” in the left panel:

click-project-2-reference

Let's locate our project and click on the gear to access the project configuration:

click-settings-project-reference

In the project configuration we locate the one that says “Git pipelines” and click on it:

click-git-pipelines-reference

Here we are going to find what we need to do this. Basically we need to set up a file in the root of our project .github/workflows/ called ci_sleakops_demo.yml and in that file we are going to paste the content that appears in this page.

git-pipelines-reference

This needs to have an environment variable SLEAKOPS_KEY, if you don't have it you have to go to the link that appears there Settings -> CLI, get it and save it as an environment variable.

With this configured and deployed every time you do a push to your “main” branch a new version of your application will be launched automatically.