This project sets up a lightweight Kubernetes cluster using Vagrant and VirtualBox. It provisions:
- One master node (
k8s-master
) that manages the cluster - Two worker nodes (
k8s-worker-1
,k8s-worker-2
) that run containerized workloads - An embedded ETCD cluster running on the master node for storing Kubernetes cluster data
All virtual machines have the necessary dependencies installed, including:
- Docker container runtime
- Kubernetes components (
kubeadm
,kubectl
,kubelet
) - Network configurations and optimizations
- ETCD, which is configured using the following configuration file:
/etc/kubernetes/pki/etcd/ca.crt /etc/kubernetes/pki/etcd/server.crt /etc/kubernetes/pki/etcd/server.key
The master node is initialized with kubeadm
, and Flannel is deployed as the pod network. Worker nodes are automatically configured to join the cluster.
To set up this Kubernetes project locally, first, clone the repository:
git clone https://github.com/DanielDimitrov1/k8s-vm-environment-lab.git
Navigate to the project directory and start the virtual machines using Vagrant:
cd k8s-vm-environment-lab
vagrant up
Once the setup is complete, follow the steps below to configure the cluster.
-
Access the Master Node
Connect to the master node using SSH.vagrant ssh master
-
Generate the Join Command
Create a token that worker nodes will use to join the cluster.sudo kubeadm token create --print-join-command
Copy the output.
-
Join Worker Nodes
Use the generated token to connect each worker node to the cluster.vagrant ssh worker-1 sudo <paste_command_from_master_node>
-
Verify Kubelet Status on the Master Node
Ensure the Kubelet service is running on the master node.sudo systemctl status kubelet
If it's inactive, start it:
sudo systemctl start kubelet
-
Check if the
.kube
Directory Exists
Verify if the kubeconfig file is available forkubectl
to work.ls $HOME/.kube/config
If the directory is missing, create it:
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
-
Verify Node Status
Check if all nodes have successfully joined the cluster.kubectl get nodes
- Assign Worker Roles to Nodes
Label worker nodes to indicate their role in the cluster.kubectl label node k8s-worker-1 node-role.kubernetes.io/worker=worker kubectl label node k8s-worker-2 node-role.kubernetes.io/worker=worker
-
Check the ETCD Cluster Status
Verify that the ETCD cluster is healthy and operational.kubectl exec -it -n kube-system etcd-k8s-master -- \ etcdctl --cacert /etc/kubernetes/pki/etcd/ca.crt \ --cert /etc/kubernetes/pki/etcd/server.crt \ --key /etc/kubernetes/pki/etcd/server.key endpoint health
-
List ETCD Cluster Members
Display the list of all ETCD members in the cluster.kubectl exec -it -n kube-system etcd-k8s-master -- \ etcdctl --cacert /etc/kubernetes/pki/etcd/ca.crt \ --cert /etc/kubernetes/pki/etcd/server.crt \ --key /etc/kubernetes/pki/etcd/server.key member list