Kubernetes Cluster Setup On Ubuntu VirtualBox: A Quick Guide

by Team 61 views
Kubernetes Cluster Setup on Ubuntu VirtualBox: A Quick Guide

Setting up a Kubernetes cluster on Ubuntu using VirtualBox is a fantastic way to get hands-on experience with container orchestration without needing dedicated hardware. This guide walks you through each step, ensuring you have a functional cluster ready for experimentation and learning. Let's dive in and get our hands dirty with some Kubernetes magic!

Prerequisites

Before we start, there are a few things you'll need to have ready:

  • VirtualBox: Make sure you have VirtualBox installed on your machine. You can download it from the official VirtualBox website.
  • Ubuntu ISO: Download the latest Ubuntu Server ISO image. This will be the base OS for our Kubernetes nodes.
  • Basic Linux Knowledge: Familiarity with basic Linux commands will be helpful.
  • Adequate Hardware Resources: Ensure your machine has enough RAM and CPU cores to run multiple VMs. A minimum of 8GB RAM and 4 CPU cores is recommended.

Step 1: Creating Ubuntu Virtual Machines in VirtualBox

First, we need to create the virtual machines that will form our Kubernetes cluster. We'll create at least three VMs: one master node and two worker nodes.

  1. Create the First VM (Master Node):
    • Open VirtualBox and click "New".
    • Name the VM (e.g., k8s-master). Select "Linux" as the type and "Ubuntu (64-bit)" as the version.
    • Allocate at least 2GB of RAM. For better performance, consider allocating 4GB.
    • Create a virtual hard disk. VDI is a good choice. Dynamically allocated is fine to save space.
    • Allocate at least 20GB of disk space.
  2. Install Ubuntu on the Master Node:
    • Start the VM. VirtualBox will prompt you to select a start-up disk. Choose the Ubuntu ISO you downloaded.
    • Follow the on-screen instructions to install Ubuntu. During installation:
      • Choose a username and password.
      • Select "Install OpenSSH server" for remote access.
      • Use the entire disk and set up LVM if you prefer.
  3. Clone the VM for Worker Nodes:
    • Once Ubuntu is installed and the VM is running, shut it down.
    • Right-click on the VM in VirtualBox and select "Clone".
    • Name the clone k8s-worker1. Choose "Full clone" to create an independent copy.
    • Repeat the cloning process to create k8s-worker2.

Now you should have three VMs: k8s-master, k8s-worker1, and k8s-worker2. Let's move on to configuring the network.

Step 2: Configuring the Network

Networking is crucial for Kubernetes to function correctly. We need to ensure that our VMs can communicate with each other. We'll use a bridged network adapter so that each VM gets an IP address from your home network, but we can also configure internal networking if you prefer.

  1. Set Static IP Addresses:

    • Start each VM.
    • Log in using the username and password you created during installation.
    • Edit the network configuration file:
    sudo nano /etc/netplan/01-network-manager-all.yaml
    
    • Configure a static IP address for each VM. Replace the example IP addresses with ones suitable for your network.
    network:
      version: 2
      renderer: networkd
      ethernets:
        enp0s3:
          dhcp4: no
          addresses: ["192.168.1.100/24"] # Master Node
          gateway4: 192.168.1.1 # Your Router's IP
          nameservers:
            addresses: ["8.8.8.8", "8.8.4.4"]
    
    • Repeat this process for k8s-worker1 (e.g., 192.168.1.101) and k8s-worker2 (e.g., 192.168.1.102).
    • Apply the network configuration:
    sudo netplan apply
    
  2. Update the /etc/hosts File:

    • On each VM, edit the /etc/hosts file:
    sudo nano /etc/hosts
    
    • Add entries for each VM so they can resolve each other by hostname:
    192.168.1.100 k8s-master
    192.168.1.101 k8s-worker1
    192.168.1.102 k8s-worker2
    
  3. Test Connectivity:

    • From each VM, ping the other VMs by hostname to ensure they can communicate.
    ping k8s-master
    ping k8s-worker1
    ping k8s-worker2
    

With networking configured, our VMs can now talk to each other. Next, we'll install the necessary Kubernetes components.

Step 3: Installing Kubernetes Components

Now comes the exciting part: installing the Kubernetes components! We'll use apt to install kubeadm, kubelet, and kubectl on all three VMs.

  1. Update Package Index and Install Dependencies:

    • On all three VMs, run the following commands:
    sudo apt update
    sudo apt install -y apt-transport-https ca-certificates curl
    
  2. Add the Kubernetes APT Repository:

    curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
    echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
    
  3. Install Kubernetes Components:

    sudo apt update
    sudo apt install -y kubelet kubeadm kubectl
    sudo apt-mark hold kubelet kubeadm kubectl
    

The apt-mark hold command prevents these packages from being accidentally updated, which could cause compatibility issues. With the Kubernetes components installed, we can now initialize the cluster.

Step 4: Initializing the Kubernetes Cluster

We'll initialize the Kubernetes cluster on the master node using kubeadm. This process sets up the control plane components that manage the cluster.

  1. Initialize the Cluster:

    • On the k8s-master VM, run the following command:
    sudo kubeadm init --pod-network-cidr=10.244.0.0/16
    
    • The --pod-network-cidr specifies the IP address range for pods. We're using the range required by Calico, a popular network plugin.
    • This command will output a kubeadm join command. Copy this command. You'll need it to join the worker nodes to the cluster.
  2. Configure kubectl:

    • After kubeadm init completes, follow the instructions to configure kubectl for your user:
    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
    
  3. Install a Pod Network Addon:

    • Kubernetes requires a pod network addon to enable communication between pods. We'll use Calico.
    kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
    

The cluster is now initialized and ready to accept worker nodes. Let's join the worker nodes to the cluster.

Step 5: Joining Worker Nodes to the Cluster

Now, we'll use the kubeadm join command that was outputted during the kubeadm init process to add the worker nodes to the cluster.

  1. Join the Worker Nodes:

    • On both k8s-worker1 and k8s-worker2 VMs, run the kubeadm join command that you copied earlier. It should look something like this:
    sudo kubeadm join <master-ip>:<port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>
    
    • Replace <master-ip>, <port>, <token>, and <hash> with the values from the kubeadm init output.

With the worker nodes joined, the cluster is almost ready. Let's verify the cluster status.

Step 6: Verifying the Cluster

We'll use kubectl on the master node to check the status of the cluster and ensure that all nodes are ready.

  1. Check Node Status:

    • On the k8s-master VM, run the following command:
    kubectl get nodes
    
    • You should see all three nodes (master and two workers) listed. Initially, they might show as NotReady. This is because it takes a few minutes for the pod network to be fully established.
    • Wait a few minutes and run the command again. Eventually, all nodes should show as Ready.
  2. Check Pod Status:

    • Run the following command to check the status of the pods:
    kubectl get pods --all-namespaces
    
    • Ensure that all essential pods are running without errors.

Congratulations! You now have a functioning Kubernetes cluster on Ubuntu using VirtualBox. 🎉

Step 7: Testing the Cluster (Optional)

To ensure everything is working correctly, let's deploy a simple application to the cluster.

  1. Deploy a Sample Application:

    • Create a deployment and a service for a simple Nginx web server.
    kubectl create deployment nginx --image=nginx
    kubectl expose deployment nginx --port=80 --type=NodePort
    
  2. Access the Application:

    • Get the node port for the Nginx service:
    kubectl get service nginx
    
    • Look for the NodePort value. It will be a port number between 30000 and 32767.
    • Access the application by opening a web browser and navigating to http://<worker-node-ip>:<nodeport>. Replace <worker-node-ip> with the IP address of one of your worker nodes and <nodeport> with the node port you found.
    • You should see the default Nginx welcome page.

Conclusion

Setting up a Kubernetes cluster on Ubuntu with VirtualBox might seem daunting at first, but by following this step-by-step guide, you can have a fully functional cluster ready for testing and development. Remember, mastering Kubernetes takes time and practice, so don't be afraid to experiment and explore different configurations. You've now got a powerful platform for deploying and managing containerized applications. Happy clustering, guys! I hope you all enjoyed the adventure of setting up a Kubernetes cluster. Keep exploring and containerizing!