From 65e030f78d450bc263aa5e1194eaa88bd58081be Mon Sep 17 00:00:00 2001 From: Nazar Makivchuk Date: Thu, 14 Nov 2024 19:08:20 +0200 Subject: [PATCH 1/3] add manifests to k8s todolist --- .infrastructure/busybox.yml | 12 ++++++++++++ .infrastructure/namespace.yml | 4 ++++ .infrastructure/todoapp-pod.yml | 23 +++++++++++++++++++++++ Dockerfile | 14 ++++++++++++++ src/api/urls.py | 4 +++- src/api/views.py | 15 ++++++++++++++- 6 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 .infrastructure/busybox.yml create mode 100644 .infrastructure/namespace.yml create mode 100644 .infrastructure/todoapp-pod.yml create mode 100644 Dockerfile diff --git a/.infrastructure/busybox.yml b/.infrastructure/busybox.yml new file mode 100644 index 00000000..91180e33 --- /dev/null +++ b/.infrastructure/busybox.yml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: busybox + namespace: todoapp +spec: + containers: + - name: busybox + image: ikulyk404/busyboxplus:curl + args: + - sleep + - "1000" diff --git a/.infrastructure/namespace.yml b/.infrastructure/namespace.yml new file mode 100644 index 00000000..6a37edac --- /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..7ef2db5f --- /dev/null +++ b/.infrastructure/todoapp-pod.yml @@ -0,0 +1,23 @@ +apiVersion: v1 +kind: Pod +metadata: + name: todoapp + namespace: todoapp +spec: + containers: + - name: todoapp + image: nmakivchuk01/todoapp:3.0.0 + ports: + - containerPort: 8080 + livenessProbe: + httpGet: + path: api/liveness + port: 8080 + initialDelaySeconds: 60 + periodSeconds: 5 + readinessProbe: + httpGet: + path: api/readiness + port: 8080 + initialDelaySeconds: 5 + periodSeconds: 5 \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..1516c120 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +ARG PYTHON_VERSION=3.8 + +FROM python:${PYTHON_VERSION} + +WORKDIR /app + +COPY ./src . + +RUN pip install --upgrade pip && \ + pip install -r requirements.txt + +EXPOSE 8080 + +ENTRYPOINT ["sh", "-c", "python manage.py migrate && python manage.py runserver 0.0.0.0:8080"] \ No newline at end of file diff --git a/src/api/urls.py b/src/api/urls.py index 201425f8..ba9c8bb3 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, name="readiness"), + path("liveness/", views.liveness, name="liveness") ] diff --git a/src/api/views.py b/src/api/views.py index 659a9dfb..d13f5507 100644 --- a/src/api/views.py +++ b/src/api/views.py @@ -8,6 +8,10 @@ from django.utils import timezone import time +startTime = time.time() +delayTime = 40 + + class IsCreatorOrReadOnly(permissions.BasePermission): """ Object-level permission to only allow owners of an object to edit it. @@ -55,4 +59,13 @@ 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 liveness(request): + return HttpResponse("live", status=200) + +def readiness(request): + if time.time() < startTime + delayTime: + return HttpResponse("Waiting for ...", status=503) + else: + return HttpResponse("Ready", status=200) From 4fb0479486e3e1d45a68e3df8032c914cbe0a9bc Mon Sep 17 00:00:00 2001 From: Nazar Makivchuk Date: Fri, 15 Nov 2024 22:36:08 +0200 Subject: [PATCH 2/3] update Readme file and fix EoF --- .infrastructure/namespace.yml | 2 +- .infrastructure/todoapp-pod.yml | 2 +- Dockerfile | 2 +- README.md | 28 ++++++++++++++++++++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/.infrastructure/namespace.yml b/.infrastructure/namespace.yml index 6a37edac..8279a843 100644 --- a/.infrastructure/namespace.yml +++ b/.infrastructure/namespace.yml @@ -1,4 +1,4 @@ apiVersion: v1 kind: Namespace metadata: - name: todoapp \ No newline at end of file + name: todoapp diff --git a/.infrastructure/todoapp-pod.yml b/.infrastructure/todoapp-pod.yml index 7ef2db5f..66a70a99 100644 --- a/.infrastructure/todoapp-pod.yml +++ b/.infrastructure/todoapp-pod.yml @@ -20,4 +20,4 @@ spec: path: api/readiness port: 8080 initialDelaySeconds: 5 - periodSeconds: 5 \ No newline at end of file + periodSeconds: 5 diff --git a/Dockerfile b/Dockerfile index 1516c120..e296370b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,4 +11,4 @@ RUN pip install --upgrade pip && \ EXPOSE 8080 -ENTRYPOINT ["sh", "-c", "python manage.py migrate && python manage.py runserver 0.0.0.0:8080"] \ No newline at end of file +ENTRYPOINT ["sh", "-c", "python manage.py migrate && python manage.py runserver 0.0.0.0:8080"] diff --git a/README.md b/README.md index ba75c4b9..d37246bd 100644 --- a/README.md +++ b/README.md @@ -53,3 +53,31 @@ metadata: 1. `README.md` file should contain instructions on how to test application using the `busyboxplus:curl` container 1. Create PR with your changes and attach it for validation on a platform. + +# Instructions on how to apply all manifests +``` +kubectl apply -f ./.infrastructure/namespace.yml + +kubectl apply -f ./.infrastructure/todoapp-pod.yml + +kubectl apply -f ./.infrastructure/busybox.yml +``` + +# Instructions on how to test ToDo application +``` +kubectl port-forward pod/todoapp 8080:8080 -n todoapp +``` +# Instructions on how to test application +1. To get the IP address of Todo App: +``` +kubectl get pods -n todoapp -o wide +``` +2. To get access to busybox shell: +``` +kubectl -n todoapp exec -it busybox -- sh +``` +3. To send curl request: +``` +curl [TodoApp_IP]:8080 +``` + From d0abf565d00cb16aaae8bd063921f206e212498d Mon Sep 17 00:00:00 2001 From: Nazar Makivchuk Date: Fri, 15 Nov 2024 22:37:39 +0200 Subject: [PATCH 3/3] fix EoF --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index d37246bd..b6a34d67 100644 --- a/README.md +++ b/README.md @@ -80,4 +80,3 @@ kubectl -n todoapp exec -it busybox -- sh ``` curl [TodoApp_IP]:8080 ``` -