From 77c0f47e091b0752e240b076b126ec98bb0e46d6 Mon Sep 17 00:00:00 2001 From: fredisson11 Date: Tue, 17 Dec 2024 01:17:22 +0100 Subject: [PATCH 1/3] Solution --- .gitignore | 3 ++- src/api/urls.py | 4 +++- src/api/views.py | 17 ++++++++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index c998ebcd..79afb141 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ - +*.sqlite3 */*/__pycache__/* */*/*/__pycache__/* +.venv diff --git a/src/api/urls.py b/src/api/urls.py index 201425f8..f5cac7e4 100644 --- a/src/api/urls.py +++ b/src/api/urls.py @@ -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") ] diff --git a/src/api/views.py b/src/api/views.py index 659a9dfb..e05df4ef 100644 --- a/src/api/views.py +++ b/src/api/views.py @@ -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. @@ -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) \ No newline at end of file + 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) + \ No newline at end of file From 72c508c9420a9edb10fef12bac1887d077381399 Mon Sep 17 00:00:00 2001 From: fredisson11 Date: Tue, 17 Dec 2024 01:20:47 +0100 Subject: [PATCH 2/3] Solution --- INSTRUCTION.md | 85 +++++++++++++++++++++++++++++++++++++++++++++++ src/.dockerignore | 1 + src/Dockerfile | 13 ++++++++ 3 files changed, 99 insertions(+) create mode 100644 INSTRUCTION.md create mode 100644 src/.dockerignore create mode 100644 src/Dockerfile diff --git a/INSTRUCTION.md b/INSTRUCTION.md new file mode 100644 index 00000000..01136407 --- /dev/null +++ b/INSTRUCTION.md @@ -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. \ No newline at end of file diff --git a/src/.dockerignore b/src/.dockerignore new file mode 100644 index 00000000..60615839 --- /dev/null +++ b/src/.dockerignore @@ -0,0 +1 @@ +*.sqlite3 diff --git a/src/Dockerfile b/src/Dockerfile new file mode 100644 index 00000000..64cb09e0 --- /dev/null +++ b/src/Dockerfile @@ -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" ] From 1d7b1e5a101000c526b8366e6692767c8a4b217c Mon Sep 17 00:00:00 2001 From: fredisson11 Date: Tue, 17 Dec 2024 01:23:06 +0100 Subject: [PATCH 3/3] Solution --- .infrastructure/busybox.yml | 11 +++++++++++ .infrastructure/namespace.yml | 4 ++++ .infrastructure/todoapp-pod.yml | 28 ++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 .infrastructure/busybox.yml create mode 100644 .infrastructure/namespace.yml create mode 100644 .infrastructure/todoapp-pod.yml diff --git a/.infrastructure/busybox.yml b/.infrastructure/busybox.yml new file mode 100644 index 00000000..6a92776d --- /dev/null +++ b/.infrastructure/busybox.yml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Pod + +metadata: + name: busybox + namespace: todoapp + +spec: + containers: + - name: busybox + image: ikulyk404/busyboxplus:curl \ No newline at end of file diff --git a/.infrastructure/namespace.yml b/.infrastructure/namespace.yml new file mode 100644 index 00000000..2a84bbc2 --- /dev/null +++ b/.infrastructure/namespace.yml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: todoapp \ No newline at end of file diff --git a/.infrastructure/todoapp-pod.yml b/.infrastructure/todoapp-pod.yml new file mode 100644 index 00000000..8157d5cb --- /dev/null +++ b/.infrastructure/todoapp-pod.yml @@ -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 \ No newline at end of file