<p>I have created a <strong>Django-Python application with a postgres database</strong>. Its working fine in my PC as well as in any other windows based systems.
I am trying to use K8s to host the application.
I have setup the postgres container successfully.</p>
<p><em><strong>But when I am trying to create the Django-Python container and tryong to start it, it shows me this kind of error:</strong></em></p>
<blockquote>
<p>Is the server running on host &quot;127.0.0.1&quot; and accepting
TCP/IP connections on port 5432?</p>
</blockquote>
<p>The Deployment and service yaml for the postgres container:</p>
<pre><code>---
# Deployment for the PostgreSQL container

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgresql
  namespace: trojanwall  
  labels:
    app: postgres-db
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres-db
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: postgres-db
        tier: postgreSQL
    spec:
      containers:
        - name: postgresql
          image: postgres:10.3
          ports:
          - containerPort: 5432
          env:
          - name: POSTGRES_USER
            valueFrom:
              secretKeyRef:
                name: postgres-db-credentials
                key: user
          - name: POSTGRES_PASSWORD
            valueFrom:
              secretKeyRef:
                name: postgres-db-credentials
                key: password
          - name: POSTGRES_DB
            value: 'postgres'
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgresql-volume-mount
          resources:
            requests:
              memory: &quot;64Mi&quot;
              cpu: &quot;250m&quot;
            limits:
              memory: &quot;128Mi&quot;
              cpu: &quot;500m&quot;
      volumes:
      - name: postgresql-volume-mount
        persistentVolumeClaim:
          claimName: postgres-pv-claim
---
# Service for the PostgreSQL container

apiVersion: v1
kind: Service
metadata:
  name: postgresql
  namespace: trojanwall  
  labels:
    app: postgres-db
spec:
  type: ClusterIP
  ports:
  - port: 5432
    targetPort: 5432
    protocol: TCP
  selector:
    app: postgres-db
    tier: postgreSQL
</code></pre>
<p><strong>The log of the Postgres container:</strong></p>
<pre><code>2020-09-23 15:39:58.034 UTC [1] LOG:  listening on IPv4 address &quot;0.0.0.0&quot;, port 5432
2020-09-23 15:39:58.034 UTC [1] LOG:  listening on IPv6 address &quot;::&quot;, port 5432
2020-09-23 15:39:58.038 UTC [1] LOG:  listening on Unix socket &quot;/var/run/postgresql/.s.PGSQL.5432&quot;
2020-09-23 15:39:58.049 UTC [23] LOG:  database system was shut down at 2020-09-23 15:37:17 UTC
2020-09-23 15:39:58.053 UTC [1] LOG:  database system is ready to accept connections
2020-09-23 15:47:12.845 UTC [1] LOG:  received smart shutdown request
2020-09-23 15:47:12.846 UTC [1] LOG:  worker process: logical replication launcher (PID 29) exited with exit code 1
2020-09-23 15:47:12.846 UTC [24] LOG:  shutting down
2020-09-23 15:47:12.851 UTC [1] LOG:  database system is shut down
2020-09-23 15:47:13.123 UTC [1] LOG:  listening on IPv4 address &quot;0.0.0.0&quot;, port 5432
2020-09-23 15:47:13.123 UTC [1] LOG:  listening on IPv6 address &quot;::&quot;, port 5432
2020-09-23 15:47:13.126 UTC [1] LOG:  listening on Unix socket &quot;/var/run/postgresql/.s.PGSQL.5432&quot;
2020-09-23 15:47:13.134 UTC [24] LOG:  database system was shut down at 2020-09-23 15:47:12 UTC
2020-09-23 15:47:13.138 UTC [1] LOG:  database system is ready to accept connections
2020-09-23 15:47:25.722 UTC [1] LOG:  received smart shutdown request
2020-09-23 15:47:25.724 UTC [1] LOG:  worker process: logical replication launcher (PID 30) exited with exit code 1
2020-09-23 15:47:25.725 UTC [25] LOG:  shutting down
2020-09-23 15:47:25.730 UTC [1] LOG:  database system is shut down
2020-09-23 15:47:25.925 UTC [1] LOG:  listening on IPv4 address &quot;0.0.0.0&quot;, port 5432
2020-09-23 15:47:25.925 UTC [1] LOG:  listening on IPv6 address &quot;::&quot;, port 5432
2020-09-23 15:47:25.927 UTC [1] LOG:  listening on Unix socket &quot;/var/run/postgresql/.s.PGSQL.5432&quot;
2020-09-23 15:47:25.937 UTC [23] LOG:  database system was shut down at 2020-09-23 15:47:25 UTC
2020-09-23 15:47:25.941 UTC [1] LOG:  database system is ready to accept connections
</code></pre>
<p>Now when I am trying to deploy the Django-Python container, it just wont connect to the database container.</p>
<p>Django-Python application deployment and service YAML file:</p>
<pre><code>---
# Deployment for the Django-Python application container

apiVersion: apps/v1
kind: Deployment
metadata:
  name: trojanwall-django
  namespace: trojanwall  
  labels:
    app: django
spec:
  replicas: 1
  selector:
    matchLabels:
      app: django
  template:
    metadata:
      labels:
        app: django
    spec:
      containers:
      - name: trojanwall-django
        image: arbabu/trojan-wall:v3.0
        imagePullPolicy: Always
        ports:
        - containerPort: 8000
        env:
          - name: POSTGRES_USER
            valueFrom:
              secretKeyRef:
                name: postgres-db-credentials
                key: user
          - name: POSTGRES_PASSWORD
            valueFrom:
              secretKeyRef:
                name: postgres-db-credentials
                key: password
          - name: POSTGRES_DB
            value: 'postgres'
          - name: DATABASE_URL
            value: postgres://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@postgresql:5432/$(POSTGRES_DB)
          - name: DJANGO_SETTINGS_MODULE
            value: 'TestProject.settings'
          - name: SECRET_KEY
            valueFrom:
              secretKeyRef:
                name: django-secret-key
                key: secret_key
        resources:
            requests:
              memory: &quot;64Mi&quot;
              cpu: &quot;250m&quot;
            limits:
              memory: &quot;128Mi&quot;
              cpu: &quot;500m&quot;
        volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgresql-volume-mount
      volumes:
      - name: postgresql-volume-mount
        persistentVolumeClaim:
          claimName: postgres-pv-claim

---
# Service for the Django-Python application container

apiVersion: v1
kind: Service
metadata:
  name: trojanwall-django
  namespace: trojanwall  
  labels:
    app: django
spec:
  ports:
  - port: 8000
    targetPort: 8000
    protocol: TCP
  type: NodePort
  selector:
    app: django
</code></pre>
<p>After this step, the pods do start running, but once I bash into the Django container and run the command:</p>
<blockquote>
<p><strong>python3 manage.py migrate</strong></p>
</blockquote>
<p>It shows me this error:</p>
<pre><code>root@trojanwall-django-7df4bc7759-89bgv:/TestProject# python3 manage.py migrate
Traceback (most recent call last):
  File &quot;/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py&quot;, line 219, in ensure_connection
    self.connect()
  File &quot;/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py&quot;, line 26, in inner
    return func(*args, **kwargs)
  File &quot;/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py&quot;, line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File &quot;/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py&quot;, line 26, in inner
    return func(*args, **kwargs)
  File &quot;/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py&quot;, line 187, in get_new_connection
    connection = Database.connect(**conn_params)
  File &quot;/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py&quot;, line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused
        Is the server running on host &quot;127.0.0.1&quot; and accepting
        TCP/IP connections on port 5432?


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File &quot;manage.py&quot;, line 21, in &lt;module&gt;
    main()
  File &quot;manage.py&quot;, line 17, in main
    execute_from_command_line(sys.argv)
  File &quot;/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py&quot;, line 401, in execute_from_command_line
    utility.execute()
  File &quot;/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py&quot;, line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File &quot;/usr/local/lib/python3.8/site-packages/django/core/management/base.py&quot;, line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File &quot;/usr/local/lib/python3.8/site-packages/django/core/management/base.py&quot;, line 371, in execute
    output = self.handle(*args, **options)
  File &quot;/usr/local/lib/python3.8/site-packages/django/core/management/base.py&quot;, line 85, in wrapped
    res = handle_func(*args, **kwargs)
  File &quot;/usr/local/lib/python3.8/site-packages/django/core/management/commands/migrate.py&quot;, line 92, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File &quot;/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py&quot;, line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File &quot;/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py&quot;, line 53, in __init__
    self.build_graph()
  File &quot;/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py&quot;, line 216, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File &quot;/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py&quot;, line 77, in applied_migrations
    if self.has_table():
  File &quot;/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py&quot;, line 55, in has_table
    with self.connection.cursor() as cursor:
  File &quot;/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py&quot;, line 26, in inner
    return func(*args, **kwargs)
  File &quot;/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py&quot;, line 259, in cursor
    return self._cursor()
  File &quot;/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py&quot;, line 235, in _cursor
    self.ensure_connection()
  File &quot;/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py&quot;, line 26, in inner
    return func(*args, **kwargs)
  File &quot;/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py&quot;, line 219, in ensure_connection
    self.connect()
  File &quot;/usr/local/lib/python3.8/site-packages/django/db/utils.py&quot;, line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File &quot;/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py&quot;, line 219, in ensure_connection
    self.connect()
  File &quot;/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py&quot;, line 26, in inner
    return func(*args, **kwargs)
  File &quot;/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py&quot;, line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File &quot;/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py&quot;, line 26, in inner
    return func(*args, **kwargs)
  File &quot;/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py&quot;, line 187, in get_new_connection
    connection = Database.connect(**conn_params)
  File &quot;/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py&quot;, line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: Connection refused
        Is the server running on host &quot;127.0.0.1&quot; and accepting
        TCP/IP connections on port 5432?
</code></pre>
<blockquote>
<p><strong>Does anyone know how to resolve this?</strong></p>
</blockquote>
<p>Here's a reference to the settings.py file's database configurations.</p>
<pre><code>DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ.get('POSTGRES_NAME', 'postgres'),
        'USER': os.environ.get('POSTGRES_USER', 'postgres'),
        'PASSWORD': os.environ.get('POSTGRES_PASSWORD', 'postgres'),     
        'HOST': os.getenv('POSTGRES_SERVICE_HOST','127.0.0.1'),
        'PORT': os.getenv('POSTGRES_SERVICE_PORT',5432)
    }
}
</code></pre>
<p>The secrets yaml file:</p>
<pre><code>---
# Secrets for the Database Credential Management

apiVersion: v1
kind: Secret
metadata:
  name: postgres-db-credentials
  namespace: trojanwall
  labels:
    app: postgres-db
type: opaque
data:
  user: cG9zdGdyZXM=
  password: cG9zdGdyZXM=
</code></pre>

arjunbnair
<pre><code>kind: Service
metadata:
  name: postgresql
  namespace: trojanwall  
  labels:
    app: postgres-db
spec:
  type: ClusterIP
</code></pre>
<p>You service for the PostreSQL instance will allocate a new IP address from the Kubernetes cluster, since your service has type <code>ClusterIP</code>. This is well.</p>
<p>But your Python app has to connect to PostgreSQL on <em>that IP address</em> and <strong>not</strong> to <code>127.0.0.1</code>.</p>
<p>From the line below in your <code>setting.py</code> it looks like the IP address for your PostgreSQL instance can be overridden, it must be changed to reflect the IP in the Kubernetes cluster.</p>
<pre><code>        'HOST': os.getenv('POSTGRES_SERVICE_HOST','127.0.0.1'),
</code></pre>
<p>Update your <code>Deployment</code> for the app, to contain an environment value for <code>POSTGRES_SERVICE_HOST</code>.</p>
<p>Example:</p>
<pre><code>    spec:
      containers:
      - name: trojanwall-django
        image: arbabu/trojan-wall:v3.0
        imagePullPolicy: Always
        ports:
        - containerPort: 8000
        env:
          - name: POSTGRES_SERVICE_HOST
            value: &quot;&lt;INSERT YOUR IP ADDRESS&gt;&quot; # update this to reflect your IP
          - name: POSTGRES_USER
            valueFrom:
              secretKeyRef:
                name: postgres-db-credentials
                key: user
          - name: POSTGRES_PASSWORD
            valueFrom:
              secretKeyRef:
                name: postgres-db-credentials
                key: password
</code></pre>
