Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add hpa and async worker to enterprise-catalog. #5

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions tutorenterprise/patches/k8s-deployments
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,48 @@ spec:
subPath: production.py
securityContext:
allowPrivilegeEscalation: false
resources:
limits:
cpu: "{{ ENTERPRISE_CATALOG_LIMIT_CPU }}"
memory: "{{ ENTERPRISE_CATALOG_LIMIT_MEMORY }}"
requests:
cpu: "{{ ENTERPRISE_CATALOG_REQUEST_CPU }}"
memory: "{{ ENTERPRISE_CATALOG_REQUEST_MEMORY }}"
Comment on lines +31 to +37
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove these limits as we don't have any idea of the ideal limits. @Jacatove

Copy link
Contributor Author

@Jacatove Jacatove Dec 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Squirrel18 "Any HPA target can be scaled based on the resource usage of the pods in the scaling target. When defining the pod specification the resource requests like cpu and memory should be specified. This is used to determine the resource utilization and used by the HPA controller to scale the target up or down". Copied from https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#support-for-resource-metrics.

So I think it is worth to give some thought about resources:

This is what I have gathered so far. I ran the enterprise-catalog-update-content-job and this is the result.

MEMORY:
image
image

CPU
image
image

Checking the resourses used by the pods, I got this:

enterprise-catalog-64554f5895-8kl52                         0m           501Mi           
enterprise-catalog-update-content-job-1703786342004-kmzmg   60m          89Mi            
jacato:~$ kubectl top pod | grep enterprise
enterprise-catalog-64554f5895-8kl52                         0m           501Mi           
enterprise-catalog-update-content-job-1703786342004-kmzmg   5m           93Mi

volumes:
- name: settings
configMap:
name: enterprise-catalog-settings
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: enterprise-catalog-worker
labels:
app.kubernetes.io/name: enterprise-catalog-worker
spec:
selector:
matchLabels:
app.kubernetes.io/name: enterprise-catalog-worker
template:
metadata:
labels:
app.kubernetes.io/name: enterprise-catalog-worker
spec:
securityContext:
runAsUser: 1000
runAsGroup: 1000
containers:
- name: enterprise-catalog-worker
image: {{ ENTERPRISE_CATALOG_DOCKER_IMAGE }}
args: ["celery", "--app=enterprise_catalog.celery", "worker", "--loglevel=info"]
ports:
- containerPort: 8000
volumeMounts:
- mountPath: /openedx/enterprise-catalog/enterprise_catalog/settings/tutor/production.py
name: settings
subPath: production.py
securityContext:
allowPrivilegeEscalation: false
volumes:
- name: settings
configMap:
Expand Down
1 change: 1 addition & 0 deletions tutorenterprise/patches/kustomization-resources
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- plugins/enterprise/k8s/hpa.yml
16 changes: 16 additions & 0 deletions tutorenterprise/patches/local-docker-compose-services
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,19 @@ enterprise-catalog:
- redis
{% if RUN_MYSQL %}- mysql{% endif %}
{% if RUN_LMS %}- lms{% endif %}

enterprise-catalog-worker:
image: {{ ENTERPRISE_CATALOG_DOCKER_IMAGE }}
environment:
DJANGO_SETTINGS_MODULE: enterprise_catalog.settings.tutor.production
command: celery --app=enterprise_catalog.celery:app worker -l INFO
restart: unless-stopped
volumes:
- ../plugins/enterprise/apps/enterprise-catalog/settings:/openedx/enterprise-catalog/enterprise_catalog/settings/tutor:ro
depends_on:
- discovery
- ecommerce
- redis
- enterprise-catalog
{% if RUN_MYSQL %}- mysql{% endif %}
{% if RUN_LMS %}- lms{% endif %}
11 changes: 11 additions & 0 deletions tutorenterprise/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@
"CATALOG_DOCKER_IMAGE": "{{ DOCKER_REGISTRY }}paops/enterprise-catalog:{{ ENTERPRISE_VERSION }}",
"DISCOVERY_USER": "discovery",
"ECOMMERCE_OAUTH_USER": "{{ ECOMMERCE_OAUTH2_KEY }}",
### HPA ###
"CATALOG_LIMIT_CPU": "1",
"CATALOG_LIMIT_MEMORY": "1Gi",
"CATALOG_REQUEST_CPU": "512m",
"CATALOG_REQUEST_MEMORY": "512Mi",
"CATALOG_ENABLE_HPA": False,
"CATALOG_MIN_REPLICAS": 1,
"CATALOG_MAX_REPLICAS": 4,
"CATALOG_AVG_CPU": 65,
"CATALOG_AVG_MEMORY": "500Mi",
},
"unique": {
"OAUTH2_SECRET_KEY": "{{ 64|random_string }}",
Expand Down Expand Up @@ -161,6 +171,7 @@
[
("enterprise/build", "plugins"),
("enterprise/apps", "plugins"),
("enterprise/k8s", "plugins"),
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
{% include "enterprise/apps/enterprise-catalog/settings/partials/common.py" %}

DISCOVERY_CATALOG_QUERY_CACHE_TIMEOUT = 0
CELERY_TASK_ALWAYS_EAGER = True

{{ patch("enterprise-catalog-development-settings") }}
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@
ECOMMERCE_BASE_URL = "{% if ENABLE_HTTPS %}https{% else %}http{% endif %}://{{ ECOMMERCE_HOST }}"
LICENSE_MANAGER_BASE_URL = ""

# Worker settings. We currently don't support async workers for the Enterprise Catalogue service.
# Enterprise Catalogue Worker settings.
CELERY_WORKER_HIJACK_ROOT_LOGGER = True
CELERY_TASK_ALWAYS_EAGER = True
CELERY_TASK_ALWAYS_EAGER = False
CELERY_BROKER_TRANSPORT = "redis"
CELERY_BROKER_HOSTNAME = "{{ REDIS_HOST }}:{{ REDIS_PORT }}"
CELERY_BROKER_VHOST = "{{ OPENEDX_CELERY_REDIS_DB }}"
Expand Down
32 changes: 32 additions & 0 deletions tutorenterprise/templates/enterprise/k8s/hpa.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{%- if ENTERPRISE_CATALOG_ENABLE_HPA %}
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: enterprise-catalog
labels:
app.kubernetes.io/name: enterprise-catalog
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: enterprise-catalog
minReplicas: {{ ENTERPRISE_CATALOG_MIN_REPLICAS }}
maxReplicas: {{ ENTERPRISE_CATALOG_MAX_REPLICAS }}
metrics:
{%- if ENTERPRISE_CATALOG_AVG_CPU > 0 %}
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: {{ ENTERPRISE_CATALOG_AVG_CPU }}
{%- endif %}
{%- if ENTERPRISE_CATALOG_AVG_MEMORY|length %}
- type: Resource
resource:
name: memory
target:
type: AverageValue
averageValue: {{ ENTERPRISE_CATALOG_AVG_MEMORY }}
{%- endif %}
{%- endif %}