how to run celery with django on openshift 3

只谈情不闲聊 提交于 2019-12-02 18:24:20

问题


What is the easiest way to launch a celery beat and worker process in my django pod?

I'm migrating my Openshift v2 Django app to Openshift v3. I'm using Pro subscription. I'm really a noob on Openshift v3 and docker and containers and kubernetes. I have used this tutorial https://blog.openshift.com/migrating-django-applications-openshift-3/ to migrate my app (which works pretty well).

I'm now struggling on how to start celery. On Openshift 2 I just used an action hook post_start:

source $OPENSHIFT_HOMEDIR/python/virtenv/bin/activate

python $OPENSHIFT_REPO_DIR/wsgi/podpub/manage.py celery worker\
--pidfile="$OPENSHIFT_DATA_DIR/celery/run/%n.pid"\
--logfile="$OPENSHIFT_DATA_DIR/celery/log/%n.log"\

python $OPENSHIFT_REPO_DIR/wsgi/podpub/manage.py celery beat\
--pidfile="$OPENSHIFT_DATA_DIR/celery/run/celeryd.pid"\
--logfile="$OPENSHIFT_DATA_DIR/celery/log/celeryd.log" &
-c 1\
--autoreload &

It is a quite simple setup. It just uses the django database as a message broker. No rabbitMQ or something.

Would a openshift "job" be appropriated for that? Or better use powershift image (https://pypi.python.org/pypi/powershift-image) action commands? But I did not understand how to execute them.

here is the current deployment configuration for my only app "

apiVersion: v1
kind: DeploymentConfig
metadata:
  annotations:
    openshift.io/generated-by: OpenShiftNewApp
  creationTimestamp: 2017-12-27T22:58:31Z
  generation: 67
  labels:
    app: django
  name: django
  namespace: myproject
  resourceVersion: "68466321"
  selfLink: /oapi/v1/namespaces/myproject/deploymentconfigs/django
  uid: 64600436-ab49-11e7-ab43-0601fd434256
spec:
  replicas: 1
  selector:
    app: django
    deploymentconfig: django
  strategy:
    activeDeadlineSeconds: 21600
    recreateParams:
      timeoutSeconds: 600
    resources: {}
    rollingParams:
      intervalSeconds: 1
      maxSurge: 25%
      maxUnavailable: 25%
      timeoutSeconds: 600
      updatePeriodSeconds: 1
    type: Recreate
  template:
    metadata:
      annotations:
    openshift.io/generated-by: OpenShiftNewApp
      creationTimestamp: null
      labels:
    app: django
    deploymentconfig: django
    spec:
      containers:
      - image: docker-registry.default.svc:5000/myproject/django@sha256:6a0caac773acc65daad2e6ac87695f9f01ae3c99faba14536e0ec2b65088c808
    imagePullPolicy: Always
    name: django
    ports:
    - containerPort: 8080
      protocol: TCP
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /opt/app-root/src/data
      name: data
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      volumes:
      - name: data
    persistentVolumeClaim:
      claimName: django-data
  test: false
  triggers:
  - type: ConfigChange
  - imageChangeParams:
      automatic: true
      containerNames:
      - django
      from:
    kind: ImageStreamTag
    name: django:latest
    namespace: myproject
      lastTriggeredImage: docker-registry.default.svc:5000/myproject/django@sha256:6a0caac773acc65daad2e6ac87695f9f01ae3c99faba14536e0ec2b65088c808
    type: ImageChange

I'm using mod_wsgi-express and this is my app.sh

ARGS="$ARGS --log-to-terminal"
ARGS="$ARGS --port 8080"
ARGS="$ARGS --url-alias /static wsgi/static"

exec mod_wsgi-express start-server $ARGS wsgi/application

Help is very appreciated. Thank you


回答1:


I have managed to get it working, though I'm not quite happy with it. I will move to a postgreSQL database very soon. Here is what I did:

wsgi_mod-express has an option called service-script which starts an additional process besides the actual app. So I updated my app.sh:

#!/bin/bash

ARGS=""

ARGS="$ARGS --log-to-terminal"
ARGS="$ARGS --port 8080"
ARGS="$ARGS --url-alias /static wsgi/static"
ARGS="$ARGS --service-script celery_starter scripts/startCelery.py"

exec mod_wsgi-express start-server $ARGS wsgi/application

mind the last ARGS=... line.

I created a python script that starts up my celery worker and beat. startCelery.py:

import subprocess

OPENSHIFT_REPO_DIR="/opt/app-root/src"

OPENSHIFT_DATA_DIR="/opt/app-root/src/data"

pathToManagePy=OPENSHIFT_REPO_DIR + "/wsgi/podpub"

worker_cmd = [
    "python",
    pathToManagePy + "/manage.py",
    "celery",
    "worker",
    "--pidfile="+OPENSHIFT_REPO_DIR+"/%n.pid",
    "--logfile="+OPENSHIFT_DATA_DIR+"/celery/log/%n.log",
    "-c 1",
    "--autoreload"
    ]
print(worker_cmd)


subprocess.Popen(worker_cmd, close_fds=True)

beat_cmd = [
    "python",
    pathToManagePy + "/manage.py",
    "celery",
    "beat",
    "--pidfile="+OPENSHIFT_REPO_DIR+"/celeryd.pid",
    "--logfile="+OPENSHIFT_DATA_DIR+"/celery/log/celeryd.log",
    ]
print(beat_cmd)

subprocess.Popen(beat_cmd)

this was actually working, but I kept receiving a message when I tried to launch the celery worker saying "Running a worker with superuser privileges when the worker accepts messages serialized with pickle is a very bad idea! If you really want to continue then you have to set the C_FORCE_ROOT environment variable (but please think about this before you do)."

Eventhough I added these configurations to my settings.py in order to remove pickle serializer, it kept giving me that same error message.

CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_ACEEPT_CONTENT = ['json']

I don't know why. At the end I added C_FORCE_ROOT to my .s2i/enviroment

C_FORCE_ROOT=true

Now it's working, at least I thinks so. My next job will only run in some hours. I'm still open for any further suggestions and tipps.



来源:https://stackoverflow.com/questions/48102638/how-to-run-celery-with-django-on-openshift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!