Skip to content
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
2 changes: 2 additions & 0 deletions 2022-08-01-getting-started-with-k8s/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
venv
__pycache__
11 changes: 11 additions & 0 deletions 2022-08-01-getting-started-with-k8s/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
47 changes: 47 additions & 0 deletions 2022-08-01-getting-started-with-k8s/README.md
Original file line number Diff line number Diff line change
@@ -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 <IMAGE_TAG> .

docker push -t <IMAGE_TAG>
```

## Deploy to Kubernetess:

```bash
export KUBECONFIG=<PATH_TO_KUBE_CONFIG>

kubectl apply -f .

kubectl get pods
```

### Validation and Debugging

```bash
kubectl port-forward <POD_NAME> 8080:80

kubectl exec -it <POD_NAME> -- bash
```
Empty file.
10 changes: 10 additions & 0 deletions 2022-08-01-getting-started-with-k8s/app/main.py
Original file line number Diff line number Diff line change
@@ -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')}"}
30 changes: 30 additions & 0 deletions 2022-08-01-getting-started-with-k8s/kubernetes/deployment.yaml
Original file line number Diff line number Diff line change
@@ -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"
16 changes: 16 additions & 0 deletions 2022-08-01-getting-started-with-k8s/kubernetes/ingress.yaml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions 2022-08-01-getting-started-with-k8s/kubernetes/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Service
metadata:
name: fast-api
spec:
selector:
app: fast-api
ports:
- protocol: TCP
port: 80
targetPort: 80
2 changes: 2 additions & 0 deletions 2022-08-01-getting-started-with-k8s/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fastapi~=0.79
uvicorn~=0.18