Skip to content

Commit

Permalink
add a worker deployment
Browse files Browse the repository at this point in the history
this runs a celery worker process in the image. we make the default
docker command just list the available `inv` tasks, and we use
kubernetes args to specify what `inv` should do inside the image
  • Loading branch information
igor47 committed Jun 21, 2019
1 parent 2fcd388 commit 12e7012
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 100 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Expand Up @@ -39,6 +39,7 @@ RUN IN_BUILD=True FLASK_APP=spaceship flask assets build
# set this to 'production' in production deploys
ENV ENVIRONMENT dev

ENTRYPOINT ["inv", "run.gunicorn"]
ENTRYPOINT ["inv"]
CMD ["--list"]

EXPOSE 9876/tcp
68 changes: 68 additions & 0 deletions k8s/container_environment.yaml
@@ -0,0 +1,68 @@
- name: IN_PRODUCTION
value: 'True'
- name: COMPONENT
valueFrom:
fieldRef:
fieldPath: metadata.labels['component']
- name: MYSQL_HOST
valueFrom:
secretKeyRef:
name: pyspaceship-mysql
key: host
- name: MYSQL_PORT
valueFrom:
secretKeyRef:
name: pyspaceship-mysql
key: port
- name: MYSQL_USERNAME
valueFrom:
secretKeyRef:
name: pyspaceship-mysql
key: username
- name: MYSQL_PASSWORD
valueFrom:
secretKeyRef:
name: pyspaceship-mysql
key: password
- name: MYSQL_DB
valueFrom:
secretKeyRef:
name: pyspaceship-mysql
key: db
- name: REDIS_HOST
valueFrom:
secretKeyRef:
name: pyspaceship-redis
key: host
- name: REDIS_PORT
valueFrom:
secretKeyRef:
name: pyspaceship-redis
key: port
- name: REDIS_DB
valueFrom:
secretKeyRef:
name: pyspaceship-redis
key: db
- name: SECRET_KEY
valueFrom:
secretKeyRef:
name: pyspaceship-session
key: secret_key
- name: SENDGRID_KEY
valueFrom:
secretKeyRef:
name: pyspaceship-sendgrid
key: secret_key
- name: GOOGLE_CLIENT_ID
valueFrom:
secretKeyRef:
name: pyspaceship-google-oauth
key: client_id
- name: GOOGLE_CLIENT_SECRET
valueFrom:
secretKeyRef:
name: pyspaceship-google-oauth
key: secret
- name: GOOGLE_APPLICATION_CREDENTIALS
value: /srv/pyspaceship/google-app-creds/key.json
94 changes: 0 additions & 94 deletions k8s/deployment.yaml

This file was deleted.

1 change: 1 addition & 0 deletions k8s/service.yaml
Expand Up @@ -6,6 +6,7 @@ spec:
type: NodePort
selector:
app: pyspaceship
component: web
ports:
- name: spaceship-port
protocol: TCP
Expand Down
35 changes: 35 additions & 0 deletions k8s/web_deployment.yaml
@@ -0,0 +1,35 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: pyspaceship-web-deployment
labels:
app: pyspaceship
spec:
selector:
matchLabels:
app: pyspaceship
component: web
replicas: {$eval: replicas}
template:
metadata:
labels:
app: pyspaceship
component: web
spec:
containers:
- name: pyspaceship-web
image: {$eval: image}
args: ["run.gunicorn"]
imagePullPolicy: "Always"
env: {$eval: container_environment}
livenessProbe:
httpGet:
path: /health
port: 9876
volumeMounts:
- name: google-app-creds
mountPath: /srv/pyspaceship/google-app-creds
volumes:
- name: google-app-creds
secret:
secretName: google-app-creds
31 changes: 31 additions & 0 deletions k8s/worker_deployment.yaml
@@ -0,0 +1,31 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: pyspaceship-worker-deployment
labels:
app: pyspaceship
spec:
selector:
matchLabels:
app: pyspaceship
component: worker
replicas: {$eval: replicas}
template:
metadata:
labels:
app: pyspaceship
component: worker
spec:
containers:
- name: pyspaceship-worker
image: {$eval: image}
args: ["run.celery-worker"]
imagePullPolicy: "Always"
env: {$eval: container_environment}
volumeMounts:
- name: google-app-creds
mountPath: /srv/pyspaceship/google-app-creds
volumes:
- name: google-app-creds
secret:
secretName: google-app-creds
2 changes: 2 additions & 0 deletions spaceship/config.py
Expand Up @@ -5,6 +5,8 @@
class Config:
IN_BUILD = bool(os.environ.get('IN_BUILD', False))
IN_PRODUCTION = bool(os.environ.get('IN_PRODUCTION', False))
COMPONENT = os.environ.get('COMPONENT', 'unknown')

SECRET_KEY = os.environ.get('SECRET_KEY', 'develoment')
SENDGRID_KEY = os.environ.get('SENDGRID_KEY', None)
EMAIL_CONFIRM_SALT = 'email-confirm-salt'
Expand Down
18 changes: 15 additions & 3 deletions tasks/image.py
Expand Up @@ -59,15 +59,27 @@ def do_deploy(tag, ns):
if ns.is_prod:
replicas = 3

deployment = load_manifest(
'deployment',
environment = load_manifest('container_environment', {})

web = load_manifest(
'web_deployment',
{
'image': tag,
'replicas': replicas,
'container_environment': environment,
}
)
ns.apply(web)

ns.apply(deployment)
worker = load_manifest(
'worker_deployment',
{
'image': tag,
'replicas': replicas,
'container_environment': environment,
}
)
ns.apply(worker)

service = load_manifest('service')
ns.apply(service)
Expand Down
2 changes: 0 additions & 2 deletions tasks/utils.py
Expand Up @@ -113,5 +113,3 @@ def load_manifest(mtype, context = {}):

def random_string(length):
return "".join( random.SystemRandom().choices(string.ascii_letters + string.digits, k=length))


0 comments on commit 12e7012

Please sign in to comment.