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

ManifestSolution #25

Open
wants to merge 4 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
10 changes: 10 additions & 0 deletions .infrastructure/busybox.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: busybox
namespace: todoapp
spec:
containers:
- name: busybox
image: curlimages/curl
command: ["sleep", "1000"]
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
25 changes: 25 additions & 0 deletions .infrastructure/todoapp-pod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: v1
kind: Pod
metadata:
name: todoapp
namespace: todoapp
labels:
app: todoapp
spec:
containers:
- name: todoapp
image: kagerou4649/todoapp:3.0.0
ports:
- containerPort: 8080
livenessProbe:
httpGet:
path: api/liveness
port: 8080
initialDelaySeconds: 40
periodSeconds: 5
readinessProbe:
httpGet:
path: api/ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
61 changes: 14 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,55 +1,22 @@
# Django ToDo list
# Applying all manifests:
kubectl apply -f ./infrastructure

This is a todo list web application with basic features of most web apps, i.e., accounts/login, API, and interactive UI. To do this task, you will need:
# A command to test application using the port-forward command:
kubectl port-forward pod/todoapp 8081:8080 -n todoapp

- CSS | [Skeleton](http://getskeleton.com/)
- JS | [jQuery](https://jquery.com/)
# Validate performance:
http://localhost

## Explore
# Testing application using busybox container:
# Check ip address of todoapp.pod:

Try it out by installing the requirements (the following commands work only with Python 3.8 and higher, due to Django 4):
kubectl get pods -n todoapp -o wide

```
pip install -r requirements.txt
```
# Use this prompt to connect to the pod:

Create a database schema:
kubectl -n todoapp exec -it busybox -- sh

```
python manage.py migrate
```
# Test this prompt:

And then start the server (default is http://localhost:8000):

```
python manage.py runserver
```

Now you can browse the [API](http://localhost:8000/api/) or start on the [landing page](http://localhost:8000/).

## Task

Create a kubernetes manifest for a pod which will containa ToDo app container:

1. Fork this repository.
1. Create a simple `Dockerfile` for the ToDo application
7. Add readiness endpoint code to `api` of the appliation (modify `api/views.py` and `api/urls.py`)
1. Add liveness endpoint cide to `api` of the application (modify `api/views.py` and `api/urls.py`)
1. Build your image and push to your personal Docker Hub account into the `todoapp` repository with the `3.0.0` tag (`todoapp:3.0.0`)
1. All manifests should be located under `.infrastructure` folder
1. Create a `manifest` which can be used to create a namespace. File should be named `namespace.yml` and should contain the following content:
```
apiVersion: v1
kind: Namespace
metadata:
name: todoapp
```
8. Creata a pod `manifest` which will start a `busyboxplus:curl` container in a cluster. File should be named `busybox.yml`.
1. Create a pod `manifest` which will be using previously created image with tag `{yourname}/todoapp:3.0.0`. File should be named `todoapp-pod.yml`.
1. ToDo app pod `manifest` should have a readiness probe configured
1. ToDo app pod `manifest` should have a liveness probe configured
1. `README.md` file should contain instructions on how to apply all manifests
1. `README.md` file should contain instructions on how to test ToDo application using the `port-forward` command
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.
curl (ip of todoapp.pod):8080
# Replace (ip of todoapp.pod) with the IP received via "kubectl get pods -n todoapp -o wide"
22 changes: 22 additions & 0 deletions src/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
ARG PYTHON_VERSION=3.8

FROM python:${PYTHON_VERSION} AS builder

WORKDIR /app

COPY . .

FROM python:${PYTHON_VERSION} as run

ENV PYTHONUNBUFFERED=1

WORKDIR /app

COPY --from=builder /app .

RUN pip install --upgrade pip && \
pip install -r requirements.txt

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("liveness/", views.liveness_check, name="liveness-check"),
path("ready/", views.readiness_check, name="readiness-check"),
]
14 changes: 13 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

start_time = time.time()
startup_period = 40

class IsCreatorOrReadOnly(permissions.BasePermission):
"""
Object-level permission to only allow owners of an object to edit it.
Expand Down Expand Up @@ -55,4 +58,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)
serializer.save(creator=creator)

def liveness_check():
return HttpResponse("Live", status=200)

def readiness_check():
if time.time() < start_time + startup_period:
return HttpResponse("Not Ready", status=503)
else:
return HttpResponse("Ready", status=200)