Kubernetes On Ubuntu 18.04: A Step-by-Step Installation Guide

by Team 62 views
Kubernetes Installation on Ubuntu 18.04: A Step-by-Step Guide

Hey guys! Want to dive into the world of container orchestration? Kubernetes is the way to go! This guide will walk you through installing Kubernetes on Ubuntu 18.04, step by simple step. Let's get started!

Prerequisites

Before we jump into the installation, let's make sure you have everything you need. This includes:

  • Ubuntu 18.04: You'll need a clean installation of Ubuntu 18.04. A virtual machine (VM) or a physical server will work just fine.
  • User with sudo privileges: Make sure you have a user account with sudo privileges. This will allow you to run commands as an administrator.
  • Internet connection: You'll need an active internet connection to download the necessary packages.
  • Basic Linux knowledge: A basic understanding of Linux commands will be helpful.

Step 1: Update Package Repository and Install Dependencies

First things first, we need to update the package repository and install some essential dependencies. Open your terminal and run the following commands:

sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
  • sudo apt update: This command updates the package lists for upgrades and new package installations. It's always a good idea to run this before installing anything new.
  • sudo apt install -y apt-transport-https ca-certificates curl software-properties-common: This command installs several packages that are required for adding and using HTTPS repositories. Let's break it down:
    • apt-transport-https: Allows apt to access repositories over HTTPS.
    • ca-certificates: Contains a collection of trusted Certificate Authority (CA) certificates.
    • curl: A command-line tool for transferring data with URLs.
    • software-properties-common: Provides scripts for managing software repositories.

The -y flag automatically answers "yes" to any prompts, which speeds up the installation process. These packages are crucial for ensuring that you can securely download and install Kubernetes and its related components. Without these, you might run into issues when adding the Kubernetes repository or verifying the downloaded packages. So, make sure you get these installed correctly before moving on to the next steps. These foundational tools are used extensively in almost any modern Linux server setup, providing the ability to manage packages and interact with remote repositories. Updating the package repository ensures that your system knows about the latest versions of software available, and installing these dependencies ensures that you have the necessary tools to proceed smoothly with the Kubernetes installation. Remember, keeping your system up-to-date and having the right tools installed are crucial for a successful Kubernetes deployment.

Step 2: Add the Kubernetes Repository

Next, we'll add the Kubernetes repository to your system. This will allow you to install Kubernetes packages using apt. Run these commands:

curl -fsSL 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
  • curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -: This command downloads the Kubernetes repository key and adds it to your system's list of trusted keys. This key is used to verify the authenticity of the Kubernetes packages.
    • curl: Downloads the GPG key from the specified URL.
    • -fsSL: These are curl options:
      • -f: Fails silently on server errors.
      • -s: Silent mode.
      • -S: Shows error messages when silent mode is used.
      • -L: Follows redirects.
    • sudo apt-key add -: Adds the downloaded key to the system's trusted keys.
  • echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list: This command adds the Kubernetes repository to your system's list of software sources. The tee command writes the output of echo to both the terminal and the specified file.

Adding the Kubernetes repository is essential because it tells your system where to find the Kubernetes packages. The repository contains all the necessary files for installing Kubernetes components like kubelet, kubeadm, and kubectl. By adding the repository and the corresponding GPG key, you ensure that you're downloading packages from a trusted source and that the packages haven't been tampered with. The kubernetes-xenial part of the repository URL might seem a bit strange since we're on Ubuntu 18.04, but it's intentional. The Kubernetes repository uses xenial (which corresponds to Ubuntu 16.04) for compatibility reasons. Don't worry; it works just fine on Ubuntu 18.04! Without this step, you won't be able to install Kubernetes using apt, and you'll have to resort to other methods, which can be more complicated. So, make sure you add the repository correctly to streamline the installation process. Once the repository is added, your system knows where to look for the Kubernetes packages, and you can proceed with installing the necessary components.

Step 3: Install Kubernetes Components

Now that we've added the Kubernetes repository, we can install the Kubernetes components. We'll install kubelet, kubeadm, and kubectl. Run the following commands:

sudo apt update
sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
  • sudo apt update: Again, we update the package list to include the newly added Kubernetes repository.
  • sudo apt install -y kubelet kubeadm kubectl: This command installs the three main Kubernetes components:
    • kubelet: The agent that runs on each node in the cluster. It's responsible for managing the containers on the node.
    • kubeadm: A tool for bootstrapping Kubernetes clusters. It simplifies the process of setting up a Kubernetes cluster.
    • kubectl: The command-line tool for interacting with the Kubernetes API server. It allows you to manage and control your Kubernetes cluster.
  • sudo apt-mark hold kubelet kubeadm kubectl: This command prevents these packages from being automatically updated. This is important because Kubernetes updates can sometimes introduce breaking changes, and it's best to control when these components are updated.

Installing these components is a crucial step in setting up your Kubernetes cluster. The kubelet is the workhorse that ensures your containers are running as expected on each node. It communicates with the control plane to receive instructions and manage the containers accordingly. The kubeadm tool simplifies the complex process of initializing and joining nodes to the cluster, making it easier to get your cluster up and running. And kubectl is your primary interface for interacting with the Kubernetes API, allowing you to deploy applications, manage resources, and monitor your cluster's health. By holding these packages, you're ensuring that your Kubernetes components remain at the version you've tested and configured. This prevents unexpected issues that might arise from automatic updates. It's generally recommended to manually update these components when you're ready to test and deploy the new versions in a controlled manner. These three components work together to form the foundation of your Kubernetes cluster. Without them, you won't be able to run containers, manage your cluster, or interact with the Kubernetes API. So, make sure you install them correctly and hold them to maintain stability in your cluster. By taking these steps, you're setting yourself up for a successful Kubernetes deployment.

Step 4: Disable Swap

Kubernetes requires swap to be disabled. To disable swap, run the following commands:

sudo swapoff -a
sudo sed -i '/ swap / s/^/#/' /etc/fstab
  • sudo swapoff -a: This command immediately disables all swap spaces.
  • sudo sed -i '/ swap / s/^/#/' /etc/fstab: This command comments out the swap entries in the /etc/fstab file. This prevents swap from being automatically enabled on reboot.

Disabling swap is a critical requirement for Kubernetes because it can interfere with the scheduler's ability to properly allocate resources to pods. When swap is enabled, the operating system might move memory pages to disk, which can cause performance degradation and unpredictable behavior for containerized applications. Kubernetes expects to have full control over memory management, and swap can disrupt this control. By disabling swap, you ensure that Kubernetes can accurately track and allocate memory resources to pods, leading to more stable and predictable performance. The swapoff -a command turns off swap immediately, while the sed command ensures that swap remains disabled after a reboot. Commenting out the swap entries in /etc/fstab prevents the system from automatically enabling swap during startup. If you skip this step, you might encounter issues when initializing your Kubernetes cluster, and the scheduler might not function correctly. So, it's essential to disable swap before proceeding with the cluster initialization. By doing so, you're setting the stage for a smooth and reliable Kubernetes deployment. This is a common requirement for many container orchestration platforms, as they rely on precise memory management for optimal performance. Disabling swap ensures that your Kubernetes cluster operates in a predictable and efficient manner.

Step 5: Initialize the Kubernetes Cluster

Now it's time to initialize the Kubernetes cluster using kubeadm. Run the following command:

sudo kubeadm init

This command will take a few minutes to run. Once it's finished, it will output a kubeadm join command that you'll need to use to join worker nodes to the cluster. Important: Save this command! It will look something like this:

kubeadm join <control-plane-IP>:<port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>
  • sudo kubeadm init: This command initializes the Kubernetes control plane. It sets up the necessary components for managing the cluster, such as the API server, scheduler, and controller manager.

Initializing the Kubernetes cluster with kubeadm init is a pivotal moment in the setup process. This command bootstraps the control plane, which is the brain of your Kubernetes cluster. It configures the API server, scheduler, controller manager, and other essential components that are responsible for managing the cluster's resources and orchestrating container deployments. During the initialization process, kubeadm generates certificates, creates the necessary configuration files, and starts the control plane services. This can take a few minutes, so be patient. Once the initialization is complete, kubeadm will provide you with a kubeadm join command. This command is crucial for adding worker nodes to the cluster. It contains a token and a discovery token CA certificate hash, which are used to securely authenticate the worker nodes and connect them to the control plane. Make sure to save this command in a safe place, as you'll need it later when setting up your worker nodes. If you lose this command, you'll have to regenerate it, which can be a bit of a hassle. The kubeadm init command essentially lays the foundation for your entire Kubernetes cluster. Without it, you won't have a functioning control plane, and you won't be able to deploy any applications. So, make sure you run this command successfully and save the kubeadm join command for future use. This step is the cornerstone of building your Kubernetes environment, and it sets the stage for scaling and managing your containerized applications.

Step 6: Configure kubectl

To use kubectl, you need to configure it to connect to your cluster. Run the following commands:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
  • mkdir -p $HOME/.kube: Creates the .kube directory in your home directory if it doesn't already exist. This is where kubectl stores its configuration files.
  • sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config: Copies the Kubernetes administrator configuration file to the .kube directory. This file contains the information needed to connect to the cluster.
  • sudo chown $(id -u):$(id -g) $HOME/.kube/config: Changes the ownership of the configuration file to your user account. This allows you to use kubectl without sudo.

Configuring kubectl is essential for interacting with your Kubernetes cluster. The kubectl command-line tool is your primary means of managing and controlling your cluster, so you need to ensure that it's properly configured to connect to the API server. The configuration file, admin.conf, contains all the necessary information for authenticating and communicating with the cluster. By copying this file to your .kube directory and changing its ownership, you're allowing kubectl to access the cluster using your user account without requiring sudo privileges. This makes it much more convenient to manage your cluster. The .kube directory is the standard location for kubectl configuration files, so it's important to create it if it doesn't already exist. Without this configuration, kubectl won't be able to connect to your cluster, and you won't be able to deploy applications, manage resources, or monitor the cluster's health. So, make sure you configure kubectl correctly to streamline your Kubernetes management tasks. This step is crucial for making your Kubernetes cluster accessible and manageable. By setting up kubectl properly, you're empowering yourself to take full control of your Kubernetes environment and deploy your applications with ease. It's a small but significant step that greatly enhances your Kubernetes experience.

Step 7: Deploy a Pod Network

A pod network is required for pods to communicate with each other. We'll use Calico as our pod network. Run the following command:

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
  • kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml: This command deploys the Calico pod network to your cluster. Calico is a popular and powerful networking solution for Kubernetes.

Deploying a pod network is a fundamental step in enabling communication between pods within your Kubernetes cluster. Without a pod network, pods won't be able to communicate with each other, which means your applications won't be able to function correctly. Calico is a widely used and highly regarded networking solution for Kubernetes, known for its performance, scalability, and security features. By applying the Calico manifest, you're installing the necessary components to create a virtual network that allows pods to communicate seamlessly. This includes setting up network policies, IP address management, and routing rules. The kubectl apply command deploys the Calico components to your cluster, creating the necessary resources and configurations. This can take a few minutes to complete, so be patient. Once Calico is deployed, your pods will be able to communicate with each other using their assigned IP addresses. This is essential for building multi-tier applications and microservices architectures. If you skip this step, your pods won't be able to communicate, and your applications won't function as expected. So, make sure you deploy a pod network like Calico to enable communication between your pods and unlock the full potential of your Kubernetes cluster. This step is critical for creating a functional and scalable Kubernetes environment. By choosing Calico, you're ensuring that your pod network is robust, secure, and capable of handling the demands of your containerized applications.

Step 8: Join Worker Nodes (Optional)

If you have other machines that you want to add as worker nodes, run the kubeadm join command that you saved earlier on those machines. For example:

kubeadm join <control-plane-IP>:<port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>
  • kubeadm join <control-plane-IP>:<port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>: This command joins a worker node to the Kubernetes cluster. Replace <control-plane-IP>, <port>, <token>, and <hash> with the values from the kubeadm join command that you saved earlier.

Joining worker nodes to your Kubernetes cluster is the key to scaling your environment and distributing your application workloads. Each worker node provides additional resources, such as CPU, memory, and storage, allowing you to run more pods and handle increased traffic. The kubeadm join command securely connects the worker node to the control plane, enabling it to receive instructions and manage containers. The token and discovery token CA certificate hash are used to authenticate the worker node and ensure that it's joining the correct cluster. By running the kubeadm join command on each worker node, you're expanding the capacity of your Kubernetes cluster and creating a more resilient and scalable environment. This is essential for running production-level applications that require high availability and performance. If you only have one machine, you can skip this step, but adding worker nodes is crucial for building a robust and scalable Kubernetes infrastructure. Remember to replace the placeholders in the kubeadm join command with the actual values that were provided during the control plane initialization. This will ensure that the worker nodes connect to the cluster successfully. Joining worker nodes is a fundamental step in building a production-ready Kubernetes environment. By adding more nodes, you're increasing the capacity and resilience of your cluster, allowing you to run more applications and handle more traffic. This is essential for scaling your applications and meeting the demands of your users.

Step 9: Verify the Installation

To verify that the installation was successful, run the following command:

kubectl get nodes

This command should output a list of nodes in your cluster. The status of the nodes should be Ready.

Verifying the installation is the final step in ensuring that your Kubernetes cluster is up and running correctly. The kubectl get nodes command retrieves a list of all the nodes in your cluster, including the control plane node and any worker nodes that you've joined. The output of this command should show the status of each node as Ready, indicating that the node is healthy and able to run pods. If the status of any node is NotReady, it means there's an issue with that node, and you'll need to investigate further. Common causes for a NotReady status include network connectivity problems, resource constraints, or issues with the kubelet service. If all the nodes are in the Ready state, it means your Kubernetes cluster has been successfully installed and configured, and you're ready to start deploying applications. This is a crucial step in validating your setup and ensuring that your cluster is functioning as expected. Without verifying the installation, you might encounter issues later on when deploying applications, so it's always a good idea to confirm that everything is working correctly. The kubectl get nodes command is a simple but effective way to check the health of your cluster and ensure that it's ready for production use. By verifying the installation, you're gaining confidence in your Kubernetes environment and setting yourself up for success.

Conclusion

And there you have it! You've successfully installed Kubernetes on Ubuntu 18.04. Now you can start deploying your applications and exploring the world of container orchestration. Happy Kuberneting!