Kubernetes Fundamentals: A Complete Guide to Orchestrating Your Containers
Discover how Kubernetes is transforming the deployment and management of containerized applications. A practical guide with concrete examples.
InSkillCoach
Kubernetes Fundamentals: A Complete Guide to Orchestrating Your Containers
Kubernetes (K8s) has become the standard platform for container orchestration at scale. This guide introduces the fundamental concepts of Kubernetes and shows you how to use it effectively to manage your containerized applications.
What Is Kubernetes?
Kubernetes is an open-source platform created by Google to automate the deployment, scaling, and management of containerized applications. It provides a framework for running distributed systems resiliently.
Why Kubernetes?
- Automated deployment: Deployments and updates with zero downtime
- Self-healing: Automatic restart of failed containers
- Horizontal scaling: Automatic capacity adjustment
- Load balancing: Traffic distribution across instances
- Storage orchestration: Management of persistent storage
- Configuration management: Separation of code and configuration
- Multi-cloud support: Runs on all major cloud providers
Kubernetes Architecture
The Kubernetes architecture consists of several key components:
Control Plane
- API Server: Entry point for all REST commands
- etcd: Key-value database that stores the cluster state
- Scheduler: Assigns pods to nodes
- Controller Manager: Maintains the desired state of the cluster
- Cloud Controller Manager: Interfaces with cloud provider APIs
Nodes
- Kubelet: Agent that runs on each node and makes sure containers are running
- Kube-proxy: Maintains network rules on nodes
- Container Runtime: Software responsible for running containers (Docker, containerd, etc.)
Installing Kubernetes
Minikube (for local development)
# Installation sur Linux
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
# Démarrer un cluster Kubernetes local
minikube start
# Vérifier l'état
minikube status
kubeadm (for production clusters)
# Installation des prérequis
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl
# Ajouter la clé GPG et le dépôt Kubernetes
sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
# Installer kubeadm, kubelet et kubectl
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
# Initialiser le cluster (sur le nœud maître)
sudo kubeadm init --pod-network-cidr=192.168.0.0/16
# Configurer kubectl
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl (command-line tool)
# Installation sur Linux
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
# Vérifier l'installation
kubectl version --client
Core Kubernetes Concepts
Pods
The pod is the basic unit of Kubernetes, representing one or more containers that share storage and networking.
# pod.yaml - Exemple de pod simple
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.19
ports:
- containerPort: 80
# Créer un pod
kubectl apply -f pod.yaml
# Lister les pods
kubectl get pods
# Décrire un pod
kubectl describe pod nginx-pod
# Supprimer un pod
kubectl delete pod nginx-pod
Deployments
Deployments manage the lifecycle of pods and ReplicaSets, making updates and rollbacks easier.
# deployment.yaml - Exemple de déploiement
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.19
ports:
- containerPort: 80
# Créer un déploiement
kubectl apply -f deployment.yaml
# Lister les déploiements
kubectl get deployments
# Mettre à jour l'image d'un déploiement
kubectl set image deployment/nginx-deployment nginx=nginx:1.20
# Effectuer un rollback
kubectl rollout undo deployment/nginx-deployment
Services
Services expose applications running on a set of pods.
# service.yaml - Exemple de service
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80
type: ClusterIP # Autres types: NodePort, LoadBalancer, ExternalName
# Créer un service
kubectl apply -f service.yaml
# Lister les services
kubectl get services
# Décrire un service
kubectl describe service nginx-service
Namespaces
Namespaces provide a mechanism for isolating groups of resources within a cluster.
# namespace.yaml - Exemple de namespace
apiVersion: v1
kind: Namespace
metadata:
name: development
# Créer un namespace
kubectl apply -f namespace.yaml
# Lister les namespaces
kubectl get namespaces
# Créer une ressource dans un namespace spécifique
kubectl apply -f pod.yaml -n development
ConfigMaps and Secrets
They let you separate configuration from containers.
# configmap.yaml - Exemple de ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
database.host: "db.example.com"
database.port: "5432"
# secret.yaml - Exemple de Secret
apiVersion: v1
kind: Secret
metadata:
name: app-secrets
type: Opaque
data:
# Les valeurs doivent être encodées en base64
database.password: cGFzc3dvcmQxMjM= # "password123" encodé en base64
# Créer un ConfigMap
kubectl apply -f configmap.yaml
# Créer un Secret
kubectl apply -f secret.yaml
# Utiliser un ConfigMap dans un pod
spec:
containers:
- name: app
image: app:1.0
envFrom:
- configMapRef:
name: app-config
Using Volumes
Volumes let you store data beyond the lifetime of a pod.
# pvc.yaml - Exemple de PersistentVolumeClaim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: data-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
# pod-with-volume.yaml - Pod utilisant un volume
apiVersion: v1
kind: Pod
metadata:
name: data-pod
spec:
containers:
- name: data-container
image: nginx
volumeMounts:
- mountPath: "/data"
name: data-volume
volumes:
- name: data-volume
persistentVolumeClaim:
claimName: data-pvc
Managing Ingress
Ingress exposes HTTP and HTTPS services from outside the cluster.
# ingress.yaml - Exemple d'Ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: example.com
http:
paths:
- path: /app
pathType: Prefix
backend:
service:
name: app-service
port:
number: 80
# Installer un contrôleur Ingress (exemple avec NGINX)
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.0.0/deploy/static/provider/cloud/deploy.yaml
# Créer un Ingress
kubectl apply -f ingress.yaml
Debugging Applications
Logs and Interactive Shell
# Afficher les logs d'un pod
kubectl logs nginx-pod
# Exécuter une commande dans un pod
kubectl exec -it nginx-pod -- /bin/bash
# Suivre les logs en temps réel
kubectl logs -f nginx-pod
Troubleshooting
# Vérifier l'état d'un pod
kubectl get pod nginx-pod -o wide
# Décrire un pod pour voir les événements
kubectl describe pod nginx-pod
# Vérifier les nœuds
kubectl get nodes
kubectl describe node <node-name>
Resource Management
Resource Limits
# resource-limits.yaml - Exemple de limites de ressources
apiVersion: v1
kind: Pod
metadata:
name: resource-pod
spec:
containers:
- name: app
image: app:1.0
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
Horizontal Pod Autoscaler (HPA)
# hpa.yaml - Exemple d'autoscaling
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: app-deployment
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
# Créer un HPA
kubectl apply -f hpa.yaml
# Lister les HPAs
kubectl get hpa
Security in Kubernetes
RBAC (Role-Based Access Control)
# role.yaml - Exemple de rôle
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list"]
# rolebinding.yaml - Exemple de liaison de rôle
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
namespace: default
subjects:
- kind: User
name: jane
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
Network Policies
# network-policy.yaml - Exemple de politique réseau
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: db-network-policy
spec:
podSelector:
matchLabels:
role: db
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
role: app
ports:
- protocol: TCP
port: 5432
Kubernetes Best Practices
- Use Deployments rather than individual pods
- Define resource limits for all containers
- Configure health probes (liveness and readiness probes)
- Implement horizontal scaling for variable workloads
- Separate environments with namespaces
- Use labels and annotations to organize resources
- Implement network policies to secure communications
- Configure RBAC to restrict access
- Use Secrets for sensitive data
- Set up a backup strategy for etcd
Complementary Tools for Kubernetes
Helm (package manager)
# Installation de Helm
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
chmod 700 get_helm.sh
./get_helm.sh
# Ajouter un dépôt
helm repo add bitnami https://charts.bitnami.com/bitnami
# Installer une application
helm install my-release bitnami/wordpress
Kustomize (configuration management)
# kustomization.yaml - Exemple de configuration Kustomize
resources:
- deployment.yaml
- service.yaml
namespace: my-namespace
commonLabels:
app: my-app
environment: production
# Appliquer une configuration Kustomize
kubectl apply -k ./
Prometheus and Grafana (monitoring)
# Installation via Helm
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm install prometheus prometheus-community/kube-prometheus-stack
Conclusion
Kubernetes has become the standard tool for container orchestration thanks to its flexibility, robustness, and rich ecosystem. Understanding its fundamental concepts is essential for effectively managing modern applications at scale.
By mastering Kubernetes, you will be able to:
- Deploy applications at scale
- Ensure high availability
- Optimize resource utilization
- Simplify operations
- Provide a declarative model for infrastructure management
The Kubernetes learning curve can be steep, but the investment is worth it for any team working with container-based architectures.
About InSkillCoach
Training and technology expert
Coach specializing in advanced technologies and AI, backed by GNeurone Inc.
Certifications:
- AWS Certified Solutions Architect – Professional
- Google Cloud certifications
- Microsoft Certified: DevOps Engineer Expert
- Certified Kubernetes Administrator (CKA)
- CompTIA Security+
Comments
Comments are powered by GitHub Discussions
Sign in with GitHub to join the discussion