Home > Software design >  How do I get Kubernetes service to open my django application on a web browser using local host?
How do I get Kubernetes service to open my django application on a web browser using local host?

Time:10-31

I have been trying to get my kubernetes to launch my web application on a browser through my local host. When I try to open local host it times out and I have tried using minikube service --url and that also does not work. All of my deployment, and service pods are running. I have also tried port forward and changing the type to nodeport. I have provided my yaml, docker, and svc code.

apiVersion: v1
kind: Service
metadata:
  name: mywebsite
spec:
  type: LoadBalancer
  selector:
    app: mywebsite
  ports:
  - protocol: TCP
    name: http
    port: 8743
    targetPort: 5000


---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mywebsite
spec:
  selector:
    matchLabels:
      app: mywebsite
  template:
    metadata:
      labels:
        app: mywebsite
    spec:
      containers:
      - name: mywebsite
        image: mywebsite
        imagePullPolicy: Never
        ports:
        - containerPort: 5000
        resources:
          requests:
            cpu: 100m
            memory: 250Mi
          limits:
            memory: "2Gi"
            cpu: "500m"   
# For more information, please refer to https://aka.ms/vscode-docker—python
FROM python:3.8-slim-buster

EXPOSE 8000
# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

# Install pip requirements
COPY requirements.txt .
RUN python -m pip install -r requirements.txt

WORKDIR lapp
COPY . .

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode—docker-python—configure-containers
RUN adduser -u 5678 --disab1ed-password --gecos "" appuser && chown -R appuser /app
USER appuser

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Name:                     mywebsite
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=mywebsite
Type:                     LoadBalancer
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.99.161.241
IPs:                      10.99.161.241
Port:                     http 8743/TCP
TargetPort:               5000/TCP
NodePort:                 http 32697/TCP
Endpoints:                172.17.0.3:5000
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

CodePudding user response:

it's due to your container running on port 8000

But your service is forwarding the traffic to 5000

apiVersion: v1
kind: Service
metadata:
  name: mywebsite
spec:
  type: LoadBalancer
  selector:
    app: mywebsite
  ports:
  - protocol: TCP
    name: http
    port: 8743
    targetPort: **8000**

deployment should be

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mywebsite
spec:
  selector:
    matchLabels:
      app: mywebsite
  template:
    metadata:
      labels:
        app: mywebsite
    spec:
      containers:
      - name: mywebsite
        image: mywebsite
        imagePullPolicy: Never
        ports:
        - containerPort: **8000**
        resources:
          requests:
            cpu: 100m
            memory: 250Mi
          limits:
            memory: "2Gi"
            cpu: "500m"

You need to change the targetPort in SVC

and containerPort in Deployment

Or else

change the

EXPOSE 8000 to 5000 and command to run the application on 5000

CMD ["python", "manage.py", "runserver", "0.0.0.0:5000"]

Dont forget to docker build one more time after the above changes.

  • Related