0

I have a deployment of a simple Django app in minikube. It has two containers one for the Django app and one for postgres db. It is working with docker-compose, but couldn't make it work in minikube k8s cluster. When I opened a terminal to the container and ping the service it wasn't successful. I didn't find what causes this communication error.

It's a basic app that just has a login page for signing in or signing up. After signing in you can create some notes. After I deployed to minikube I was able to access 127.0.0.1:8000, but when I enter the information to sign up it gave the error below. Apparently, it couldn't store data in the db.

DATABASES part in settings.py in django project:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'postgres',
    'USER': 'postgres',
    'PASSWORD': 'postgres',
    'HOST': 'db',
    'PORT': '5432',
}
}

Dockerfile for building the image for the app:

FROM python:2.7

WORKDIR /notejam

COPY ./ ./

RUN pip install -r requirements.txt

EXPOSE 8000

CMD python manage.py runserver 0.0.0.0:8000

Deployment file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: notejam-deployment
  labels: 
    app: notejam-app
spec:
  selector:
    matchLabels:
      app: notejam-app
  template:
    metadata:
      labels:
        app: notejam-app
    spec:
      volumes:
        - name: postgres-pvc
          persistentVolumeClaim:
            claimName: postgres-pvc
      containers:
      - name: notejam
        image: notejam_k8s
        imagePullPolicy: Never
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 8000
      - name: postgres-db
        image: postgres
        imagePullPolicy: Never
        ports:
        - containerPort: 5432
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        env:
        - name: POSTGRES_USERNAME
          valueFrom:
            configMapKeyRef:
              name: postgres-configmap
              key: postgres-username
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: postgres-secret
              key: postgres-password
        - name: POSTGRES_DB
          valueFrom:
            configMapKeyRef:
              name: postgres-configmap
              key: postgres-db
        volumeMounts:
          - mountPath: /notejam-db
            name: postgres-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: db
spec:
  selector:
    app: notejam-app
  ports:
  - port: 5432
    targetPort: 5432
---
apiVersion: v1
kind: Service
metadata:
  name: notejam-external-service
spec:
  selector:
    app: notejam-app
  type: LoadBalancer
  ports:
  - protocol: TCP 
    port: 8000
    targetPort: 8000

Error:

OperationalError at /signup/

could not connect to server: Connection timed out
    Is the server running on host "db" (10.96.150.207) and accepting
    TCP/IP connections on port 5432?

Request Method:     POST
Request URL:    http://127.0.0.1:8000/signup/
Django Version:     1.6.5
Exception Type:     OperationalError
Exception Value:    

could not connect to server: Connection timed out
    Is the server running on host "db" (10.96.150.207) and accepting
    TCP/IP connections on port 5432?

Exception Location:     /usr/local/lib/python2.7/site-packages/psycopg2/__init__.py in connect, line 127
Python Executable:  /usr/local/bin/python
Python Version:     2.7.18
Python Path:    

['/notejam',
'/usr/local/lib/python27.zip',
'/usr/local/lib/python2.7',
'/usr/local/lib/python2.7/plat-linux2',
'/usr/local/lib/python2.7/lib-tk',
'/usr/local/lib/python2.7/lib-old',
'/usr/local/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/site-packages']

Server time:    Thu, 26 Aug 2021 00:40:58 +0300
4
  • Are you tried this? 'ENGINE': 'django.db.backends.postgresql' source Commented Aug 25, 2021 at 21:32
  • I changed the settings.py then rebuilt the image and redeploy the application, but no luck. It didn't work. Commented Aug 25, 2021 at 21:39
  • Can you add errors to your question? at least other guys that see the question can help you better. Commented Aug 25, 2021 at 21:42
  • 1
    Yeah, of course. Commented Aug 25, 2021 at 21:44

1 Answer 1

2

You're trying to run postgres as a secondary container in the same pod. It should be its own deployment and service. Multi-container pods are not like docker compose :)

Sign up to request clarification or add additional context in comments.

2 Comments

So you mean I should create another deployment yaml for postgresql? I didn't get the purpose though.
@coderanger Thanks for the answer. It worked. Then we should define another deployment in order to make them run in different pods, because services provide communication between pods.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.