<p>I am having a lot of issues configuring My Dockerized Django + PostgreSQL DB application to work on Kubernetes Cluster, which I have created using Google Cloud Platform.</p>

<p>How do I specify DATABASES.default.HOST from my settings.py file when I deploy image of PostgreSQL from Docker Hub and an image of my Django Web Application, to the Kubernetes Cluster?</p>

<p>Here is how I want my app to work. When I run the application locally, I want to use SQLITE DB, in order to do that I have made following changes in my settings.py file:</p>

<pre><code>if(os.getenv('DB')==None):
    print('Development - Using "SQLITE3" Database')
    DATABASES = {
        'default':{
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR,'db.sqlite3'),
        }
    }
else:
    print('Production - Using "POSTGRESQL" Database')
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'agent_technologies_db',
            'USER': 'stefan_radonjic',
            'PASSWORD': 'cepajecar995',
            'HOST': ,  #???
            'PORT': ,  #???
            }
    }
</code></pre>

<p>The main idea is that when I deploy application to Kubernetes Cluster, inside of Kubernetes Pod object, a Docker container ( my Dockerized Django application ) will run. When creating a container I am also creating Environment Variable <code>DB</code> and setting it to True. So when I deploy application I use PostgreSQL Database .</p>

<p><strong>NOTE</strong>: If anyone has any other suggestions how I should separate Local from Production development, please leave a comment. </p>

<p>Here is how my Dockerfile looks like:</p>

<pre><code>FROM python:3.6

ENV PYTHONUNBUFFERED 1
RUN mkdir /agent-technologies
WORKDIR /agent-technologies
COPY . /agent-technologies 
RUN pip install -r requirements.txt

EXPOSE 8000
</code></pre>

<p>And here is how my docker-compose file looks like:</p>

<pre><code>version: '3'
services:
  web:
    build: .
    command: python src/manage.py runserver --settings=agents.config.settings
    volumes: 
      - .:/agent-technologies
    ports: 
      - "8000:8000"
    environment:
      - DB=true
</code></pre>

<p>When running application locally it works perfectly fine. But when I try to deploy it to Kubernetes cluster, Pod objects which run my application containers are crashing in an infinite loop, because I dont know how to specify the DATABASES.default.HOST when running app in production environment. And of course the command specified in docker-compose file (<code>command: python src/manage.py runserver --settings=agents.config.settings</code>) probably produces an exception and makes the Pods crash in infinite loop.</p>

<p>NOTE: I have already created all necessary configuration files for Kubernetes ( Deployment definitions / Services / Secret / Volume files ). Here is my github link: <a href="https://github.com/StefanCepa/agent-technologies-bachelor" rel="nofollow noreferrer">https://github.com/StefanCepa/agent-technologies-bachelor</a></p>

<p>Any help would be appreciated! Thank you all in advance!</p>

<p>You will have to create a <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">service</a> (cluster ip) for your postgres pod to make it "accessible". When you create a service, you can <a href="https://kubernetes.io/docs/concepts/services-networking/service/#dns" rel="nofollow noreferrer">access</a> it via <code>&lt;service name&gt;.default:&lt;port&gt;</code>. However, running postgres (or any db) as a simple pod is dangerous (you will loose data as soon as you or k8s re-creates the pod or scale it up). You can use a <a href="https://cloud.google.com/sql/docs/postgres/" rel="nofollow noreferrer">service</a> or install it properly using <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="nofollow noreferrer">statefulSets</a>.</p>

<p>Once you have the address, you can put it in <a href="https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/" rel="nofollow noreferrer">env variable</a> and access it from your settings.py</p>

<p><strong>EDIT</strong>:
Put this in your deployment yaml (<a href="https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/" rel="nofollow noreferrer">example</a>):</p>

<pre><code>env:
- name: POSTGRES_HOST
  value: "postgres-service.default"
- name: POSTGRES_PORT
  value: "5432"
- name: DB
  value: "DB"
</code></pre>

<p>And in your settings.py</p>

<pre><code>'USER': 'stefan_radonjic',
'PASSWORD': 'cepajecar995',
'HOST': os.getenv('POSTGRES_HOST'),
'PORT': os.getenv('POSTGRES_PORT'),
</code></pre>
