Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #52

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

*.sqlite3
*/*/__pycache__/*
*/*/*/__pycache__/*
.venv
11 changes: 11 additions & 0 deletions .infrastructure/busybox.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Pod

metadata:
name: busybox
namespace: todoapp

spec:
containers:
- name: busybox
image: ikulyk404/busyboxplus:curl
4 changes: 4 additions & 0 deletions .infrastructure/namespace.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: todoapp
28 changes: 28 additions & 0 deletions .infrastructure/todoapp-pod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
apiVersion: v1
kind: Pod

metadata:
name: todoapp-pod
namespace: todoapp

spec:
containers:
- name: todoapp
image: fredisson11/todoapp:3.0.0

ports:
- containerPort: 8080

livenessProbe:
httpGet:
path: /liveness
port: 8080
initialDelaySeconds: 5
periodSeconds: 5

readinessProbe:
httpGet:
path: /readiness
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
85 changes: 85 additions & 0 deletions INSTRUCTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
## 1. How to Apply All Manifests

To apply all manifests for your application, follow these steps:

1. Create a namespace for the application:
```bash
kubectl apply -f .infrastructure/namespace.yml
```

2. Apply the manifest to create the `busybox` container (for testing):
```bash
kubectl apply -f .infrastructure/busybox.yml
```

3. Apply the pod manifest for your ToDo application:
```bash
kubectl apply -f .infrastructure/todoapp-pod.yml
```

This manifest will create a pod with the container for the ToDo application using the `fredisson11/todoapp:3.0.0` image.

4. Verify that the pod has been created:
```bash
kubectl get pods -n todoapp
```

If the pod is running correctly, its status should be `Running`.

---

## 2. How to Test the ToDo Application Using `port-forward`

1. Use the `kubectl port-forward` command to access the service:
```bash
kubectl port-forward pod/todoapp-pod 8080:8080 -n todoapp
```

2. Open your browser and go to:
```
http://localhost:8080
```

This will allow you to test the ToDo application interface locally, through port 8080.

---

## 3. How to Test the Application Using the `busyboxplus:curl` Container

1. Use the `busyboxplus:curl` container to check the availability of the `/readiness` and `/liveness` endpoints.

2. For **liveness** check:
```bash
kubectl run -i --tty --rm busybox --image=ikulyk404/busyboxplus:curl --restart=Never -- curl http://todoapp-pod:8080/liveness
```

3. For **readiness** check:
```bash
kubectl run -i --tty --rm busybox --image=ikulyk404/busyboxplus:curl --restart=Never -- curl http://todoapp-pod:8080/readiness
```

A successful response for each of these requests should be:

```
Ready
```

If you get a different status or message, it means the pod is not ready or is not functioning properly.

---

## 4. How to Test Kubernetes Deployment

After applying all manifests and testing, verify the status of your pod:

1. Use the command to check the pod status:
```bash
kubectl get pods -n todoapp
```

2. Check the pod logs:
```bash
kubectl logs todoapp-pod -n todoapp
```

The logs will help you understand if there are any issues with the application.
1 change: 1 addition & 0 deletions src/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.sqlite3
13 changes: 13 additions & 0 deletions src/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.12

WORKDIR /app

COPY . .

RUN pip install --upgrade pip && \
pip install -r requirements.txt && \
python manage.py migrate

EXPOSE 8080

ENTRYPOINT [ "python", "manage.py", "runserver", "0.0.0.0:8080" ]
4 changes: 3 additions & 1 deletion src/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@

app_name = "api"
urlpatterns = [
path("", include(router.urls))
path("", include(router.urls)),
path("readiness/", views.readiness_view, name="readiness"),
path("liveness/", views.liveness_view, name="liveness")
]
17 changes: 16 additions & 1 deletion src/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from django.utils import timezone
import time

from django.db import connections, OperationalError


class IsCreatorOrReadOnly(permissions.BasePermission):
"""
Object-level permission to only allow owners of an object to edit it.
Expand Down Expand Up @@ -55,4 +58,16 @@ class TodoViewSet(viewsets.ModelViewSet):
def perform_create(self, serializer):
user = self.request.user
creator = user if user.is_authenticated else None
serializer.save(creator=creator)
serializer.save(creator=creator)

def readiness_view():
try:
db_conn = connections["default"]
db_conn.ensure_connection()
return HttpResponse("Ready", status=200)
except OperationalError:
return HttpResponse("NotReady", status=503)

def liveness_view():
return HttpResponse("Alive", status=200)