diff --git a/2022-08-01-getting-started-with-k8s/.gitignore b/2022-08-01-getting-started-with-k8s/.gitignore new file mode 100644 index 0000000..7f93ebf --- /dev/null +++ b/2022-08-01-getting-started-with-k8s/.gitignore @@ -0,0 +1,2 @@ +venv +__pycache__ diff --git a/2022-08-01-getting-started-with-k8s/Dockerfile b/2022-08-01-getting-started-with-k8s/Dockerfile new file mode 100644 index 0000000..3831a93 --- /dev/null +++ b/2022-08-01-getting-started-with-k8s/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3.9 + +WORKDIR /code + +COPY ./requirements.txt /code/requirements.txt + +RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt + +COPY ./app /code/app + +CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"] \ No newline at end of file diff --git a/2022-08-01-getting-started-with-k8s/README.md b/2022-08-01-getting-started-with-k8s/README.md new file mode 100644 index 0000000..ca83009 --- /dev/null +++ b/2022-08-01-getting-started-with-k8s/README.md @@ -0,0 +1,47 @@ +# Getting Started with Kubernetes + +Video: https://www.youtube.com/watch?v=XltFOyGanYE + +## Python Application + +### Create a virtual environment: +```bash +python3 -m venv ./venv +source ./venv/bin/activate +``` + +### Install the Dependencies +```bash +pip install -r requirements.txt +``` + +### Start the App +```bash +uvicorn main:app --reload +``` + +## Containerize the Application + +```bash +docker build -t . + +docker push -t +``` + +## Deploy to Kubernetess: + +```bash +export KUBECONFIG= + +kubectl apply -f . + +kubectl get pods +``` + +### Validation and Debugging + +```bash +kubectl port-forward 8080:80 + +kubectl exec -it -- bash +``` \ No newline at end of file diff --git a/2022-08-01-getting-started-with-k8s/app/__init__.py b/2022-08-01-getting-started-with-k8s/app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/2022-08-01-getting-started-with-k8s/app/main.py b/2022-08-01-getting-started-with-k8s/app/main.py new file mode 100644 index 0000000..9109378 --- /dev/null +++ b/2022-08-01-getting-started-with-k8s/app/main.py @@ -0,0 +1,10 @@ +from fastapi import FastAPI + +import os + +app = FastAPI() + + +@app.get("/") +def read_root(): + return {"Hello": f"From: {os.environ.get('HOSTNAME', 'DEFAULT_ENV')}"} diff --git a/2022-08-01-getting-started-with-k8s/kubernetes/deployment.yaml b/2022-08-01-getting-started-with-k8s/kubernetes/deployment.yaml new file mode 100644 index 0000000..074ea18 --- /dev/null +++ b/2022-08-01-getting-started-with-k8s/kubernetes/deployment.yaml @@ -0,0 +1,30 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: fast-api + labels: + app: fast-api +spec: + replicas: 3 + selector: + matchLabels: + app: fast-api + template: + metadata: + labels: + app: fast-api + spec: + containers: + - name: fast-api + image: sidpalas/k8s-getting-started:0.0.3 + ports: + - containerPort: 80 + resources: + requests: + cpu: 200m + memory: 300Mi + limits: + memory: 400Mi + env: + - name: ENV + value: "CIVO" \ No newline at end of file diff --git a/2022-08-01-getting-started-with-k8s/kubernetes/ingress.yaml b/2022-08-01-getting-started-with-k8s/kubernetes/ingress.yaml new file mode 100644 index 0000000..f649222 --- /dev/null +++ b/2022-08-01-getting-started-with-k8s/kubernetes/ingress.yaml @@ -0,0 +1,16 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: fast-api +spec: + rules: + - host: k8s.devopsdirective.com + http: + paths: + - path: / + pathType: Exact + backend: + service: + name: fast-api + port: + number: 80 \ No newline at end of file diff --git a/2022-08-01-getting-started-with-k8s/kubernetes/service.yaml b/2022-08-01-getting-started-with-k8s/kubernetes/service.yaml new file mode 100644 index 0000000..03f1796 --- /dev/null +++ b/2022-08-01-getting-started-with-k8s/kubernetes/service.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Service +metadata: + name: fast-api +spec: + selector: + app: fast-api + ports: + - protocol: TCP + port: 80 + targetPort: 80 \ No newline at end of file diff --git a/2022-08-01-getting-started-with-k8s/requirements.txt b/2022-08-01-getting-started-with-k8s/requirements.txt new file mode 100644 index 0000000..ac98127 --- /dev/null +++ b/2022-08-01-getting-started-with-k8s/requirements.txt @@ -0,0 +1,2 @@ +fastapi~=0.79 +uvicorn~=0.18 \ No newline at end of file