Skip to content

Commit

Permalink
Merge pull request #253 from norcalipa/norcalipa/redis-two-node
Browse files Browse the repository at this point in the history
Norcalipa/redis two node
  • Loading branch information
norcalipa authored Nov 15, 2024
2 parents 602b5c3 + 712126a commit a5c1a49
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 50 deletions.
3 changes: 2 additions & 1 deletion .env-prod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ DB_NAME=crank
DB_USER=crank
DB_PORT=3306
PYTHON_UNBUFFERED=1
REDIS_URL=redis://redis:6379/0
REDIS_MASTER_URL=redis://redis-master:6379/0
REDIS_SLAVE_URL=redis://redis-slave:6379/0
CACHE_TTL=60
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
ENV: dev
SECRET_KEY: ${{ secrets.SECRET_KEY }}
DJANGO_SETTINGS_MODULE: 'crank.settings'
REDIS_URL: 'redis://localhost:6379/0'
REDIS_MASTER_URL: 'redis://localhost:6379/0'
CACHE_TTL: 60
PYTHON_UNBUFFERED: 1
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
env:
ENV: dev
SECRET_KEY: '${{ secrets.SECRET_KEY }}'
REDIS_URL: 'redis://localhost:6379/0'
REDIS_MASTER_URL: 'redis://localhost:6379/0'
CACHE_TTL: 60
run: |
pytest
Expand Down
6 changes: 4 additions & 2 deletions crank/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,13 @@
SESSION_COOKIE_AGE = 1800 # 30 minutes in seconds

CACHE_MIDDLEWARE_SECONDS = int(os.environ["CACHE_TTL"]) # Timeout for cached items in seconds
REDIS_URL = os.environ["REDIS_URL"]
REDIS_MASTER_URL = os.environ.get("REDIS_MASTER_URL", "redis://redis-master:6379/0")

# Optional: To use Redis for session storage
CACHES = {
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': REDIS_URL,
'LOCATION': REDIS_MASTER_URL,
'OPTIONS': {
'CLIENT_CLASS': 'django_redis.client.DefaultClient',
'SOCKET_CONNECT_TIMEOUT': 5, # in seconds
Expand All @@ -174,6 +175,7 @@
# Set the TTL for session entries in Redis
SESSION_REDIS = {
'ttl': 1800, # 30 minutes in seconds
'master': REDIS_MASTER_URL,
}

# Password validation
Expand Down
10 changes: 10 additions & 0 deletions crank/settings/prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from django.db import connection
from pathlib import Path

from crank.settings import REDIS_MASTER_URL

pymysql.install_as_MySQLdb()

BASE_DIR = Path(__file__).resolve().parent.parent.parent
Expand All @@ -19,6 +21,14 @@
SESSION_COOKIE_HTTPONLY = True
CSRF_COOKIE_SECURE = True

REDIS_SLAVE_URLS = os.environ.get("REDIS_SLAVE_URLS", "redis://redis-slave:6379/0").split(",")

SESSION_REDIS = {
'ttl': 1800, # 30 minutes in seconds
'master': REDIS_MASTER_URL,
'slaves': REDIS_SLAVE_URLS,
}

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
Expand Down
6 changes: 4 additions & 2 deletions k8s/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ spec:
value: "3306"
- name: ENV
value: "prod"
- name: REDIS_URL
value: "redis://redis:6379/0"
- name: REDIS_MASTER_URL
value: "redis://redis-master:6379/0"
- name: REDIS_SLAVE_URLS
value: "redis://redis-slave:6379/0"
- name: CACHE_TTL
value: "60"
- name: PYTHONUNBUFFERED
Expand Down
111 changes: 68 additions & 43 deletions k8s/redis.yml
Original file line number Diff line number Diff line change
@@ -1,84 +1,109 @@
# Copyright (c) 2024 Isaac Adams
# Licensed under the MIT License. See LICENSE file in the project root for full license information.
---
apiVersion: v1
kind: Namespace
metadata:
name: crank
---
apiVersion: v1
kind: PersistentVolume
kind: Service
metadata:
name: redis-pv
name: redis-master
namespace: crank
labels:
app: redis
role: master
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /mnt/data/redis
ports:
- port: 6379
targetPort: 6379
selector:
app: redis
role: master
---
apiVersion: v1
kind: PersistentVolumeClaim
kind: Service
metadata:
name: redis-pvc
name: redis-slave
namespace: crank
labels:
app: redis
role: slave
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
ports:
- port: 6379
targetPort: 6379
selector:
app: redis
role: slave
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
name: redis-master
namespace: crank
labels:
app: redis
role: master
spec:
replicas: 1
selector:
matchLabels:
app: redis
role: master
template:
metadata:
labels:
app: redis
role: master
spec:
containers:
- name: redis
image: redis:7.0.5-alpine
ports:
- containerPort: 6379
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
volumeMounts:
- mountPath: /data
name: redis-storage
volumes:
- name: redis-storage
persistentVolumeClaim:
claimName: redis-pvc
- name: redis
image: redis:latest
ports:
- containerPort: 6379
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
---
apiVersion: v1
kind: Service
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
name: redis-slave
namespace: crank
labels:
app: redis
role: slave
spec:
ports:
- port: 6379
targetPort: 6379
replicas: 1
selector:
app: redis
type: ClusterIP
matchLabels:
app: redis
role: slave
template:
metadata:
labels:
app: redis
role: slave
spec:
containers:
- name: redis
image: redis:latest
ports:
- containerPort: 6379
env:
- name: REDIS_MASTER_SERVICE_HOST
value: "redis-master"
- name: REDIS_REPLICATION_MODE
value: "slave"
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"

0 comments on commit a5c1a49

Please sign in to comment.