Google Kubernetes Engine (GKE)
1 - Présentation
GKE est le service Kubernetes managé de Google Cloud, offrant une expérience Kubernetes native avec des fonctionnalités avancées.
2 - Autopilot vs Standard
| Aspect | Autopilot | Standard |
|---|---|---|
| Gestion nodes | Vous | |
| Pricing | Par pod | Par node |
| Scaling | Automatique | Configurable |
| Sécurité | Renforcée | Configurable |
| Customisation | Limitée | Totale |
| Maintenance | Zéro | Vous |
2.1 Choisir Autopilot si
- Vous voulez zéro gestion d'infrastructure
- Workloads standard sans besoins spéciaux
- Priorité à la simplicité
2.2 Choisir Standard si
- Vous avez besoin de GPU/TPU
- Configuration node spécifique
- Contrôle total sur les nodes
3 - Créer un cluster
3.1 Cluster Autopilot
# Créer un cluster Autopilot
gcloud container clusters create-auto mon-cluster \
--region=europe-west1 \
--project=mon-projet
3.2 Cluster Standard
# Créer un cluster Standard
gcloud container clusters create mon-cluster \
--region=europe-west1 \
--num-nodes=3 \
--machine-type=e2-standard-4 \
--enable-autoscaling \
--min-nodes=1 \
--max-nodes=10 \
--enable-autorepair \
--enable-autoupgrade
3.3 Se connecter au cluster
# Obtenir les credentials
gcloud container clusters get-credentials mon-cluster \
--region=europe-west1 \
--project=mon-projet
# Vérifier
kubectl get nodes
4 - Déployer une application
4.1 Manifests Kubernetes
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: app
image: europe-west1-docker.pkg.dev/mon-projet/images/my-app:v1.0.0
ports:
- containerPort: 8080
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 512Mi
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
---
apiVersion: v1
kind: Service
metadata:
name: my-app
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8080
selector:
app: my-app
4.2 Déployer
kubectl apply -f deployment.yaml
# Vérifier
kubectl get pods
kubectl get services
5 - Workload Identity
Accès sécurisé aux services GCP depuis les pods.
5.1 Activer Workload Identity
# Sur le cluster (déjà activé sur Autopilot)
gcloud container clusters update mon-cluster \
--region=europe-west1 \
--workload-pool=mon-projet.svc.id.goog
5.2 Configurer le Service Account
# Créer un GSA
gcloud iam service-accounts create my-app-sa
# Donner les permissions
gcloud projects add-iam-policy-binding mon-projet \
--member="serviceAccount:[email protected]" \
--role="roles/storage.objectViewer"
# Lier KSA au GSA
gcloud iam service-accounts add-iam-policy-binding \
[email protected] \
--role="roles/iam.workloadIdentityUser" \
--member="serviceAccount:mon-projet.svc.id.goog[default/my-app-ksa]"
5.3 Utiliser dans le pod
apiVersion: v1
kind: ServiceAccount
metadata:
name: my-app-ksa
annotations:
iam.gke.io/gcp-service-account: my-app-sa@mon-projet.iam.gserviceaccount.com
---
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
serviceAccountName: my-app-ksa
6 - Ingress et HTTPS
6.1 GKE Ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
annotations:
kubernetes.io/ingress.global-static-ip-name: "my-static-ip"
networking.gke.io/managed-certificates: "my-cert"
spec:
rules:
- host: app.example.com
http:
paths:
- path: /*
pathType: ImplementationSpecific
backend:
service:
name: my-app
port:
number: 80
6.2 Managed Certificate
apiVersion: networking.gke.io/v1
kind: ManagedCertificate
metadata:
name: my-cert
spec:
domains:
- app.example.com
7 - CI/CD avec Cloud Build
# cloudbuild.yaml
steps:
# Build
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', '${_IMAGE}', '.']
# Push
- name: 'gcr.io/cloud-builders/docker'
args: ['push', '${_IMAGE}']
# Deploy to GKE
- name: 'gcr.io/cloud-builders/gke-deploy'
args:
- 'run'
- '--filename=kubernetes/'
- '--location=europe-west1'
- '--cluster=mon-cluster'
- '--image=${_IMAGE}'
substitutions:
_IMAGE: 'europe-west1-docker.pkg.dev/$PROJECT_ID/images/my-app:$COMMIT_SHA'
8 - Monitoring
8.1 Cloud Monitoring intégré
GKE envoie automatiquement les métriques à Cloud Monitoring :
- CPU/Memory par pod et node
- Network traffic
- Disk usage
8.2 Dashboard GKE
# Voir les métriques
gcloud monitoring dashboards list
# Activer les métriques Kubernetes
gcloud container clusters update mon-cluster \
--region=europe-west1 \
--monitoring=SYSTEM,WORKLOAD
Résumé
Dans ce chapitre, nous avons appris :
- La différence entre Autopilot et Standard
- La création de clusters
- Le déploiement d'applications
- Workload Identity pour la sécurité
- Ingress et HTTPS
- L'intégration CI/CD
Prochaine étape
Dans le prochain chapitre, nous verrons Cloud Run.
→ Chapitre suivant : Cloud Run