Conteneurisation

Kubernetes

Cheatsheet Kubernetes — kubectl, pods, deployments, services et configuration

kubernetesk8skubectlpodsdeploymentsservices

kubectl — commandes essentielles

Informations du cluster

$kubectl cluster-info
$kubectl get nodes -o wide
NAME          STATUS   ROLES           AGE   VERSION   INTERNAL-IP
control-01    Ready    control-plane   30d   v1.31.0   10.0.0.10
worker-01     Ready    <none>          30d   v1.31.0   10.0.0.11
worker-02     Ready    <none>          30d   v1.31.0   10.0.0.12
$kubectl api-resources
Liste toutes les ressources API disponibles

Pods

$kubectl get pods -n default
$kubectl get pods -A
Tous les pods dans tous les namespaces
$kubectl describe pod mon-pod
$kubectl logs mon-pod -f --tail=100
Suivre les 100 dernières lignes de logs
$kubectl logs mon-pod -c sidecar
Logs d'un conteneur spécifique dans un pod multi-conteneur
$kubectl exec -it mon-pod -- /bin/sh
$kubectl port-forward pod/mon-pod 8080:80
Accéder au pod localement sur localhost:8080
$kubectl delete pod mon-pod

Deployments

apiVersion: apps/v1
kind: Deployment
metadata:
  name: api
  labels:
    app: api
spec:
  replicas: 3
  selector:
    matchLabels:
      app: api
  template:
    metadata:
      labels:
        app: api
    spec:
      containers:
        - name: api
          image: mon-api:1.0
          ports:
            - containerPort: 3000
          resources:
            requests:
              cpu: 100m
              memory: 128Mi
            limits:
              cpu: 500m
              memory: 256Mi
          readinessProbe:
            httpGet:
              path: /healthz
              port: 3000
            initialDelaySeconds: 5
            periodSeconds: 10
          livenessProbe:
            httpGet:
              path: /healthz
              port: 3000
            initialDelaySeconds: 15
            periodSeconds: 20
$kubectl apply -f deployment.yaml
$kubectl get deployments
$kubectl rollout status deployment/api
$kubectl rollout history deployment/api
$kubectl rollout undo deployment/api
Rollback à la version précédente
$kubectl scale deployment/api --replicas=5
$kubectl set image deployment/api api=mon-api:2.0
Mise à jour de l'image (rolling update)

Services

apiVersion: v1
kind: Service
metadata:
  name: api-svc
spec:
  selector:
    app: api
  ports:
    - port: 80
      targetPort: 3000
  type: ClusterIP
TypeUsage
ClusterIPInterne au cluster (défaut)
NodePortExpose sur un port de chaque node (30000-32767)
LoadBalancerCrée un LB cloud externe
ExternalNameAlias DNS vers un service externe
$kubectl get svc
$kubectl expose deployment api --port=80 --target-port=3000 --type=ClusterIP

ConfigMaps & Secrets

$kubectl create configmap app-config --from-literal=ENV=production --from-literal=LOG_LEVEL=info
$kubectl create secret generic db-creds --from-literal=password=S3cur3P4ss
# Utilisation dans un pod
spec:
  containers:
    - name: app
      envFrom:
        - configMapRef:
            name: app-config
        - secretRef:
            name: db-creds

Namespaces

$kubectl create namespace staging
$kubectl get all -n staging
$kubectl config set-context --current --namespace=staging
Changer le namespace par défaut

Debugging

$kubectl get events --sort-by=.metadata.creationTimestamp
$kubectl top pods
CPU et mémoire des pods (metrics-server requis)
$kubectl top nodes
$kubectl run debug --image=busybox -it --rm -- sh
Pod éphémère pour debugger le réseau
$kubectl get pods -o jsonpath='{.items[*].status.phase}'
Extraire des champs spécifiques avec JSONPath

Contextes & Clusters

$kubectl config get-contexts
$kubectl config use-context production
$kubectl config current-context