Garantindo escalabilidade em aplicações produtivas
Dica: escreva suas app base nas linguagens que você mais usa e evolua ela.
FROM python:3.7.2-alpine3.7
RUN pip install pipenv
COPY Pipfile Pipfile
COPY Pipfile.lock Pipfile.lock
RUN pipenv install --system --deploy --ignore-pipfile
COPY . /app
WORKDIR /app
CMD ["gunicorn", "app.main:app", "-w", "4", "-b", "0.0.0.0:5000", "-t", "1000"]
docker build . -t k8s-flask-app
version: "3"
services:
web:
build:
context: .
command: gunicorn app.main:app --workers=4 --bind 0.0.0.0:5000 --reload
volumes:
- .:/app
ports:
- "5000:5000"
docker-compose up -d
docker tag <image-id> <docker-hub-username>/k8s-flask-app docker push <docker-hub-username>/k8s-flask-app
Pods são a menor unidade computacional que roda dentro de um cluster Kubernetes.
Quando falamos de ReplicaSet, queremos garantir a disponibilidade de um número fixo e específico de pods.
Um deployment é o estado desejado de seus Pods e ReplicaSets.
Permitem que a gente tenha configurações desacopladas de uma aplicação contêinerizada.
Um secret é um objeto que contém dados sensíveis como senhas, tokens ou chaves.
apiVersion: v1
kind: ConfigMap
metadata:
name: k8s-flask-app-env
data:
NOT_SO_SECRET_VARIABLE: $(NOT_SO_SECRET_VARIABLE)
apiVersion: v1
kind: Secret
metadata:
name: k8s-flask-app-secrets
type: Opaque
stringData:
SECRET_VARIABLE: $(SECRET_VARIABLE)
apiVersion: v1
kind: Service
metadata:
name: k8s-flask-app
spec:
ports:
- port: 5000
targetPort: 5000
protocol: TCP
name: http
selector:
app: k8s-flask-app
type: NodePort
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: k8s-flask-app
spec:
backend:
serviceName: k8s-flask-app
servicePort: 5000
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: k8s-flask-app
labels:
app: k8s-flask-app
spec:
replicas: 8
selector:
matchLabels:
app: k8s-flask-app
template:
metadata:
labels:
app: k8s-flask-app
spec:
containers:
- name: k8s-flask-app
image: biancarosa/k8s-flask-app
envFrom:
- configMapRef:
name: k8s-flask-app-env
- secretRef:
name: k8s-flask-app-secrets
ports:
- containerPort: 5000
minikube start kubectl apply -f kubernetes/configmap.yaml kubectl apply -f kubernetes/secrets.yaml kubectl apply -f kubernetes/deployment.yaml
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
stages:
- stage: Test
jobs:
- job: Test
steps:
- task: UsePythonVersion@0
displayName: 'Use Python 3.7'
inputs:
versionSpec: '3.7'
architecture: 'x64'
- script: pip install pipenv
displayName: 'Install pipenv'
- script: pipenv install --system --dev --deploy --ignore-pipfile
displayName: 'Install dependencies'
- script: make coverage
displayName: 'Run tests'
- script: make lint
displayName: 'Run lint'
- task: PublishTestResults@2
condition: succeededOrFailed()
inputs:
testResultsFiles: '**/test-*.xml'
testRunTitle: 'Publish test results for Python 3.7'
- task: PublishCodeCoverageResults@1
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.xml'
reportDirectory: '$(System.DefaultWorkingDirectory)/**/htmlcov'
- stage: BuildAndPush
jobs:
- job: BuildAndPush
steps:
- task: Docker@2
inputs:
containerRegistry: 'dockerhub'
repository: 'biancarosa/k8s-flask-app'
command: 'buildAndPush'
Dockerfile: 'Dockerfile'
tags: |
$(Build.BuildId)
latest
- stage: Deploy
jobs:
- job: Deploy
steps:
- task: Kubernetes@1
inputs:
connectionType: 'Kubernetes Service Connection'
kubernetesServiceEndpoint: 'k8s-flask-app-cluster'
command: 'apply'
arguments: '-f kubernetes/configmap.yaml'
secretType: 'dockerRegistry'
containerRegistryType: 'Azure Container Registry'
- task: Kubernetes@1
inputs:
connectionType: 'Kubernetes Service Connection'
kubernetesServiceEndpoint: 'k8s-flask-app-cluster'
command: 'apply'
arguments: '-f kubernetes/secrets.yaml'
secretType: 'dockerRegistry'
containerRegistryType: 'Azure Container Registry'
- task: Kubernetes@1
inputs:
connectionType: 'Kubernetes Service Connection'
kubernetesServiceEndpoint: 'k8s-flask-app-cluster'
command: 'apply'
arguments: '-f kubernetes/deployment.yaml'
secretType: 'dockerRegistry'
containerRegistryType: 'Azure Container Registry'