diff --git a/.gitignore b/.gitignore index c998ebcd..18c805b8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ - -*/*/__pycache__/* -*/*/*/__pycache__/* +.idea +DS_Store diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..13566b81 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/devops_todolist_kubernetes_task_3_write_a_manifest.iml b/.idea/devops_todolist_kubernetes_task_3_write_a_manifest.iml new file mode 100644 index 00000000..08d21393 --- /dev/null +++ b/.idea/devops_todolist_kubernetes_task_3_write_a_manifest.iml @@ -0,0 +1,15 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 00000000..86d6f0be --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,19 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 00000000..105ce2da --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..360db3c8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.infrastructure/busybox.yaml b/.infrastructure/busybox.yaml new file mode 100644 index 00000000..60c4027d --- /dev/null +++ b/.infrastructure/busybox.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: busybox + namespace: todoapp +spec: + containers: + - name: busybox + image: ikulyk404/busyboxplus:curl + imagePullPolicy: IfNotPresent + args: + - sleep + - "1000" diff --git a/.infrastructure/namespace.yaml b/.infrastructure/namespace.yaml new file mode 100644 index 00000000..b6ade867 --- /dev/null +++ b/.infrastructure/namespace.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: todoapp diff --git a/.infrastructure/todoapp-pod.yaml b/.infrastructure/todoapp-pod.yaml new file mode 100644 index 00000000..d79526f3 --- /dev/null +++ b/.infrastructure/todoapp-pod.yaml @@ -0,0 +1,28 @@ +apiVersion: v1 +kind: Pod +metadata: + name: todoapp + labels: + role: todoapp +spec: + containers: + - name: todoapp + image: vpcomtek/todoapp:3.0.0 + imagePullPolicy: IfNotPresent + ports: + - name: todoapp + containerPort: 8080 + protocol: TCP + 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..41bd69ee --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM python:3.8 + +WORKDIR /app + +COPY ./src /app + +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"] diff --git a/INSTRUCTION.md b/INSTRUCTION.md new file mode 100644 index 00000000..46362334 --- /dev/null +++ b/INSTRUCTION.md @@ -0,0 +1,32 @@ +# Instruction +### How to Apply All Manifests +``` +kubectl apply -f .infrastructure/namespace.yml +kubectl apply -f .infrastructure/busybox.yml +kubectl apply -f .infrastructure/todoapp-pod.yml +``` +### Testing ToDo Application Using port-forward +``` +kubectl port-forward pod/todoapp -n todoapp 8000:8000 +``` +- Visit http://localhost:8000 to access the application. +### Testing Application Using busyboxplus:curl +``` +kubectl exec -it busybox -n todoapp -- curl http://todoapp:8000 +kubectl exec -it busybox -n todoapp -- curl http://todoapp:8000/readiness +kubectl exec -it busybox -n todoapp -- curl http://todoapp:8000/liveness +``` +or + +1. Retrieve the IP address of the TodoApp pod: +``` +kubectl get pods -n todoapp -o wide +``` +2. Access the BusyBox shell: +``` +kubectl -n todoapp exec -it busybox -- sh +``` +3. Send a curl request to the TodoApp: +``` +curl :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..bf53651e 100644 --- a/src/api/views.py +++ b/src/api/views.py @@ -55,4 +55,17 @@ 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) + +start_time = time.time() +delay_time = 40 + +def readiness (request): + if time.time() < start_time + delay_time: + return HttpResponse("Not ready", status=503) + else: + return HttpResponse("Ready", status=200) + +def liveness(request): + return HttpResponse("Alive", status=200) +