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

Challenge 01 #2

Open
wants to merge 19 commits into
base: master
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
20 changes: 20 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Create environment file

on:
push:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/[email protected]

- name: 'Create env file'
working-directory: challenge-01/frontend.yml
run: |
touch .env
echo API_KEY=${{ secrets.API_KEY }} >> .env
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/2022ChallengeOnfire.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions challenge-01/SOLUCION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Challenge 01

## 1. Dockerfile
En el directorio backend se encuentra el archivo Dockerfile el cual contiene las instrucciones para construir la imagen del backend, para construir la imagen se debe ejecutar el siguiente comando en la raiz del directorio backend

```docker build -t backend_pokemon .
docker run -d -p 8000:8000 backend_pokemon
```

En el directorio frontend se encuentra el otro archivo necesario para el despliegue del frontend, el cual es el Dockerfile, para construir la imagen se debe ejecutar el siguiente comando en la raiz del directorio frontend

```docker build -t frontend_pokemon .
docker run -d -p 3000:3000 frontend_pokemon
```

## 2. Docker-compose
En el directorio raiz se encuentra el archivo docker-compose.yml el cual contiene las instrucciones para construir las imagenes y desplegar los contenedores del backend y frontend, para construir las imagenes y desplegar los contenedores se debe ejecutar el siguiente comando en la raiz del directorio raiz

```
docker-compose up -d
```

## Variables de entorno
En la ruta frontend se crea el archivo .env el cual contiene las variables de entorno para el frontend, en este archivo se encuentran las variables de entorno para el backend, estas variables de entorno se utilizan para indicarle al frontend a que url se debe conectar para obtener los datos de los pokemones, estas variables de entorno son las siguientes

```REACT_APP_URL_DEVELOPMENT=http://localhost:8000
REACT_APP_URL_PRODUCTION=http://localhost:8000
```

Estas variables estan protegidas y son creadas gracias a un Github Action que se encarga de crearlas una vez se hace push a la rama master.

## Terraform

En la ruta terraform se encuentran diferentes archivos que se encargan de crear los recursos necesarios para desplegar la aplicacion en AWS, para desplegar la aplicacion en AWS se debe ejecutar el siguiente comando en la raiz del directorio terraform

```
terraform init
terraform plan
terraform apply
```

Esto creara un cluster en EKS, una subnet, y un security group, ademas de crear un archivo de configuracion para kubectl, el cual se debe ejecutar para poder conectarse al cluster de EKS

```
aws eks --region us-east-1 update-kubeconfig --name pokemon
```

Una vez conectado al cluster de EKS se debe ejecutar el siguiente comando para desplegar los pods y servicios de la aplicacion

```
kubectl apply -f kubernetes/
```

## Kubernetes

Una vez desplegada la aplicacion en AWS se debe ejecutar el siguiente comando para obtener la direccion ip del load balancer

```
kubectl get svc
```
10 changes: 10 additions & 0 deletions challenge-01/backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM python:3.8-slim-buster

WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

COPY . .
EXPOSE 8000

CMD ["python", "main.py"]
13 changes: 13 additions & 0 deletions challenge-01/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: '3.7'
services:
frontend:
build: ./frontend/
ports:
- 3000:3000
depends_on:
- backend

backend:
build: ./backend/
ports:
- 8000:8000
2 changes: 2 additions & 0 deletions challenge-01/frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
node_modules
10 changes: 10 additions & 0 deletions challenge-01/frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM node:16.13.2-alpine3.15 AS base

WORKDIR /app

COPY package.json .
RUN npm install
COPY . .

EXPOSE 3000
CMD [ "npm", "run", "start" ]
12 changes: 12 additions & 0 deletions challenge-01/kubernetes/backend-service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Service
metadata:
name: backend-service
spec:
type: LoadBalancer
selector:
app: backend
ports:
- protocol: TCP
port: 8000
targetPort: 8000
22 changes: 22 additions & 0 deletions challenge-01/kubernetes/backend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: backend
labels:
app: backend
spec:
replicas: 1
selector:
matchLabels:
app: backend
template:
metadata:
labels:
app: backend
spec:
containers:
- name: fastapi-backend
image: waltergsteven/backend_pokemon:latest
imagePullPolicy: Always
ports:
- containerPort: 8000
15 changes: 15 additions & 0 deletions challenge-01/kubernetes/frontend-service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Service
metadata:
name: frontend-service
spec:
type: LoadBalancer
selector:
app: frontend
ports:
- protocol: TCP
port: 80
targetPort: 3000
tls:
- secretName: example-tls
termination: edge
22 changes: 22 additions & 0 deletions challenge-01/kubernetes/frontend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend
labels:
app: frontend
spec:
replicas: 1
selector:
matchLabels:
app: frontend
template:
metadata:
labels:
app: frontend
spec:
containers:
- name: react-frontend
image: waltergsteven/frontend_pokemon:latest
imagePullPolicy: Always
ports:
- containerPort: 3000
31 changes: 31 additions & 0 deletions challenge-01/terraform/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*

# Crash log files
crash.log

# Ignore any .tfvars files that are generated automatically for each Terraform run. Most
# .tfvars files are managed as part of configuration and so should be included in
# version control.
#
# example.tfvars

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json

# Include override files you do wish to add to version control using negated pattern
#
# !example_override.tf

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*

*.terraform.lock.hcl*
7 changes: 7 additions & 0 deletions challenge-01/terraform/eip.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "aws_eip" "nat1" {
depends_on = [aws_internet_gateway.igw] # This is needed to ensure the EIP is created after the IGW
}

resource "aws_eip" "nat2" {
depends_on = [aws_internet_gateway.igw] # Use depends_on to ensure the EIP is created after the IGW
}
Loading