Kubernetes/Django: Redis values randomly change (inconsistent)

Hello I’m trying to make a Django website with an api route that when called will increment a redis value, I need to use kubernetes and host this project on GCP

I’ve been following this tutoruial: Deploying Django, Postgres, and Redis Containers To Kubernetes (Part 2) | by Bill Prin | Google Cloud - Community | Medium

And have my code hosted here:

I followed everything (had to make some small changes since this tutorial is a little out of date) but I have my Django/Redis/Postgresql containers running, and the guestbook django project displays a number of how many times you have visited the page in guestbook\guestbook\views.py:

from django.conf import settings
from django.core import serializers
from django.http import HttpResponse
from django.shortcuts import render

from rest_framework.decorators import api_view
from rest_framework import status
from rest_framework.response import Response

from .models import Message

from django.core.cache import cache

import math
import logging

globalcounter=0

def __update_visited():
    """ Updates the visited count by one in the cache
    :return: The visited count in string form.
    """
    visited = cache.get('visited')
    if not visited:
        visited = 0
    else:
        visited = int(visited)
    visited += 1
    cache.set('visited', str(visited))
    return str(visited)

But in production when you visit the site http://35.247.102.51 this number changes randomly, it doesn’t seem to be stored consistently and I cant figure out why:

#1 page visit: "This guestbook has been visited 1 times."
#2 page visit: "This guestbook has been visited 2 times."
#3 page visit: "This guestbook has been visited 1 times."
#4 page visit: "This guestbook has been visited 2 times."
#5 page visit: "This guestbook has been visited 3 times."
#6 page visit: "This guestbook has been visited 1 times."
#7 page visit: "This guestbook has been visited 1 times."
#8 page visit: "This guestbook has been visited 2 times."

As you can see, the number is not being stored or retrieved correctly it seems, the number should go up by 1 value every time you visit the page. I’m not getting any error messages so I dont think anything is configured correclty but I could be wrong, how can I ensure my django project is using my redis db and that both are dockeried with kubernetes correctly?

In my github repo I documented every step I took in the README file, which I will also post here:

WHAT I DID:

new martin readme steps to set up this project:

  • Create new Google Cloud Platform project (drdm-316805), enable kubernetes engine api.
  • Run $ gcloud init locally in my win10 WSL bash terminal to login and set project
  • Run $ gcloud container clusters create guestbook --scopes "https://www.googleapis.com/auth/userinfo.email","cloud-platform" --num-nodes 2 --region us-west1 to create new cluster
  • Run $ gcloud container clusters get-credentials guestbook --region us-west1 to configure kubectl with the right context
  1. setup local python virtual env
$ virtualenv mypython
$ source mypython/bin/activate
$ pip install -r requirements.txt --no-cache-dir
  1. Create redis cluster in kubernetes
    $ kubectl create -f kubernetes_configs/redis_cluster.yaml

  2. Create GCP database
    $ gcloud compute disks create pg-data --size 500GB --replica-zones us-west2-a,us-west2-b

  3. Generate password:
    $ echo mysecretpassword | base64

  4. Save password in kubernetes_configs/db_password.yaml

apiVersion: v1
kind: Secret
metadata:
  name: db-passwords
data:
  djangouserpw: ((put pword here))
  1. Add secret to cluster:
    $ kubectl create -f kubernetes_configs/db_password.yaml

  2. Build and publish postgresql image

$ cd postgres_image
$ docker build -t gcr.io/drdm-316805/postgres-pw .
$ docker push gcr.io/drdm-316805/postgres-pw
$ cd ..
  1. Edit kubernetes_configs/postgres.yaml to include my gcr image location:
    containers:
      - name: postgres
        image: gcr.io/drdm-316805/postgres-pw
  1. Create postgres pod
    $ kubectl create -f kubernetes_configs/postgres.yaml

  2. create frontend pod

  • replace guestbook location in frontend.yaml:
      - name: guestbook
        # Replace with your project ID or use `make template`
        image: gcr.io/drdm-316805/guestbook
  • Run command to create:
    $ kubectl create -f kubernetes_configs/frontend.yaml
  1. build image using dockerfile inside guesstbook folder
    $ docker build -t gcr.io/drdm-316805/guestbook ./guestbook/.

  2. upload new frontend image
    $ docker push gcr.io/drdm-316805/guestbook

  3. scale down then scale up

$ kubectl scale rc frontend --replicas=0 # kill your pods
$ kubectl scale rc frontend --replicas=3 # new image
  1. Run command $ kubectl get services

I wonder if this is a caching issue. Does it work if you set the frontend replicas to just 1?

Tried setting everything to 1:

kubectl scale rc redis-slave --replicas=1

kubectl scale rc frontend --replicas=1

but the same issue persists