Can't connect to Pod in DevSpace following Django tutorial

9/9/2021

The DevSpace-Django tutorial in question is the following:

https://devspace.cloud/blog/2019/10/18/deploy-django-to-kubernetes

Trying something completely barebones to understand how devspace works to decide if I want to make the switch from skaffold. There are a number of things in the above tutorial that seem to be no longer accurate or have changed in more recent versions of devpsace.

At any rate, I'm not able to connect to the Django tutorial app when I navigate to localhost:8000. It just says "Can't connect to the server."

This is the output I get when I devspace dev:

 devspace dev
[warn]   There is a newer version of DevSpace: v5.15.0. Run `devspace upgrade` to upgrade to the newest version.

[info]   Using namespace 'mysite'
[info]   Using kube context 'mysite'
[info]   Execute 'helm upgrade mysite component-chart --namespace mysite --values /var/folders/tl/wqf19mws155_7bkqyw401z_w0000gn/T/325113665 --install --repo https://charts.devspace.sh --repository-config='' --version 0.8.0 --kube-context mysite'
[info]   Execute 'helm list --namespace mysite --output json --kube-context mysite'
[done] √ Deployed helm chart (Release revision: 5)                    
[done] √ Successfully deployed mysite with helm                       
[done] √ Port forwarding started on 8000:8000 (mysite/mysite-7b856bb78b-2ztpf-devspace)
                                             
#########################################################
[info]   DevSpace UI available at: http://localhost:8090
#########################################################

[0:sync] Waiting for pods...
[0:sync] Starting sync...
[0:sync] Sync started on /Users/cjones/Projects/Apps/Test/mysite <-> . (Pod: mysite/mysite-7b856bb78b-2ztpf-devspace)
[0:sync] Waiting for initial sync to complete
[info]   Opening 'http://localhost:8000' as soon as application will be started (timeout: 4m0s)
[info]   Opening shell to pod:container mysite-7b856bb78b-2ztpf-devspace:container-0
Installing Python Dependencies
Requirement already satisfied: asgiref==3.4.1 in /usr/local/lib/python3.9/site-packages (from -r requirements.txt (line 1)) (3.4.1)
Requirement already satisfied: Django==3.2.7 in /usr/local/lib/python3.9/site-packages (from -r requirements.txt (line 2)) (3.2.7)
Requirement already satisfied: pytz==2021.1 in /usr/local/lib/python3.9/site-packages (from -r requirements.txt (line 3)) (2021.1)
Requirement already satisfied: sqlparse==0.4.1 in /usr/local/lib/python3.9/site-packages (from -r requirements.txt (line 4)) (0.4.1)
WARNING: Running pip as root will break packages and permissions. You should install packages reliably by using venv: https://pip.pypa.io/warnings/venv
WARNING: You are using pip version 21.1.2; however, version 21.2.4 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.

   ____              ____
  |  _ \  _____   __/ ___| _ __   __ _  ___ ___
  | | | |/ _ \ \ / /\___ \| '_ \ / _` |/ __/ _ \
  | |_| |  __/\ V /  ___) | |_) | (_| | (_|  __/
  |____/ \___| \_/  |____/| .__/ \__,_|\___\___|
                          |_|

Welcome to your development container!

This is how you can work with it:
- Run `python main.py` to build the application
- Files will be synchronized between your local machine and this container
- Some ports will be forwarded, so you can access this container on your local machine via localhost:


 Image   ImageSelector   LabelSelector   Ports (Local:Remote)  
         username/app                    8000:8000             

root@mysite-7b856bb78b-2ztpf-devspace:/app# 

This is the DevSpace.yaml:

version: v1beta10

# `vars` specifies variables which may be used as ${VAR_NAME} in devspace.yaml
vars:
- name: IMAGE
  value: username/app

# `deployments` tells DevSpace how to deploy this project
deployments:
- name: mysite
  # This deployment uses `helm` but you can also define `kubectl` deployments or kustomizations
  helm:
    # We are deploying the so-called Component Chart: https://devspace.sh/component-chart/docs
    componentChart: true
    # Under `values` we can define the values for this Helm chart used during `helm install/upgrade`
    # You may also use `valuesFiles` to load values from files, e.g. valuesFiles: ["values.yaml"]
    values:
      containers:
      - image: ${IMAGE} # Use the value of our `${IMAGE}` variable here (see vars above)
      service:
        ports:
        - port: 8000

# `dev` only applies when you run `devspace dev`
dev:
  # `dev.ports` specifies all ports that should be forwarded while `devspace dev` is running
  # Port-forwarding lets you access your application via localhost on your local machine
  ports:
  - imageSelector: ${IMAGE} # Select the Pod that runs our `${IMAGE}`
    forward:
    - port: 8000

  # `dev.open` tells DevSpace to open certain URLs as soon as they return HTTP status 200
  # Since we configured port-forwarding, we can use a localhost address here to access our application
  open:
  - url: http://localhost:8000

  # `dev.sync` configures a file sync between our Pods in k8s and your local project files
  sync:
  - imageSelector: ${IMAGE} # Select the Pod that runs our `${IMAGE}`
    excludePaths:
    - .git/
    uploadExcludePaths:
    - Dockerfile

  # `dev.terminal` tells DevSpace to open a terminal as a last step during `devspace dev`
  terminal:
    imageSelector: ${IMAGE} # Select the Pod that runs our `${IMAGE}`
    # With this optional `command` we can tell DevSpace to run a script when opening the terminal
    # This is often useful to display help info for new users or perform initial tasks (e.g. installing dependencies)
    # DevSpace has generated an example ./devspace_start.sh file in your local project - Feel free to customize it!
    command:
    - ./devspace_start.sh

  # Since our Helm charts and manifests deployments are often optimized for production,
  # DevSpace let's you swap out Pods dynamically to get a better dev environment
  replacePods:
  - imageSelector: ${IMAGE} # Select the Pod that runs our `${IMAGE}`
    # Since the `${IMAGE}` used to start our main application pod may be distroless or not have any dev tooling, let's replace it with a dev-optimized image
    # DevSpace provides a sample image here but you can use any image for your specific needs
    replaceImage: loftsh/python:latest
    # Besides replacing the container image, let's also apply some patches to the `spec` of our Pod
    # We are overwriting `command` + `args` for the first container in our selected Pod, so it starts with `sleep 9999999`
    # Using `sleep 9999999` as PID 1 (instead of the regular ENTRYPOINT), allows you to start the application manually
    patches:
    - op: replace
      path: spec.containers[0].command
      value:
      - sleep
    - op: replace
      path: spec.containers[0].args
      value:
      - "9999999"
    - op: remove
      path: spec.containers[0].securityContext

# `profiles` lets you modify the config above for different environments (e.g. dev vs production)
profiles:
  # This profile is called `production` and you can use it for example using: devspace deploy -p production
  # We generally recommend to use the base config without any profiles as optimized for development (e.g. image build+push is disabled)
- name: production
# This profile adds our image to the config so that DevSpace will build, tag and push our image before the deployment
  merge:
    images:
      app:
        image: ${IMAGE} # Use the value of our `${IMAGE}` variable here (see vars above)
        dockerfile: ./Dockerfile

This is the Dockerfile:

FROM python:3.8-slim-buster

# Create project directory (workdir)
WORKDIR /app

# Add requirements.txt to WORKDIR and install dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt

# Add source code files to WORKDIR
ADD . .

# Application port (optional)
EXPOSE 8000

# Container start command
# It is also possible to override this in devspace.yaml via images.*.cmd
CMD ["manage.py", "runserver", "8000"]

Both DevSpace UI and kubectl get pods show the Pod is running.

This is deploying to minikube using the Docker driver.

Nothing else is running on port 8000 and I've also tried 8080 by changing every occurrence of 8000 to 8080.

The Django project is only what you get when you run: django-admin startproject mysite. Nothing fancy.

It isn't clear to me what I'm doing wrong here. Suggestions?

EDIT:

Also, meant to point out on M1 Mac. Not sure if it could be related but worth pointing out.

Tried with docker-desktop cluster and minikube thinking it could be a potential issue with minikube. Same issue.

Also, tried the Quick Start guide here using a DevSpace provided project with the same results:

https://devspace.sh/cli/docs/quickstart

-- cjones
devspace
django
docker
kubernetes
minikube

1 Answer

9/10/2021

Kind of important step being left off of the Django tutorial. After running devspace dev, you need to run in the devspace CLI that comes up:

./manage.py runserver

This will launch the application in browser.

-- cjones
Source: StackOverflow