This project provides a fully automated way to deploy a multi-node Kubernetes cluster (1 master + 2 workers) locally using Kubeadm, Ansible, and Vagrant.
Definition
Kubeadm is a Kubernetes tool that simplifies cluster bootstrapping. It handles:
- Initialization of the master node (
kubeadm init) - Configuration of control plane components
- Generation of join tokens for worker nodes
Why it's important:
- Standardizes cluster creation
- Ensures best practices for configuration
- Reduces manual setup errors
Definition
Containerd is a lightweight container runtime that manages containers on each node.
In Kubernetes:
- Acts as the runtime for pods (replacing Docker since K8s 1.24)
- Communicates with Kubelet via CRI
- Handles image pulling, container lifecycle, and storage
Advantages:
- Native CRI support
- Stable and production-ready
Definition
Calico is a network plugin (CNI) for Kubernetes that provides:
- Pod-to-pod networking
- Network policies for security
- IP address management
Why it's important:
- Allows pods to communicate across nodes
- Enforces network isolation rules
- Works well with containerd
Definition
Kubelet is the agent running on every node that:
- Communicates with the Kubernetes API Server
- Manages pod lifecycle on the node
- Interfaces with container runtimes via CRI
Key role:
- Ensures pods run as expected
- Monitors node health and reports to master
- Vagrant = 2.4.9
- VirtualBox = 7.2
- Ansible = 2.19
.
βββ group_vars
β βββ all.yml # Global variables for all hosts
βββ hosts.ini # Ansible inventory (masters + workers)
βββ playbook.yml # Main playbook for the Kubernetes cluster
βββ README.md # Project documentation
βββ roles
β βββ calico
β β βββ tasks
β β βββ main.yml # Tasks to install/configure Calico
β βββ common
β β βββ tasks
β β βββ main.yml # Common tasks for all hosts
β βββ containerd
β β βββ files
β β β βββ crictl.yaml # crictl configuration file
β β βββ handlers
β β β βββ main.yml # Handlers (e.g., restart service) for containerd
β β βββ tasks
β β β βββ main.yml # Tasks to install/configure containerd
β β βββ templates
β β βββ config.toml.j2 # Template for containerd config.toml
βΒ Β βββ kubernetes
βΒ Β βββ tasks
βΒ Β βββ main.yml # Tasks to init master and join workers
βββ Vagrantfile # VM definitions (master + workers)vagrant up- Master Node: 192.168.56.10
- Worker Nodes: 192.168.56.11, 192.168.56.12
- OS: Debian Bullseye
- Resources: 2 CPUs, 2GB RAM per VM
ansible-playbook -i ansible/inventory.ini ansible/k8s-setup.yamlPlaybook Tasks:
- Install required packages (apt-transport-https, curl, ca-certificates, etc.)
- Add Docker and Kubernetes GPG keys & repositories
- Install containerd and configure SystemdCgroup
- Install and lock kubeadm, kubelet, kubectl
- Load kernel modules (overlay, br_netfilter) and configure sysctl
- Configure crictl
- Disable swap
- Start kubelet service
- Initialize master node with kubeadm init
- Deploy Calico CNI
- Generate and distribute join token
- Join worker nodes to cluster
On the master node:
kubectl get nodesNAME STATUS ROLES AGE VERSION
master Ready control-plane 61m v1.34.1
worker1 Ready <none> 61m v1.34.1
worker2 Ready <none> 61m v1.34.1kubectl get pods -Akubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system calico-kube-controllers-b45f49df6-xft7k 1/1 Running 0 4m47s
kube-system calico-node-dv2gs 1/1 Running 0 4m41s
kube-system calico-node-j6hvz 1/1 Running 0 4m48s
kube-system calico-node-vqq89 1/1 Running 0 4m42s
kube-system coredns-66bc5c9577-4ht8w 1/1 Running 0 4m47s
kube-system coredns-66bc5c9577-jhqwq 1/1 Running 0 4m47s
kube-system etcd-master 1/1 Running 0 4m53s
kube-system kube-apiserver-master 1/1 Running 0 4m53s
kube-system kube-controller-manager-master 1/1 Running 0 4m53s
kube-system kube-proxy-87w6s 1/1 Running 0 4m42s
kube-system kube-proxy-8nwkf 1/1 Running 0 4m48s
kube-system kube-proxy-blk5s 1/1 Running 0 4m41s
kube-system kube-scheduler-master 1/1 Running 0 4m53sDΓ©ployer une application test
cat > test-nginx.yaml << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-test
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: NodePort
EOFAppliquer la configuration
kubectl apply -f test-nginx.yamlVΓ©rifier le dΓ©ploiement
> kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-test 3/3 3 3 12s> kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-test-54fc99c8d-jcqcg 1/1 Running 0 20s 10.244.235.129 worker1 <none> <none>
nginx-test-54fc99c8d-mrqv5 1/1 Running 0 20s 10.244.189.66 worker2 <none> <none>
nginx-test-54fc99c8d-ww57l 1/1 Running 0 20s 10.244.189.65 worker2 <none> <none>> kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 64m
nginx-service NodePort 10.97.183.191 <none> 80:32498/TCP 29sObtenir le NodePort :
NODE_PORT=$(kubectl get svc nginx-service -o jsonpath='{.spec.ports[0].nodePort}')
echo $NODE_PORT> curl http://192.168.56.10:$NODE_PORT
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>Vagrant VM Management
# Stop VMs
vagrant halt# Restart VMs
vagrant reload# Destroy VMs
vagrant destroy -f# Check VM status
vagrant status# Destroy cluster and VMs
vagrant destroy -f- The next tasks planned for this project include deploying Velero or another backup system for the Kubernetes cluster.
- The backup system will be provisioned using Terraform, allowing for infrastructure-as-code management of backup resources.