Aller au contenu principal

Chapitre 12.2 - CI/CD avec Kubernetes

Objectifs d'Apprentissage​

À la fin de ce chapitre, vous serez capable de:

  • Comprendre les pipelines CI/CD pour Kubernetes
  • Configurer GitHub Actions pour Kubernetes
  • Implémenter GitLab CI/CD
  • Utiliser GitOps avec ArgoCD
  • Automatiser les déploiements
  • Implémenter les stratégies de déploiement

Introduction​

Les pipelines CI/CD automatisent le build, test, et déploiement d'applications dans Kubernetes.


GitHub Actions​

Pipeline Basique​

name: Deploy to Kubernetes

on:
push:
branches: [main]

jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Build Docker image
run: |
docker build -t my-app:${{ github.sha }} .
docker tag my-app:${{ github.sha }} my-registry/my-app:${{ github.sha }}

- name: Push to registry
run: |
echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
docker push my-registry/my-app:${{ github.sha }}

- name: Deploy to Kubernetes
uses: azure/k8s-deploy@v4
with:
manifests: |
k8s/deployment.yaml
k8s/service.yaml
images: |
my-registry/my-app:${{ github.sha }}
kubectl-version: 'latest'

GitLab CI/CD​

.gitlab-ci.yml​

stages:
- build
- test
- deploy

build:
stage: build
script:
- docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA

test:
stage: test
script:
- docker run $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA npm test

deploy:
stage: deploy
script:
- kubectl set image deployment/my-app app=$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
only:
- main

GitOps avec ArgoCD​

Installation​

# Installer ArgoCD
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

Application ArgoCD​

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: my-app
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/user/my-app
targetRevision: main
path: k8s
destination:
server: https://kubernetes.default.svc
namespace: production
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=true

Stratégies de Déploiement​

Rolling Update​

spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0

Blue/Green​

Déployer une nouvelle version en parallèle, puis basculer le trafic.

Canary​

Déployer progressivement à un pourcentage d'utilisateurs.


Bonnes Pratiques​

1. Tests Automatiques​

Exécuter les tests avant chaque déploiement.

2. Environnements​

Séparer dev, staging, production.

3. Approbations​

Exiger des approbations pour la production.

4. Rollback​

Avoir un plan de rollback rapide.

5. Monitoring​

Surveiller les déploiements en temps réel.


Résumé​

Dans ce chapitre, vous avez appris:

CI/CD: Automatisation du build, test, et déploiement
GitHub Actions: Pipelines CI/CD intégrés
GitLab CI: Pipelines avec .gitlab-ci.yml
GitOps: ArgoCD pour déploiements déclaratifs
Stratégies: Rolling, Blue/Green, Canary
Bonnes pratiques: Tests, environnements, approbations, rollback


Prochaines Étapes​

Chapitre 12.3: Environnements
Lab 12.2: Autoscaling Horizontal et Vertical
Lab 12.3: GitOps avec ArgoCD


Chapitre créé le: Décembre 2024